C C ideal-gas.f -- program to compute values from the ideal gas law C C Rich Thomson, May 4th, 1994 C CS101 - FORTRAN C menu -- returns an integer indicating which type of computation we want C to perform using the ideal gas law PV = nRT, or if the user wants to exit. C C The return values indicate: C 1. Compute pressure given volume, moles of gas and temperature C 2. Compute volume given pressure, moles of gas and temperature C 3. Compute moles of gas given pressure, volume and temperature C 4. Compute temperature given pressure, volume and moles of gas C 5. Exit the program. C integer function menu() implicit none C-- choice will hold the user's input integer choice C-- initialize choice to an invalid selection, so the loop will be C-- entered the first time. choice = 0 C-- keep asking for input until we get a valid choice do while ((choice .lt. 1) .or. (choice .gt. 5)) C-- print out the menu and get the user's choice print *, 'Please select which quantity you would like to' print *, 'compute from the ideal gas law PV=nRT:' print * print *, '1. Compute pressure' print *, '2. Compute volume' print *, '3. Compute moles of gas' print *, '4. Compute temperature' print * print *, '5. Exit the program.' print * print *, 'Your choice?' read *, choice C-- warn them they've been naughty! if ((choice .lt. 1) .or. (choice .gt. 5)) then print *, 'Please enter a number from 1 to 5, inclusive.' print * endif enddo C-- return the choice to the caller menu = choice return end C input -- gets the appropriate input values from the user, given the C quantity being computed. C subroutine input(quanty, pressr, volume, moles, temp) implicit none C-- IN: the quantity to be computed (one of the menu choices from menu) integer quanty C-- OUT: the appropriate values for the other values in the gas law real pressr, volume, moles, temp C-- for some choice we don't understand, just return if ((quanty .lt. 1) .or. (quanty .gt. 4)) return C-- For each menu choice (i.e. 1 means compute pressure), we need to C-- prompt for the other quantities in the gas law. This means that C-- when the choice *isn't* to compute the pressure, we must ask for C-- the pressure, and when the choice *isn't* to compute the volume, C-- we need to ask for the volume, etc. C-- first take care of getting the pressure if (quanty .ne. 1) then print *, +'Please enter the pressure of the gas, in atmospheres:' read *, pressr endif C-- next take care of getting the volume if (quanty .ne. 2) then print *, 'Please enter the volume of the gas, in liters:' read *, volume endif C-- next, the moles of gas if (quanty .ne. 3) then print *, 'Please enter the number of moles of the gas:' read *, moles endif C-- and finally, the temperature if (quanty .ne. 4) then print *, 'Please enter the temperature of the gas,' print *, 'in degrees kelvin:' read *, temp endif return end C main -- The part that brings it all together; as long as the user C wants to keep computing values, prompts for the input data C and prints out the desired quantity. C program main implicit none C-- holds the users choice as returned by the menu integer choice C-- function that returns a menu result integer menu C-- various quantities from the ideal gas law real pressr, volume, moles, temp, gasconst C-- the ideal gas constant R, is 8.31 J/(g-mole-K) parameter (gasconst = 8.31) C-- keep doing computations until the user selects choice 5 to exit choice = 0 do while (choice .ne. 5) choice = menu() C-- get the needed quantities for the gas law call input(choice, pressr, volume, moles, temp) C-- compute the desired quantity, based on the menu choice and output it if (choice .eq. 1) then pressr = moles*gasconst*temp/volume print *, 'The pressure is', pressr, ' atmospheres.' print * else if (choice .eq. 2) then volume = moles*gasconst*temp/pressr print *, 'The volume is', volume, ' liters.' print * else if (choice .eq. 3) then moles = pressr*volume / (gasconst*temp) print *, 'The number of moles is', moles print * else if (choice .eq. 4) then temp = pressr*volume / (moles*gasconst) print *, 'The temperature is', temp, ' degrees kelvin' print * endif C-- go do the next computation enddo stop end