!################################################################## !## This program takes 2 numbers (input from keyboard) and operates on them. !## It shows how to use IF statements and Functions. !################################################################## PROGRAM Calculator !## Declare all variables to be used in program, including the function 'add'. IMPLICIT None !This means you have to declare everything (no implicit variables). !Should always use this otherwise it's easy to lose track of variables. REAL num1,num2,answer,add CHARACTER*3 operation !## Input the numbers and operation needed: ## print*,'Please input 1st number:' read(5,*)num1 print* print*,'Operation (+-*div)?' read(5,*)operation print* print*,'2nd number?' read(5,*)num2 !## Do the calculation, depending on which operation was asked for: ## if(operation.eq.'+') then !If the stuff in brackets is true, do the next line (otherwise ignore). answer=add(num1,num2) !Calls the Function 'add' which is declared after end of program. endif !Could just use num1+num2 if(operation.eq.'*') then answer=num1*num2 !Not using a function here. endif if(operation.eq.'-') then answer=num1-num2 endif if(operation.eq.'div') then !F77 has a problem with the '/' character so let's use 'div'. answer=num1/num2 endif !## Result in now stored in the variable 'answer' so we can print to screen: ## !## Here we use a particular format 'write(6,100)'. The 6 means to screen and the 100 !## is a format we define below. The 100 in the FORMAT statement should be one !## space in from the left. 'f8.2' means a number with 8 character places (including decimal !## point) and 2 numbers after the decimal point. The 'a4' means a string 4 characters long. print*,'===================================' write(6,100)num1,operation,num2,' = ',answer print*,'===================================' 100 FORMAT(f8.2,a5,f8.2,a4,f9.3) stop END !## End the program before writing the functions. ## !############################################## !## Here is the 'add' function - after the main program. !## when you use 'add(blah,blah)' the computer comes !## down here and does the calculation. It returns the !## value 'add' to the main program. !############################################## REAL function add(x,y) implicit none real x,y add=x+y return end !## Don't forget 'return' and 'end' - it's like a separate litle program. ## !#######################################################