Example 4: Program Source code
identification division.
program-id. example.
author. Jacques Levin.
date-written. Nov 23 1995.
*
* This program is taken from M.B.khan's textbook on COBOL
* Fig 5-10 page 114
*
* This program was modified to accept negative numbers
*
environment division.
*
input-output section.
file-control.
select employee-file-in
assign to input "sign.in".
select employee-file-out
assign to output "sign.out".
*
data division.
*
file section.
*
* describe the input file
*
fd employee-file-in
label records standard
block contains 5 records
record contains 31 characters
data record is employee-record-in.
01 employee-record-in.
02 employee-name-in pic x(20).
02 employee-rate-in pic S9(2)v99 sign is leading separate character.
02 employee-hours-in pic 9(3)v99.
02 line-feed-in pic x(1).
*
* describe the output file
*
fd employee-file-out
label records omitted
record contains 56 characters
data record is print-line.
01 print-line.
02 employee-name-out pic x(20).
02 filler pic x(5).
02 employee-rate-out pic -9(2).99.
02 filler pic x(5).
02 employee-hours-out pic 9(3).99.
02 filler pic x(5).
02 gross-pay-out pic -9(4).99.
02 line-feed-out pic x(1).
working-storage section.
01 control-fields.
02 end-of-file pic x(1) value 'n'.
02 special-value pic S9(5)v99 value -15.00 sign is leading separate character.
01 working-fields.
02 ws-gross-pay pic S9(5)v99 value 0 sign is leading separate character.
02 ws-overtime-pay pic S9(5)v99 value 0 sign is leading separate character.
02 ws-overtime-hours pic 9(5)v99 value 0.
02 ws-overtime-rate pic S9(5)v99 value 0 sign is leading separate character.
procedure division.
*
* main paragraph.
*
a000-control-logic.
perform b100-initialize-processing.
perform b200-process-data
until end-of-file equal to 'y'.
perform b300-terminate-processing.
stop run.
*
* open input/output files.
*
b100-initialize-processing.
open input employee-file-in
output employee-file-out.
perform x100-read-data.
*
* process data.
*
b200-process-data.
perform c100-calc-regular-pay.
if employee-rate-in is negative
display ws-gross-pay.
if employee-rate-in is equal to special-value
display ws-gross-pay.
if employee-hours-in is greater than 40
perform c200-calc-overtime-pay.
display employee-rate-in ws-overtime-rate ws-overtime-pay ws-gross-pay.
perform x200-write-data.
perform x100-read-data.
*
* close input/output files.
*
b300-terminate-processing.
close employee-file-in
employee-file-out.
*
* calculate regular pay.
*
c100-calc-regular-pay.
if employee-hours-in is greater than 40
multiply employee-rate-in by 40
giving ws-gross-pay
else
multiply employee-rate-in by employee-hours-in
giving ws-gross-pay.
*
* calculate overtime pay.
*
c200-calc-overtime-pay.
subtract 40 from employee-hours-in
giving ws-overtime-hours.
multiply 1.5 by employee-rate-in
giving ws-overtime-rate.
multiply ws-overtime-rate by ws-overtime-hours
giving ws-overtime-pay.
add ws-overtime-pay to ws-gross-pay.
*
* read input file.
*
x100-read-data.
read employee-file-in
at end move 'y' to end-of-file.
*
* write output file.
*
x200-write-data.
move employee-name-in to employee-name-out.
move employee-rate-in to employee-rate-out.
move employee-hours-in to employee-hours-out.
move ws-gross-pay to gross-pay-out.
move line-feed-in to line-feed-out.
write print-line.