Date post: | 04-Jan-2016 |
Category: |
Documents |
Upload: | barnaby-simpson |
View: | 227 times |
Download: | 0 times |
Program Payroll_Calculator;Uses Crt, Printer;
ConstA_Payrate = 100.0;B_Payrate = 50.0;
VarSS_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;
Reading Input that is Integer and/or Real
• Space is a delimitor
• 12 45 60.5
Readln(Personnel, hours1, hours2, hours3)
Writeln(lst,SS,blank,First_Name);
Sends output to printer
Variables }
Don’t forget to includethe ‘Uses Printer’ statement
(Grade = ‘A’) XOR (Class = ‘COP1000’)
With ‘XOR’ only one can be true --
not both
Expression or Condition
(Not Grade = ‘A’) and (Class = ‘COP1000’)
Not evaluates the expression and then gives it the opposite value.
Expression or Condition
IF Score > 90 THEN
Letter_Grade := ‘A’;
If the student’s grade is 91 or better,then the variable Letter_Grade will havea value of ‘A’. If the score is less than 91then no value will be assigned to Letter_Grade
IF Score > 90 THEN Letter_Grade := ‘A’ELSE Letter_Grade := ‘B’;
This code means everyone willget an A or a B
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’;
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;
Where do they fit?
Program name;
CONST
VAR
Procedure name
Begin
____
____
End;
Begin (*Program Body *)
____
_____
End;
Begins with the wordProcedure
Give the pocedurea name
Procedure Open_Files;
Begin
Assign(Income, Data_File);
End;
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
Naming a Procedure
Usually, procedures have a verb
indicating purpose of procedure
Example: Calculate_Scores
Global Variables
• Declared at the beginning of the program
• All procedures, all code recognizes global variables
Local Variables
• Declared within the procedure
• Known only within the procedure
• Protects against unintended consequences
Procedure Compare_Values (VAR Val1 : integer;
VAR Val2 : Real);
Var
I : Integer;
Begin
For I := 1 to Val1 DO
Val2 := val2 + val1
End;
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.
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
BeginOpen_files;Initialize_Accumulators(WH_Total, SS_Total, Gross_Total, Net_Total);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
BeginReadln(Personnel, dataline);Personnel_SS := dataline;If personnel_ss = payroll_SS then
Begin Parse_Dataline (personnel_SS, First_Name,
last_name, paygrade); ________
________
Formal Parameter
Procedure Parse_Data(DATA : STRING; VARVAR 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
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 *)
XY
Z
Scope of Each Variable
Differences BetweenFunctions and Procedures
• Procedures capable of returning to the calling program more than one value.
• Functions return only one value.
Function Parse_Name(Data : String) : String
Keyword
FunctionName
Parameters
Data type of value the function returns
Important!
A function does not automatically
assign a value to a variable.
You must program it that way
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 ofthe blank in a variable. It can not be used later in the program.