! Example P.2. (Catherine Spade, 4 October 1999) ! Examples of intrinsic library functions. program A02 implicit none real :: A real, parameter :: B = 1.234, C = 5.678 integer, parameter :: I = 9, J = 10 ! start program A02 A = sqrt( B ) + abs( C ) write (unit = *, fmt = *) A, int( A ), nint( A ), real( I ), I write (unit = *, fmt = *) I / J, modulo( I, J ), abs( I ), sign( I, 1 ), sign( I, -1 ) write (unit = *, fmt = *) B, B / C, modulo( B, C ), abs( B ) write (unit = *, fmt = *) sign( B, 1.0 ), sign( B, -1.0 ) write (unit = *, fmt = *) atan2( 0.0, -1.0 ), exp( 1.0 ) write (unit = *, fmt = *) cos( 1.0 ), sin( 1.0 ), tan( 1.0 ), log( 2.0 ), log10( 2.0 ) stop end program A02 ! Example P.3. (Catherine Spade, 6 October 1999) ! Temperature Converter. program A03 implicit none real, parameter :: T_SCALE = 1.8, OFFSET = 32.0 ! named constants real :: Fahr, Cels ! type declaration ! start program A03 read (unit = *, fmt = *) Fahr ! input Cels = (Fahr - OFFSET) / T_SCALE ! assignment write (unit = *, fmt = *) Fahr, Cels ! output stop end program A03 ! Example P.5. (Catherine Spade, 8 October 1999) ! Temperature Converter with Repetition. program A05 implicit none integer, parameter :: LIMIT = 10 real, parameter :: T_SCALE = 1.8, OFFSET = 32.0 real :: Fahr, Cels integer :: Loop ! start program A05 do Loop = 1, LIMIT write (unit = *, fmt = *) " Please enter Fahrenheit temperature ", Loop, ":" read (unit = *, fmt = *) Fahr Cels = (Fahr - OFFSET) / T_SCALE write (unit = *, fmt = *) Fahr, Cels end do stop end program A05 program aa character (len=50) :: a, b, c, d, e integer :: i, j a = "aaa bbbb ccccc dddddd eeeeeee" print *, a i = index(a," ")+1 b = a(:i-2) print *, b c = a(i:) print *, c j = index (c," ") +1 d = c(:j-2) print *, d e = d(j:) print *, e end program aa ! Exercise Ohm's Law. program A1X01 implicit none real, parameter :: Curr = 1.2, Resist = 3.4 ! start program A1X01 write (unit = *, fmt = *) Curr, Resist, Curr * Resist stop end program A1X01 ! Exercise Ohm's Law. program A1X01 implicit none real, parameter :: Curr = 1.2, Resist = 3.4 ! start program A1X01 write (unit = *, fmt = *) Curr, Resist, Curr * Resist stop end program A1X01 ! Temperature Converter with Procedures. program A08 use A08M implicit none real :: Fahr, Cels ! start program A08 call Input( Fahr ) ! Subroutine reference Cels = Temp_C( Fahr ) ! Function reference call Output( Fahr, Cels ) ! Subroutine reference stop end program A08 module A08M implicit none public :: Input, Temp_C, Output contains subroutine Input( F_Temp ) real, intent(out) :: F_Temp ! start subroutine Input write (unit = *, fmt = *) " Please enter the Fahrenheit temperature. " read (unit = *, fmt = *) F_Temp return end subroutine Input function Temp_C( F_Temp ) result( Temp_C_R ) real, intent(in) :: F_Temp real :: Temp_C_R real, parameter :: T_SCALE = 1.8, OFFSET = 32.0 ! start function Output Temp_C_R = (F_Temp - OFFSET) / T_SCALE return end function Temp_C subroutine Output( F_Temp, Temp_C_R ) real, intent(in) :: F_Temp, Temp_C_R ! start subroutine Output write (unit = *, fmt = *) F_Temp, " deg. F = ", Temp_C_R, " deg. C" return end subroutine Output end module A08M 0000015544 00000 n