home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Learn Pascal in a Three Days (2nd Ed.)
(Publisher: Wordware Publishing, Inc.)
Author(s):
ISBN: 1556225679
Publication Date: 07/01/97

Bookmark It

Search this book:
 
Previous Table of Contents Next


Drill 9-3

Add a procedure to the last program to display a Payroll Summary report as shown:

------- PayRoll Summary -------
ID ------ Name ------ Salary
122  Tammy M. Ockman $3592.00
123  Tara S. Strahan $2438.40
125  John G. Trainer $4011.28
------------------------

The program may also be modified in such a way as to read the “HoursWorked” from the file TIMSHEET.EMP which you created in Drill 9-1.

9-6 Non-Text Files

The TEXT file is a special predefined type of file, but as mentioned earlier the general definition of a file allows the file components to be of any type other than the type FILE. You can declare a file of any predefined or user-defined type using the form:

type-identifier = FILE OF component-type;

The component type can be a simple type (like INTEGER), a structured type (like an array), or a user-defined type (like a record).

The following is an example of a file declaration whose components are records (a simplified form of “EmployeeRecord” is used to make the program shorter):

    TYPE
      EmployeeRecord = RECORD
               ID       :INTEGER;
               Name     :STRING[20];
               Rate     :REAL;
             END;
      EmpFileRec = FILE OF EmployeeRecord;
    VAR
      F     :EmpFileRec;         { The file variable }
      EmployeeRec:EmployeeRecord;         { The record variable }

The main properties of non-TEXT files are:

1.  Data are represented in the internal binary format, which means that you cannot display the contents of a file using the DOS command TYPE. This also speeds up the transfer of data to and from the file.
2.  The main advantage of non-TEXT files comes when using structured types such as arrays or records, because then you do not need to read or write the record field by field. For example, after the previous declarations you may read or write the whole record using these statements:
READ(F, EmployeeRec);
WRITE(F, EmployeeRec);
3.  Because non-TEXT files are not made up of lines as TEXT files are, the procedures READLN and WRITELN may not be used with these files.

Application 4: Payroll System

This is the same payroll program but in a better shape. The program is divided into two separate modules (programs). The first module (figure 9-8) reads the employees’ records from the keyboard and stores them in a non-TEXT file EMPFILE.BIN. In the second module (figure 9-9) the “HoursWorked” are entered from the keyboard and wages are calculated and written to the file PAYFILE.TXT, which is a TEXT file. The first program may be used only once to create the employee file, but the second program is used every pay period to create the “PayFile.”

Here is the first program:

{ ------------------------------ figure 9-8 ------------------------------ }
PROGRAM EmpPayInfo(INPUT,OUTPUT,F);
{ This program is used to create a user-defined file “EMPFILE.BIN”
  whose components are records. }
TYPE
  EmployeeRecord = RECORD
            ID       :INTEGER;
            Name     :STRING[20];
            Rate     :REAL;
          END;
  EmpFileRec = FILE OF EmployeeRecord;
VAR
  F     :EmpFileRec;         { The file variable }
  EmployeeRec:EmployeeRecord;
{ ----------- Procedure WriteRecord ---------- }
PROCEDURE WriteRecord;
BEGIN
{ Store one record in the file }
  WRITE(F, EmployeeRec)
END;
{ ----------- Procedure GetData ---------- }
PROCEDURE getdata;
VAR
  Counter:INTEGER;
BEGIN
  Counter:= 0;
  WITH EmployeeRec DO
    BEGIN
      WRITE('Please enter Employee ID (or 0 to end):'); READLN(ID);
      WHILE ID <> 0 DO
        BEGIN
          Counter:= counter + 1;
          WRITE('Employee Name: '); READLN(Name);
          WRITE('Hourly Rate: '); READLN(Rate);
          WriteRecord;
          WRITE('Please enter Employee ID (or 0 to end):'); READLN(ID)
        END
    END;
  WRITELN(Counter, ' Employee records have been filed.')
END;
{ ---------------- Main Program -------------- }
{ Main Program }
BEGIN
  ASSIGN(F, 'EMPFILE.BIN'); REWRITE(F);
  GetData;
  CLOSE(F);
  WRITELN('Press ENTER to continue..');
  READLN
END.

The second module (PayRoll2) is made up of four procedures:

• “GetInfo” to read one record of the file EMPFILE.BIN
• “CalcWages” to carry out the calculations
• “FilePayroll” to write a record to the file PAYFILE.TXT
• “ReadPayRoll” to read the file PAYFILE.TXT and display the payroll at the end of the process
{ ------------------------------ figure 9-9 ------------------------------ }
PROGRAM PayRoll2(INPUT,OUTPUT,MasterFile,PayFile);
{ This program reads the file “EMPFILE.BIN” one record at a time,
 then calculates wages and stores the output in the text file
 “PAYFILE.TXT” }
TYPE
  EmployeeRecord = RECORD
            ID :INTEGER;
            Name:STRING[20];
            Rate:REAL;
          END;
  PayRecord = RECORD
         ID :INTEGER;
         Name  :STRING[20];
         Wages :REAL;
       END;
  EmployeeFile = FILE OF EmployeeRecord;
VAR
  MasterFile    :EmployeeFile;
  PayFile     :TEXT;
  EmployeeRec     :EmployeeRecord;
  PayRec        :PayRecord;
  HoursWorked, Wages :REAL;
{ --------------- Procedure GetInfo ------------------ }
{ This procedure reads and displays a record from
the file “EMPFILE.BIN”    }
PROCEDURE GetInfo(VAR F:EmployeeFile);
BEGIN
  READ(F,EmployeeRec);
  WITH EmployeeRec DO
    BEGIN
      WRITELN('ID: ',ID);
      WRITELN('Name: ',Name);
      WRITELN('Hourly rate: $', Rate:0:2);
    END;
END;
{ --------------- Procedure CalcWages ---------------- }
PROCEDURE CalcWages(HoursWorked:REAL; VAR Wages:REAL);
BEGIN
  WITH EmployeeRec DO
    Wages := Hoursworked * Rate;
  Wages := ROUND(100 * Wages) / 100 { rounding cents }
END;
{ -------------- Procedure FilePayRoll --------------- }
{ This procedure writes a record to “PAYFILE.TXT” }
PROCEDURE FilePayRoll(VAR F:EmployeeFile; VAR P:TEXT; Wages:REAL);
BEGIN
  WITH EmployeeRec DO
    BEGIN
      PayRec.ID:= ID;
      PayRec.Name:= Name;
      PayRec.Wages:= Wages
    END;
  WITH PayRec DO
    BEGIN
      WRITELN(P, ID);
      WRITELN(P, Name);
      WRITELN(P, Wages);
    end;
END;
{ -------------- Procedure ReadPayRoll --------------- }
{ This procedure reads and displays “PAYFILE.TXT” }
PROCEDURE ReadPayRoll(VAR P:TEXT);
VAR
  I :INTEGER;
BEGIN
  WITH PayRec DO
    BEGIN
      READLN(P, ID);
      READLN(P, Name);
      READLN(P, Wages);
      WRITE(ID:3,' ');
      WRITE(Name);
{ Fill the rest of the 20 places with blanks }
      FOR I:= 1 TO 20-LENGTH(Name) DO
        WRITE(' ');
      WRITELN(' $',Wages:0:2)
    END;
END;
{ --------------- Main Program ------------------ }
BEGIN
  ASSIGN(MasterFile, 'EMPFILE.BIN'); RESET(MasterFile);
  ASSIGN(Payfile, 'PAYFILE.TXT'); REWRITE(PayFile);
  WHILE NOT EOF(MasterFile) DO
    BEGIN
      GetInfo(MasterFile);
      WRITE('Please enter hours worked for this pay period: ');
      READLN(HoursWorked);
      CalcWages(HoursWorked, Wages);
      FilePayRoll(MasterFile, PayFile, Wages)
    END;
  CLOSE(MasterFile);
  CLOSE(PayFile);
  RESET(PayFile);
  WRITELN('--------- PayRoll Summary --------- ');
  WRITELN('ID # ------- Name -------- Salary');
  WHILE NOT EOF(PayFile) DO
    ReadPayroll(PayFile);
  WRITELN('----------------------------------- ');
  CLOSE(PayFile);
  WRITELN('Press ENTER to continue..');
  READLN
END.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.