(* ============================================================================== * File: GradeCalculator2.p * Purpose: Simple program to calculate the percentage and letter grade * with procedures and functions * Author: Jordan Kidney ( kidney@cpsc.ucalgary.ca ) * Created on: Feb 24, 2006 ============================================================================== *) program GradeCalculator2 (input,output); (*----------------------- readTotalMarks ---------------------------------------- * Purpose: prompts the user to enter in the total marks for the test. This * function also does some basic error checking on the number entered. * returns: the number entered by the user as a real *------------------------------------------------------------------------------ *) function readTotalMarks : real; begin var marks : real; var EndLoop : boolean; (* Used as a flag to end loops for error checking *) EndLoop := false; repeat write('Enter the total number of marks : '); readln(marks); if (marks <= 0) then writeln('Error: The number must be greater then zero.') else EndLoop := true until EndLoop = true; readTotalMarks := marks; end; (*----------------------- readUsersMark ----------------------------------------- * Purpose: prompts the user to enter the mark they got for the test. This * function also does some basic error checking on the number entered. * Parameters: TotalMarks - the total number of possible marks on the test. This * value is used for error checking. * returns: the number entered by ther use as a real * ------------------------------------------------------------------------------ *) function readUsersMark(TotalMarks : real) : real; begin var mark : real; var EndLoop : boolean; (* Used as a flag to end loops for error checking *) EndLoop := false; repeat write('Enter your mark : '); readln(mark); if ((mark < 0) or (mark > TotalMarks)) then writeln('Error: inncorect input') else EndLoop := true until EndLoop = true; readusersMark := mark; end; (*----------------------- computePercentage ------------------------------------ * Purpose: computes the percentage the user got * Parameters: TotalMarks - the total number of possible marks on the test. * Mark - the mark the user got on the test * returns: the number entered by ther use as a real *----------------------------------------------------------------------------- *) function computePercentage(TotalMarks , Mark : real ) : real; begin computePercentage := (Mark * 100.0)/TotalMarks; end; (*----------------------- findLetterGrade ----------------------------------------- * Purpose: find the letter grade based upon a given percentage value * Parameters: percentage - the percentage used to find the letter grade * returns: the letter grade as a string *------------------------------------------------------------------------------- *) function findLetterGrade(percentage : real ) : string; begin var LetterGrade : string[3]; if (Percentage < 39.0) then LetterGrade := 'F' else if (Percentage < 44.0) then LetterGrade := 'D-' else if (Percentage < 49.0) then LetterGrade := 'D' else if (Percentage < 54.0) then LetterGrade := 'D+' else if (Percentage < 59.0) then LetterGrade := 'C-' else if (Percentage < 64.0) then LetterGrade := 'C' else if (Percentage < 69.0) then LetterGrade := 'C+' else if (Percentage < 74.0) then LetterGrade := 'B-' else if (Percentage < 79.0) then LetterGrade := 'B' else if (Percentage < 84.0) then LetterGrade := 'B+' else if (Percentage < 89.0) then LetterGrade := 'A-' else LetterGrade := 'A'; findLetterGrade := LetterGrade; end; (* ========================== MAIN ============================================== *) begin var Mark : real; (* The mark the user got *) var TotalMarks : real; (* Total number of possible marks *) var Percentage : real; (* Used to store the calculated percentage value *) var LetterGrade : string[3]; (* Used to store the letter cade for the mark *) TotalMarks := readTotalMarks; Mark := readUsersMark(TotalMarks); Percentage := computePercentage(TotalMarks,Mark); LetterGrade := findLetterGrade(Percentage); (* Write information to the console *) writeln('You got ', Percentage:0:2, '% on the test which gives you a letter grade of ',LetterGrade); end. (* ============================================================================== *)