temperature converter using fortran 95

7
TEMPERATURE CONVERTER USING FORTRAN 95 Here, The program of temperature converter using Fortran 95. ! DECLARED VARIABLES ! c = the temperature in degrees Celsius. ! f = the temperature in degrees Fahrenheit. ! k = the temperature in Kelvin. ! r = the temperature in degrees Rankine. ! re = the temperature in degrees Réaumur. ! t = the input value for temperature. ! scail = the user's input menu choice. It's my homophone adaptation of "scale," which happens to ! be a FORTRAN command. It isn't a CHARACTER variable because my menu is numbered. ! choice = a one character CHARACTER string for the user to input 'y', 'n', 'Y', or 'N' when ! prompted to run the program again. REAL :: c, f, k, r, re, t INTEGER :: scail CHARACTER(1) :: choice CONTAINS ! This is basically the "library" of the module. REAL FUNCTION cf(t) ! This is a REAL FUNCTION because temperature can be fractional. IMPLICIT NONE ! Its name is 'cf' and the 't' is a dummy variable used in it. REAL, INTENT(IN) :: t ! INTENT(IN) means the value comes from the program and is not ! to be modified or relayed to the program by the FUNCTION. cf = t*(9.0/5.0) + 32.0 ! This is the conversion equation. END FUNCTION cf ! This ends the FUNCTION. ! And now what follows is more of the same. As I said before, lots of delicious copypasta. REAL FUNCTION ck(t)

Upload: ali-abdurrahman-sungkar

Post on 28-Apr-2015

21 views

Category:

Documents


1 download

DESCRIPTION

Temperature Converter Using Fortran 95 .doc version

TRANSCRIPT

Page 1: Temperature Converter Using Fortran 95

TEMPERATURE CONVERTER USING FORTRAN 95

Here, The program of temperature converter using Fortran 95.

! DECLARED VARIABLES! c = the temperature in degrees Celsius.! f = the temperature in degrees Fahrenheit.! k = the temperature in Kelvin.! r = the temperature in degrees Rankine.! re = the temperature in degrees Réaumur.! t = the input value for temperature.! scail = the user's input menu choice. It's my homophone adaptation of "scale," which happens to! be a FORTRAN command. It isn't a CHARACTER variable because my menu is numbered.! choice = a one character CHARACTER string for the user to input 'y', 'n', 'Y', or 'N' when! prompted to run the program again.

REAL :: c, f, k, r, re, tINTEGER :: scailCHARACTER(1) :: choice

CONTAINS ! This is basically the "library" of the module.

REAL FUNCTION cf(t) ! This is a REAL FUNCTION because temperature can be fractional.

IMPLICIT NONE ! Its name is 'cf' and the 't' is a dummy variable used in it.

REAL, INTENT(IN) :: t ! INTENT(IN) means the value comes from the program and is not

! to be modified or relayed to the program by the FUNCTION.

cf = t*(9.0/5.0) + 32.0 ! This is the conversion equation.

END FUNCTION cf ! This ends the FUNCTION.

! And now what follows is more of the same. As I said before, lots of delicious copypasta.REAL FUNCTION ck(t)IMPLICIT NONE

REAL, INTENT(IN) :: t

ck = t + 273.15

END FUNCTION ck

REAL FUNCTION cr(t)IMPLICIT NONE

REAL, INTENT(IN) :: t

cr = (t + 273.15)*(9.0/5.0)

Page 2: Temperature Converter Using Fortran 95

END FUNCTION cr

REAL FUNCTION cre(t)IMPLICIT NONE

REAL, INTENT(IN) :: t

cre = t*(4.0/5.0)

END FUNCTION cre

REAL FUNCTION fc(t)IMPLICIT NONE

REAL, INTENT(IN) :: t

fc = (t - 32.0)*(5.0/9.0)

END FUNCTION fc

REAL FUNCTION fk(t)IMPLICIT NONE

REAL, INTENT(IN) :: t

fk = (t + 459.67)*(5.0/9.0)

END FUNCTION fk

REAL FUNCTION fr(t)IMPLICIT NONE

REAL, INTENT(IN) :: t

fr = t + 459.67

END FUNCTION fr

REAL FUNCTION fre(t)IMPLICIT NONE

REAL, INTENT(IN) :: t

fre = (t - 32.0)*(4.0/9.0)

END FUNCTION fre

REAL FUNCTION kc(t)IMPLICIT NONE

REAL, INTENT(IN) :: t

kc = t - 273.15

Page 3: Temperature Converter Using Fortran 95

END FUNCTION kc

REAL FUNCTION kf(t)IMPLICIT NONE

REAL, INTENT(IN) :: t

kf = t*(9.0/5.0) - 459.67

END FUNCTION kf

REAL FUNCTION kr(t)IMPLICIT NONE

REAL, INTENT(IN) :: t

kr = t*(9.0/5.0)

END FUNCTION kr

REAL FUNCTION kre(t)IMPLICIT NONE

REAL, INTENT(IN) :: t

kre = (t - 273.15)*(4.0/5.0)

END FUNCTION kre

REAL FUNCTION rc(t)IMPLICIT NONE

REAL, INTENT(IN) :: t

rc = (t - 491.67)*(5.0/9.0)

END FUNCTION rc

REAL FUNCTION rf(t)IMPLICIT NONE

REAL, INTENT(IN) :: t

rf = t - 459.67

END FUNCTION rf

REAL FUNCTION rk(t)IMPLICIT NONE

REAL, INTENT(IN) :: t

rk = t*(5.0/9.0)

END FUNCTION rk

Page 4: Temperature Converter Using Fortran 95

REAL FUNCTION rre(t)IMPLICIT NONE

REAL, INTENT(IN) :: t

rre = (t - 491.67)*(4.0/9.0)

END FUNCTION rre

REAL FUNCTION remc(t)IMPLICIT NONE

REAL, INTENT(IN) :: t

remc = t*(5.0/4.0)

END FUNCTION remc

REAL FUNCTION ref(t)IMPLICIT NONE

REAL, INTENT(IN) :: t

ref = t*(9.0/4.0) + 32.0

END FUNCTION ref

REAL FUNCTION rek(t)IMPLICIT NONE

REAL, INTENT(IN) :: t

rek = t*(5.0/4.0) + 273.15

END FUNCTION rek

REAL FUNCTION rer(t)IMPLICIT NONE

REAL, INTENT(IN) :: t

rer = t*(9.0/4.0) + 491.67

END FUNCTION rer

! So now all of the functions are covered, now it's time for the subroutines.

SUBROUTINE menu(scail, t) ! This is a SUBROUTINE named 'menu' that uses the variables

IMPLICIT NONE ! 'scail' and 't'.

INTEGER, INTENT(OUT) :: scail ! INTENT(OUT) means these variables will be written to by

Page 5: Temperature Converter Using Fortran 95

REAL, INTENT(OUT) :: t ! either the user or the program.

101 FORMAT(/A) ! These are the FORMATs used by the 'menu' SUBROUTINE.

110 FORMAT(A/)111 FORMAT(A)1010 FORMAT(/A/)

! What follows is the erotic text-based menu that the user sees. Notice the ADVANCE for user input.WRITE(*,101) "Please select your current temperature scale:"WRITE(*,*) "1. Celsius"WRITE(*,*) "2. Fahrenheit"WRITE(*,*) "3. Kelvin"WRITE(*,*) "4. Rankine"WRITE(*,*) "5. Réaumur"WRITE(*,101,ADVANCE="NO") "Enter the number of your selection here: "READ(*,*) scail

! This IF checks to make sure 'scail' is a valid choice. The reason I put this in is not because I! think I can do a better job than the CASE construct, but because if an invalid selection is made,! the program doesn't notice until after it asks the user for a temperature.

IF (scail < 1 .OR. scail > 5) THENWRITE(*,1010) " ### ERROR: Invalid selection. Program ending. ###"STOP

END IF

! Now the user is asked for his or her temperature. Notice the use of the ADVANCE formatter.WRITE(*,101,ADVANCE="NO") "Enter the current temperature: "READ(*,*) t

! And that's all for this SUBROUTINE.END SUBROUTINE menu

SUBROUTINE endgame(c, f, k, r, re, choice) ! This SUBROUTINE is named 'endgame' and uses the

IMPLICIT NONE! variables 'c', 'f', 'k', 'r', 're', and 'choice'.

REAL, INTENT(IN) :: c, f, k, r, re ! These are the values to be returned to the user.

CHARACTER(1), INTENT(OUT) :: choice ! This is a string to be input by the user

101 FORMAT(/A)! Once again, these are the FORMATs used by the

110 FORMAT(A/)! SUBROUTINE.

111 FORMAT(A)1010 FORMAT(/A/)

! This is the sensually organized output seen by the user.WRITE(*,101) "The current temperature is:"WRITE(*,*) c, "Celsius"WRITE(*,*) f, "Fahrenheit"

Page 6: Temperature Converter Using Fortran 95

WRITE(*,*) k, "Kelvin"WRITE(*,*) r, "Rankine"WRITE(*,*) re, "Réaumur"

! This is when the user is prompted to run the program again. Notice the ADVANCE formatter.WRITE(*,1010,ADVANCE="NO") "Do you want to run this program again? (y/n) "READ(*,*) choice

! That's all for this SUBROUTINE as well.END SUBROUTINE endgame

! And now for the big finish:END MODULE tempmod