CHAPTER 7 各部之间的关系及程序举例
二.程序举例:
例1:
读入一组产品销售记录,每读入一个记录,计算出销售总额(数量X单价).然后打印出该产品的全部数据.数据形式如下:
1 9 9 2 1 2 3 1 0 0 0 0 0 1 A A A A 0 1 0 0 5 0 0 0
1 9 9 3 1 2 3 1 1 0 0 0 0 1 B B B B 0 2 0 0 6 0 0 0
------------------------- ----------------- ------------ ------------ -----------
DATE PRODCODE CUST AMT PRICE
程序如下:
IDENTIFICATION DIVISION.
PROGRAM-ID. PEXAM1
ENVIRONMENT DIVISION.
CONFIGRATION SECTION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT INPUT-FILE ASSIGN TO FILE1
ORGANIZATION IS SEQUENCIAL
ACCESS MODE IS SEQUENCIAL
FILE-STATUS IS WS-INPUT-FILE-STATUS.
SELECT OUTPUT-FILE ASSIGN TO P-FILE
ORGANIZATION IS SEQUENCIAL
ACCESS MODE IS SEQUENCIAL
FILE-STATUS IS WS-OUTPUT-FILE-STATUS.
DATA DIVISION.
FILLE SECTION.
FD INPUT-FILE LABEL RECORD IS STANDARD.
01 IN-REC.
05 DATE PIC 9(8).
05 PROD-CODE PIC 9(6).
05 CUSTOMER-CODE PIC X(4).
05 QUANTITY PIC 9(4).
05 UNIT-PRICE PIC 9(4).
FD OUTPUT-FILE LABEL RECORD IS STANDARD.
01 OUT-REC.
05 DATE PIC 9999B99B99.
05 FILLER PIC X(5).
05 PROD-CODE PIC 9(6).
05 FILLER PIC X(5).
05 CUSTOMER-CODE PIC X(4).
05 FILLER PIC X(5).
05 QUANTITY PIC ZZZ9.
05 FILLER PIC X(5).
05 UNIT-PRICE PIC $(5).
05 FILLER PIC X(5).
05 SALES-VALUE PIC $(8).
WORKING-STORAGE SECTION.
01 WS-VARIBLES.
05 WS-INPUT-FILE-STATUS PIC 9(2).
88 WS-INPUT-FILE-SUCCESSFUL VALUE 0.
88 WS-INPUT-FILE-EOF VALUE 23.
05 WS-OUTPUT-FILE-STATUS PIC 9(2).
88 WS-OUTPUT-FILE-SUCCESSFUL VALUE 0.
PROCEDURE DIVISION.
A000-MAIN.
PERFORM A100-OPEN-FILES
PERFORM R000-READ-FILE
PERFORM A200-PROCESS-RECORD UNTIL WS-INPUT-FILE-EOF
PERFORM A300-CLOSE-FILES
STOP RUN
.
A000-EXIT.
EXIT
.
A100-OPEN-FILES.
OPEN INPUT INPUT-FILE
OUTPUT OUTPUT-FILE
IF NOT WS-INPUT-FILE-SUCCESSFUL
DISPLAY ‘INPUT-FILE OPEN NOT SUCCESSFUL’ UPON CONSOLE
PERFORM Z000-ABEND
END-IF
IF NOT WS-OUTPUT-FILE-SUCCESSFUL
DISPLAY ‘OUTPUT-FILE OPEN NOT SUCCESSFUL’ UPON CONSOLE
PERFORM Z000-ABEND
END-IF
.
A200-PROCESS-RECORD.
INITIALIZE OUT-REC
MOVE CORR IN-REC TO OUT-REC
COMPUTE SALES-VALUE = QUANTITY OF IN-REC * UNIT-PRICE OF OUT-REC
PERFORM W000-WRITE-FILE
PERFORM R000-READ-FILE
.
A300-CLOSE-FILE.
CLOSE INPUT-FILE
OUTPUT-FILE
.
R000-READ-FILE.
READ INPUT-FILE
IF NOT WS-INPUT-FILE-SUCCESSFUL AND NOT WS-INPUT-FILE-EOF
DISPLAY ‘INPUT-FILE READ NOT SUCCESSFUL’ UPON CONSOLE
PERFORM Z000-ABEND THRU Z000-EXIT
END-IF
.
W000-WRITE-FILE.
WRITE OUT-REC AFTER 1
IF NOT WS-OUTPUT-FILE-SUCCESSFUL
DISPLAY ‘OUTPUT-FILE WRITE NOT SUCCESSFUL’ UPON CONSOLE
PERFORM Z000-ABEND
END-IF
.
Z000-ABEND.
DISPLAY ‘PROGRAM ABEND !’ UPON CONSOLE
PERFORM A300-CLOSE-FILES THRU A300-EXIT
MOVE 16 TO RETURN-CODE
GOBACK
.
输出的数据形式如下:
1992 12 31 000001 AAAA 100 $5000 $500000
1993 12 31 100001 BBBB 200 $6000 $1200000
例2:
Program : INVI300 Update inventory file (random)
File | Description | Use |
VALTRAN | Valid inventory transaction file | Input |
INVMAST | Inventory master file | Update |
ERRTRAN | Unmatched inventory transaction file | Output |
Process specifications
This program updates an inventory master file (INVMAST) based on the data in a sequential
file of valid inventory transaction records (VALTRAN). The inventory master file is indexed by item number and updated randomly.
If the program finds a master record with the same item number as a transaction, it uses the
transaction data to update the master record .It does this by increasing the on hand quantity
in the master record by the receipt quantity in the transaction record.
If the program cannot find a master record for a transaction, it writes the transaction record on
the file of error transactions (ERRTRAN). The record format format for ERRTRAN is same as
for VALTRAN.
The basic processing requirements are:
1. Read a transaction record.
2. Read the master record with the same item number as in the transaction record.
3. If the master record is found , update and rewrite the matching master record.
4. If the transaction is not found , write the transaction record on the file of error transactions.
程序如下:
IDENTIFICATION DIVISION.
PROGRAM-ID. INV1300.
ENVIRONENT DIVISON.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT VALTRAN ASSIGN TO SYS020-AS-VALTRAN.
SELECT INVMAST ASSIGN TO SYS021-INVMAT
ORGANIZATION IS INDEXED
ACCESS IS RANDOM
RECORD KEY IS MR-ITEM-NO
FILE STATUS IS INVMAST-ERROR-CODE.
SELECT ERRTRAN ASSIGN TO SYS022-AS-ERRTRAN.
DATA DIVISION.
FILE SECTION.
FD VALTRAN
LABEL RECORDS ARE STANDARD.
01 VALID-TRANSACTION-AREA PIC X(21).
FD INVMAST
LABEL RECORDS ARE STANDARD.
01 MASTER-RECORD-AREA.
05 MR-ITEM-NO PIC X(5).
05 FILLER PIC X(45).
FD ERRTRAN
LABEL RECORDS ARE STANDARD.
01 ERROR-TRANSACTION PIC X(21).
WORKING-STORAGE SECTION.
01 SWITCHES.
05 VALTRAN-EOF-SWITCH PIC X VALUE ‘N’.
88 VALTRAN-EOF VALUE ‘Y’.
05 MASTER-FOUND-SWITCH PIC X.
88 MASTER-FOUND VALUE ‘Y’.
01 FILE-STATUS-FIELD.
05 INVMAST-ERROR-CODE PIC X(2).
01 INVENTROY-TRANSACTION-RECORD.
05 IT-ITEM-NO PIC X(5).
05 IT-VENOR-NO PIC X(5).
05 IT-RECEIPT-DATE PIC X(6).
05 IT-RECEIPT-QUANTITY PIC S9(5).
01 INVENTORY-MASTER-RECORD.
05 IM-DESCRIPTIVE-DATA.
10 IM-ITEM-NO PIC X(5).
10 IM-ITEM-DESC PIC X(20).
10 IM-UNIT-COST PIC S9(3)V9(2).
10 IM-UNIT-PRICE PIC S9(3)V9(2).
05 IM-INVENTORY-DATA.
10 IM-REORDER-POINT PIC S9(5).
10 IM-ON-HAND PIC S9(5).
10 IM-ON-ORDER PIC S9(5).
PROCEDURE DIVISION.
A000-UPDATE-INVENTORY-FILE.
OPEN INPUT VALTRAN
I-O INVMAST
OUTPUT ERRTRAN
PERFORM B000-PROCESS-INVENTORY-TRAN THRU B000-EXIT
UNTIL VALTRAN-EOF
CLOSE VALTRAN
INVMAST
ERRTRAN
DISPLAY ‘PROGRAM INV1300 NORMAL END’
GOBACK.
B000-PROCESS-INVENTORY-TRAN.
PERFORM B100-READ-INVENTORY-TRAN
IF NOT VALTRAN-EOF
PERFORM B200-READ-INVENTORY-MASTER
IF MASTER-FOUND
PERFORM B300-UPDATE-INVENTORY-MASTER
ELSE
PERFORM B400-WRITE-ERROR-TRAN
END-IF
END-IF.
B000-EXIT.
EXIT.
B100-READ-INVENTRORY-TRAN.
READ VALTRAN INTO INVENTORY-TRANSACTION-RECORD
AT END MOVE ‘Y’ TO VALTRAN-EOF-SWITCH.
B100-EXIT.
EXIT.
B200-READ-INVENTORY-MASTER.
MOVE IT-ITEM-NO TO MR-ITEM-NO
READ INVMASST INTO INVENTORY-MASTER-RECORD
IF INVMAST-ERROR-CODE = ‘00’
MOVE ‘Y’ TO MASTER-FOUND-SWITCH
ELSE
MOVE ‘N’ TO MASTER-FOUND-SWITCH
END-IF.
B300-UPDATE-INVENTORY-MASTER.
ADD IT-RECEIPT-QUANTITY TO IM-ON-HAND
REWRITE MASTER-RECORD-AREA FROM INVENTORY-MASTER-RECORD
IF INVMAST-ERROR-CODE NOT = ‘00’
DISPLAY ‘ INV1300 REWRITE INVMAST ERROR . ITEM NUMBER = ’ IM-ITEM-NO
DISPLAY ‘ FILE STATUS = ’ INVMAST-ERROR-CODE
MOVE ‘Y’ TO VALTRAN-EOF-SWITH
END-IF.
B300-EXIT.
EXIT.
B400-WRITE-ERROR-TRAN.
WRITE ERROR-TRANSACTION FROM INVENTORY-TRANSACTION-RECORD.
B400-EXIT.
EXIT.
IDENTIFICATION DIVISION.
PROGRAM-ID. PROGRAM3.
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-RESP PIC S9(8) COMP.
88 WS-RESP-NORMAL VALUE 0.
01 PGM3COM.
05 CUST-NO PIC 9(8).
05 PGM3-OUT.
10 PGM3-RETURN-CODE PIC X(2).
88 PGM3-SUCCESSFUL VALUE ' '.
88 PGM3-CUST-NBR-ERR VALUE '01'.
88 PGM3-CUST-NOT-FND VALUE '02'.
10 PGM3-AC PIC 9(5).
10 PGM3-BAL PIC 9(11).
01 MASTER-RECORD.
05 MASTER-KEY PIC 9(8).
05 CUST-NAME PIC X(20).
05 CHECK-AC PIC 9(5).
05 BAL PIC 9(11).
LINKAGE SECTION.
01 DFHCOMMAREA PIC X(26).
PROCEDURE DIVISION.
A000-MAIN-PROCESS.
MOVE DFHCOMMAREA TO PGM3COM
INITIALIZE PGM3-OUT
PERFORM B000-VALIDATION THRU B000-EXIT
IF PGM3-SUCCESSFUL
PERFORM C000-PROCESS THRU C000-EXIT
END-IF
.
A999-RETURN.
MOVE PGM3COM TO DFHCOMMAREA.
EXEC CICS RETURN END-EXEC.
GOBACK.
*
B000-VALIDATION.
IF CUST-NO NOT NUMERIC
SET PGM3-CUST-NBR-ERR TO TRUE
END-IF
.
B000-EXIT.
EXIT.
*
C000-PROCESS.
MOVE CUST-NO TO MASTER-KEY
EXEC CICS READ FILE ('VMASTER')
RIDFLD(MASTER-KEY)
INTO (MASTER-RECORD)
LENGTH(LENGTH OF MASTER-RECORD)
RESP (WS-RESP)
END-EXEC
IF WS-RESP NOT = DFHRESP(NORMAL)
IF WS-RESP = DFHRESP(NOTFND)
SET PGM3-CUST-NOT-FND TO TRUE
ELSE
EXEC CICS ABEND ABCODE('ABCD') END-EXEC
END-IF
ELSE
MOVE CHECK-AC TO PGM3-AC
MOVE BAL TO PGM3-BAL
END-IF
.
C000-EXIT.
EXIT.