+ All Categories
Home > Documents > Files Input and Output Program Payroll_Calculator; Uses Crt, Printer; Const A_Payrate = 100.0;...

Files Input and Output Program Payroll_Calculator; Uses Crt, Printer; Const A_Payrate = 100.0;...

Date post: 04-Jan-2016
Category:
Upload: barnaby-simpson
View: 227 times
Download: 0 times
Share this document with a friend
57
Transcript

FilesInput and Output

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;

Begin clrscr;

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

Reset opens a file to ReadRewrite opens a file to Write

Rewrite erases any previous contents

Close(Personnel);End.

(previous code)

Reading Input from a File

Readln(Personnel, Data_line);

File Variable

variable

Reading Input that is Integer and/or Real

• Space is a delimitor

• 12 45 60.5

Readln(Personnel, hours1, hours2, hours3)

String Input

Size can be a delimitor

Use String Functions to Separate

Writing to a File

Writeln(Personnel,SS,blank,First_Name);

File Variable

Variables }

Writeln(lst,SS,blank,First_Name);

Sends output to printer

Variables }

Don’t forget to includethe ‘Uses Printer’ statement

Branching

Option A

Option B

Option C

Evaluation of a condition

Conditions

• Equality Grade = A

• Greater Than A > B

• Lesser Than B < C

Truth

• Boolean Operators

• AND

• OR

• XOR

• NOT

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

True True

(Grade = ‘A’) or (Class = ‘COP1000’)

With ‘or’ at least one must be true

Expression or Condition

(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

The IF Statement

IF THENELSE

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’;

No Punctuation beforethe ‘ELSE’

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;

Vocabulary

• Iteration

• Procedures

• Procedure Calls

• Encapsulation

What do procedures do?

• Organize code by tasks

• Allows reusable code

• Protects values

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

Local Variables vs

Global Variables

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;

Parameters

values that are passed to functions, procedures or other subprograms

Passing Parameters

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.

Begin

________;

________:

Compare_Values (Value1, Value2);

________;

________;

End.

Parameters

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

Value1

Val1 Val2

Value2

Procedure

Call

Make sure your data types are consistent

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

Scope

Definition

Scope concerns the

visability of

variables

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

Functions

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.

RecursionA function which

calls itself

That’s Enough

The End


Recommended