Presentation is loading. Please wait.

Presentation is loading. Please wait.

Files Input and Output Program Payroll_Calculator; Uses Crt, Printer; Const A_Payrate = 100.0; B_Payrate = 50.0; Var SS_personnel : string[9]; First_Name,

Similar presentations


Presentation on theme: "Files Input and Output Program Payroll_Calculator; Uses Crt, Printer; Const A_Payrate = 100.0; B_Payrate = 50.0; Var SS_personnel : string[9]; First_Name,"— Presentation transcript:

1

2

3 Files Input and Output

4 Program Payroll_Calculator; Uses Crt, Printer; Const A_Payrate = 100.0; B_Payrate = 50.0; Var SS_personnel : string[9]; First_Name, Last_Name : String[20]; Paygrade : String[1]; Data_Line : String; Gross_Pay, Net_Pay, Withholding_Amount, SS_WithHolding_Amount,Deductions : Real; Gross_Pay_Accumulator : real; Personnel : Text; Found : Boolean; Blank_Location,error : Integer;

5 Begin clrscr; Assign(Personnel, 'A:\Personel.txt'); Reset(Personnel);

6 Reset opens a file to Read Rewrite opens a file to Write Rewrite erases any previous contents

7 Close(Personnel); End. (previous code)

8 Reading Input from a File Readln(Personnel, Data_line); File Variable variable

9 Reading Input that is Integer and/or Real Space is a delimitor 12 45 60.5 Readln(Personnel, hours1, hours2, hours3)

10 String Input Size can be a delimitor Use String Functions to Separate

11 Writing to a File Writeln(Personnel,SS,blank,First_Name); File Variable Variables }

12 Writeln(lst,SS,blank,First_Name); Sends output to printer Variables } Don’t forget to include the ‘Uses Printer’ statement

13 Branching

14 Option A Option B Option C Evaluation of a condition

15 Conditions Equality Grade = A Greater Than A > B Lesser Than B < C

16 Truth Boolean Operators AND OR XOR NOT

17 (Grade = ‘A’) and (Class = ‘COP1000’) True

18 (Grade = ‘A’) or (Class = ‘COP1000’) With ‘or’ at least one must be true Expression or Condition

19 (Grade = ‘A’) XOR (Class = ‘COP1000’) With ‘XOR’ only one can be true -- not both Expression or Condition

20 (Not Grade = ‘A’) and (Class = ‘COP1000’) Not evaluates the expression and then gives it the opposite value. Expression or Condition

21 The IF Statement

22 IF THEN ELSE

23 IF Score > 90 THEN Letter_Grade := ‘A’; If the student’s grade is 91 or better, then the variable Letter_Grade will have a value of ‘A’. If the score is less than 91 then no value will be assigned to Letter_Grade

24 IF Score > 90 THEN Letter_Grade := ‘A’ ELSE Letter_Grade := ‘B’; This code means everyone will get an A or a B

25 IF Score > 90 THEN Letter_Grade := ‘A’ ELSE IF Score > 80 THEN Letter_Grade := ‘B’ ELSE IF Score > 70 THEN Letter_Grade := ‘C’ ELSE Letter_Grade := ‘D’;

26 No Punctuation before the ‘ELSE’

27 If Paygrade = 'A' then Begin (* Paygrade=A *) Gross_Pay := Hours_Worked * A_Payrate; Withholding_amount := Gross_Pay * A_withholding_Rate; End (* Paygrade=A *) else Begin (* Paygrade=B *) Gross_Pay := Hours_Worked * B_Payrate; Withholding_Amount := Gross_Pay * B_Withholding_Rate; end; (*Paygrade=B*) SS_Withholding_Amount := Gross_Pay * SS_Withholding_Rate; Deductions := SS_withholding_Amount + withholding_Amount; Net_pay := Gross_Pay - Deductions; Gross_Pay_Accumulator := Gross_Pay_Accumulator + Gross_Pay; Net_Pay_Accumulator := Net_Pay_Accumulator + Net_Pay; WH_Accumulator := WH_Accumulator + Withholding_Amount; SS_Accumulator := SS_Accumulator + SS_Withholding_Amount;

28

29 Vocabulary Iteration Procedures Procedure Calls Encapsulation

30 What do procedures do? Organize code by tasks Allows reusable code Protects values

31 Where do they fit? Program name; CONST VAR Procedure name Begin ____ End; Begin (*Program Body *) ____ _____ End;

32 Begins with the word Procedure Give the pocedure a name Procedure Open_Files; Begin Assign(Income, Data_File); End;

33 Begin Open_Files; Initialize_Count; While Not EOF(Income) DO Begin If Sex = ‘F’ Then Add_Females Else Add_Males; End; Compute_Averages; Output_Results; Close_Files; End; ‘Calling’ the Procedure

34 Naming a Procedure Usually, procedures have a verb indicating purpose of procedure Example: Calculate_Scores

35 Local Variables vs Global Variables

36 Global Variables Declared at the beginning of the program All procedures, all code recognizes global variables

37 Local Variables Declared within the procedure Known only within the procedure Protects against unintended consequences

38 Procedure Compare_Values ( VAR Val1 : integer; VAR Val2 : Real) ; Var I : Integer; Begin For I := 1 to Val1 DO Val2 := val2 + val1 End;

39 Parameters values that are passed to functions, procedures or other subprograms

40 Passing Parameters

41 What’s the difference ??? Reference, Formal or VAR parameters pass the address of the variable to the procedure. Thus any changes the procedure makes to the variable is passed back to the calling program. Value parameters pass a copy of the variable. Any changes the procedure makes to the value of the variable is not retained in the calling program.

42 Begin ________; ________: Compare_Values (Value1, Value2); ________; End. Parameters

43 Procedure Compare_Values ( VAR Val1 : integer; VAR Val2 : Real) ; Var I : Integer; Begin For I := 1 to Val1 DO Val2 := val2 + val1 End; Order is important. The parameters in the code which calls the procedure must be in the same order as the procedure

44 Value1 Val1 Val2 Value2 Procedure Call Make sure your data types are consistent

45

46 Begin Open_files; Initialize_Accumulators(WH_Total, SS_Total, Gross_Total, Net_Total); While not eof(Payroll) Do Begin (* payroll outer loop *) Readln(Payroll, Payroll_SS, Hours_Worked); Initialize_Inner_Loop; While (not eof(personnel)) and not found do Begin Readln(Personnel, dataline); Personnel_SS := dataline; If personnel_ss = payroll_SS then Begin Parse_Dataline (personnel_SS, First_Name, last_name, paygrade); ________ Formal Parameter

47 Procedure Parse_Data(DATA : STRING; VAR VAR FNAME, LNAME : String); CONST BLANK = ‘ ‘; VAR Blank Location : Integer; BEGIN Blank_Location := POS(Data,Blank); FNAME := COPY(DATA,1,Blank_Location -1); Delete(Data, 1, Blank_Location); LNAME := DATA; END; Value Actual Local

48 Scope

49 Definition Scope concerns the visability of variables

50 Program Example; VAR X : Integer; Procedure A; VAR y : Integer; Procedure B Var z : Integer; Begin (*Procedure B *) ____ End; (*Procedure B *) Begin (* Procedure A *) End; (* Procedure A *) Begin (*Program Code *) X Y Z Scope of Each Variable

51 Functions

52 Differences Between Functions and Procedures Procedures capable of returning to the calling program more than one value. Functions return only one value.

53 Function Parse_Name(Data : String) : String Keyword Function Name Parameters Data type of value the function returns

54 Important! A function does not automatically assign a value to a variable. You must program it that way

55 FIRST_NAME := COPY(DATA, 1, POS(DATA,BLANK) -1) Blank_Location := POS(DATA,BLANK); FIRST_NAME := COPY(DATA,1,Blank_Location-1) note: the second method does not store the location of the blank in a variable. It can not be used later in the program.

56 Recursion A function which calls itself

57 That’s Enough

58 The End


Download ppt "Files Input and Output Program Payroll_Calculator; Uses Crt, Printer; Const A_Payrate = 100.0; B_Payrate = 50.0; Var SS_personnel : string[9]; First_Name,"

Similar presentations


Ads by Google