From 9593a595363f8bddc00767d4aa864eca4440d4bf Mon Sep 17 00:00:00 2001 From: YL Wang <121748352+code4yonglei@users.noreply.github.com> Date: Wed, 4 Sep 2024 14:06:27 +0200 Subject: [PATCH] fortran code sample for 2nd exercise (conditioals) --- .../code/02_conditionals-f/hello-world.f90 | 15 +++++++++++ content/code/02_conditionals-f/message.f90 | 25 +++++++++++++++++++ .../02_conditionals-f/solution/CMakeLists.txt | 19 ++++++++++++++ .../solution/hello-world.f90 | 15 +++++++++++ .../02_conditionals-f/solution/message.f90 | 25 +++++++++++++++++++ 5 files changed, 99 insertions(+) create mode 100644 content/code/02_conditionals-f/hello-world.f90 create mode 100644 content/code/02_conditionals-f/message.f90 create mode 100644 content/code/02_conditionals-f/solution/CMakeLists.txt create mode 100644 content/code/02_conditionals-f/solution/hello-world.f90 create mode 100644 content/code/02_conditionals-f/solution/message.f90 diff --git a/content/code/02_conditionals-f/hello-world.f90 b/content/code/02_conditionals-f/hello-world.f90 new file mode 100644 index 0000000..d0642d9 --- /dev/null +++ b/content/code/02_conditionals-f/hello-world.f90 @@ -0,0 +1,15 @@ +program hello_world + + use messaging + + implicit none + + type(Messenger) :: hello, bye + + hello%message_ = 'Hello, CMake world!' + print *, print_message(hello) + + bye%message_ = 'Bye, CMake world!' + print *, print_message(bye) + +end program diff --git a/content/code/02_conditionals-f/message.f90 b/content/code/02_conditionals-f/message.f90 new file mode 100644 index 0000000..bd9a8c2 --- /dev/null +++ b/content/code/02_conditionals-f/message.f90 @@ -0,0 +1,25 @@ +module messaging + + implicit none + + public Messenger + type Messenger + character(len=19) :: message_ + end type + + public print_message + + private + +contains + + pure function print_message(postman) result(m) + + type(Messenger), intent(in) :: postman + character(len=19) :: m + + m = postman%message_ + + end function + +end module diff --git a/content/code/02_conditionals-f/solution/CMakeLists.txt b/content/code/02_conditionals-f/solution/CMakeLists.txt new file mode 100644 index 0000000..4bb1b2e --- /dev/null +++ b/content/code/02_conditionals-f/solution/CMakeLists.txt @@ -0,0 +1,19 @@ +# set minimum cmake version +cmake_minimum_required(VERSION 3.18) + +# project name and language +project(conditionals LANGUAGES Fortran) + +set(MAKE_SHARED_LIBRARY OFF) + +if(MAKE_SHARED_LIBRARY) + message(STATUS "Build shared library") + add_library(message SHARED message.f90) +else() + message(STATUS "Build static library") + add_library(message STATIC message.f90) +endif() + +add_executable(hello-world hello-world.f90) + +target_link_libraries(hello-world PRIVATE message) diff --git a/content/code/02_conditionals-f/solution/hello-world.f90 b/content/code/02_conditionals-f/solution/hello-world.f90 new file mode 100644 index 0000000..d0642d9 --- /dev/null +++ b/content/code/02_conditionals-f/solution/hello-world.f90 @@ -0,0 +1,15 @@ +program hello_world + + use messaging + + implicit none + + type(Messenger) :: hello, bye + + hello%message_ = 'Hello, CMake world!' + print *, print_message(hello) + + bye%message_ = 'Bye, CMake world!' + print *, print_message(bye) + +end program diff --git a/content/code/02_conditionals-f/solution/message.f90 b/content/code/02_conditionals-f/solution/message.f90 new file mode 100644 index 0000000..bd9a8c2 --- /dev/null +++ b/content/code/02_conditionals-f/solution/message.f90 @@ -0,0 +1,25 @@ +module messaging + + implicit none + + public Messenger + type Messenger + character(len=19) :: message_ + end type + + public print_message + + private + +contains + + pure function print_message(postman) result(m) + + type(Messenger), intent(in) :: postman + character(len=19) :: m + + m = postman%message_ + + end function + +end module