Example 3: Program Source code
identification division.
program-id. sort.
author. Jacques Levin.
date-written. Nov 23 1995.
*
* This program is taken grom M.B.khan's textbook on COBOL
* Fig 2.7 page 26
*
* To run this program, compile it with "ccbl sort.cbl"
* Then run it with "runcbl"
*
environment division.
*
input-output section.
file-control.
select employee-file-in
assign to input "sort.in".
select work1-file
assign to disk "sort.work1".
select work2-file
assign to disk "sort.work2".
select index-file
assign to disk "sort.index"
organization is indexed
record key is index-name.
select employee-file-out
assign to output "sort.out".
*
data division.
*
file section.
*
* describe the input file
*
fd index-file
label records standard
block contains 5 records
record contains 30 characters
data record is index-record.
01 index-record.
02 index-name pic x(20).
02 index-rate pic 9(3)v99.
02 index-hours pic 9(3)v99.
*
sd work1-file
data record is work1-record.
01 work1-record.
02 work1-name pic x(20).
02 work1-rate pic 9(3)v99.
02 work1-hours pic 9(3)v99.
*
fd work2-file
label records standard
block contains 5 records
record contains 30 characters
data record is work2-record.
01 work2-record.
02 work2-name pic x(20).
02 work2-rate pic 9(3)v99.
02 work2-hours pic 9(3)v99.
*
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 9(3)v99.
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 54 characters
data record is employee-record-out.
01 employee-record-out.
02 employee-name-out pic x(20).
02 filler pic x(5).
02 employee-rate-out pic 9(3).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(3).99.
02 line-feed-out pic x(1).
working-storage section.
01 control-fields.
02 end-of-file pic x(1) value 'n'.
02 line-feed pic x(1).
01 working-fields.
02 ws-gross-pay pic 9(5)v99 value 0.
02 ws-overtime-pay pic 9(5)v99 value 0.
02 ws-overtime-hours pic 9(5)v99 value 0.
02 ws-overtime-rate pic 9(5)v99 value 0.
procedure division.
*
* main paragraph.
*
a000-control-logic.
*
* section 1
*
perform b100-initialize-processing.
perform b200-process-data
until end-of-file equal to 'y'.
perform b300-terminate-processing.
*
* section 2
*
perform b150-initialize-processing.
perform b250-process-data
until end-of-file equal to 'y'.
perform b350-terminate-processing.
stop run.
*
* open input/output files.
*
b100-initialize-processing.
open input employee-file-in
output work2-file.
perform x100-read-data.
*
b150-initialize-processing.
open input index-file
output employee-file-out.
move 'n' to end-of-file.
perform x150-read-data.
*
* process data.
*
b200-process-data.
perform x200-write-data.
perform x100-read-data.
*
b250-process-data.
perform c100-calc-regular-pay.
if employee-hours-in is greater than 40
perform c200-calc-overtime-pay.
perform x250-write-data.
perform x150-read-data.
*
* close input/output files.
*
b300-terminate-processing.
close employee-file-in
work2-file
perform x300-sort-file.
*
b350-terminate-processing.
close index-file
employee-file-out.
*
* calculate regular pay.
*
c100-calc-regular-pay.
if index-hours is greater than 40
multiply index-rate by 40
giving ws-gross-pay
else
multiply index-rate by index-hours
giving ws-gross-pay.
*
* calculate overtime pay.
*
c200-calc-overtime-pay.
subtract 40 from index-hours
giving ws-overtime-hours.
multiply 1.5 by index-rate
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.
*
* read index file.
*
x150-read-data.
read index-file record
at end move 'y' to end-of-file.
display index-record.
*
* write work2 file.
*
x200-write-data.
move line-feed-in to line-feed.
move employee-name-in to work2-name.
move employee-rate-in to work2-rate.
move employee-hours-in to work2-hours.
display work2-record.
write work2-record.
*
* write output file.
*
x250-write-data.
move index-name to employee-name-out.
move index-rate to employee-rate-out.
move index-hours to employee-hours-out.
move ws-gross-pay to gross-pay-out.
move line-feed to line-feed-out.
display employee-record-out.
write employee-record-out.
*
* sort file.
*
x300-sort-file.
sort work1-file
ascending key work1-name
using work2-file
giving index-file.
*