gpt4 book ai didi

output - 如何在cobol中显示十进制格式输出?

转载 作者:行者123 更新时间:2023-12-02 15:18:10 25 4
gpt4 key购买 nike

我目前正在学习一门古老的编程语言 COBOL,但遇到了一个问题。这是 Cobol 程序的示例。

   IDENTIFICATION DIVISION.
PROGRAM-ID. MONTHLY.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT IN-FILE ASSIGN TO "USERINPUT.DAT".
SELECT OUT-FILE ASSIGN TO "USEROUTPUT.DAT".
DATA DIVISION.
FILE SECTION.
FD IN-FILE
LABEL RECORDS ARE STANDARD
DATA RECORD IS IN-REC.
01 IN-REC.
02 C-NAME PIC X(25).
02 STREET PIC X(20).
02 ZIP-CODE PIC X(15).
02 CREDIT PIC 9(6)V99.
02 MONTH PIC 99.
02 FILLER PIC XX VALUE "\n".
FD OUT-FILE
LABEL RECORDS ARE STANDARD
DATA RECORD IS OUT-REC.
01 OUT-REC PIC X(80).
*-----------------------
WORKING-STORAGE SECTION.
*-----------------------
01 HDG-01.
02 FILLER PIC X(27) VALUE SPACES.
02 FILLER PIC X(27) VALUE "ABC Loans & Savings Company".
01 HDG-02.
02 FILLER PIC X(28) VALUE SPACES.
02 FILLER PIC X(25) VALUE "Ayala Avenue, Makati City".
01 HDG-03.
02 FILLER PIC X(30) VALUE SPACES.
02 FILLER PIC X(20) VALUE "SCHEDULE OF PAYMENTS".
01 HDG-04.
02 FILLER PIC X(28) VALUE SPACES.
02 FILLER PIC X(15) VALUE "ORIGINAL AMOUNT".
02 REC-CREDIT PIC Z(5)9.99.
01 HDG-05.
02 FILLER PIC X(14) VALUE SPACES.
02 FILLER PIC X(9) VALUE "MONTH".
02 FILLER PIC X(11) VALUE "INTEREST".
02 FILLER PIC X(17) VALUE "TOTAL-PAYMENT".
02 FILLER PIC X(14) VALUE "UNPAID-BALANCE".
01 TRANSFER-LINE.
02 FILLER PIC X(16) VALUE SPACES.
02 REC-MONTH PIC 99.
02 FILLER PIC X(6) VALUE SPACES.
02 INTEREST PIC 9(3)V99.
02 FILLER PIC X(7) VALUE SPACES.
02 TOTAL-PAY PIC 9(6)v99.
02 FILLER PIC X(10) VALUE SPACES.
02 UNPAID-BAL PIC 9(6)v99.
01 PRINT-LINE.
02 FILLER PIC X(16) VALUE SPACES.
02 FILLER PIC 99.
02 FILLER PIC X(6) VALUE SPACES.
02 FILLER PIC ZZ9.99.
02 FILLER PIC X(7) VALUE SPACES.
02 FILLER PIC Z(5)9.99.
02 FILLER PIC X(10) VALUE SPACES.
02 FILLER PIC Z(5)9.99.
01 PRINT-NULL.
02 FILLER PIC X(16) VALUE SPACES.
02 FILLER PIC XX VALUE "--".
02 FILLER PIC X(6) VALUE SPACES.
02 FILLER PIC XXXXX VALUE "-----".
02 FILLER PIC X(7) VALUE SPACES.
02 FILLER PIC X(9) VALUE "---------".
02 FILLER PIC X(9) VALUE SPACES.
02 FILLER PIC X(9) VALUE "---------".
01 X PIC 99.
01 REM PIC 999.
01 CHECK-MONTH PIC 99.
01 CLIENT-NO PIC 9.
01 PRINT-ASTERISK.
02 FILLER PIC X(30) VALUES ALL "*" .
02 FILLER PIC X(18) VALUES "-END OF CLIENT NO ".
02 CLIENT PIC 9.
02 FILLER PIC X VALUE "-".
02 FILLER PIC X(30) VALUES ALL "*" .
PROCEDURE DIVISION.

OPEN INPUT IN-FILE
OUTPUT OUT-FILE.

REPEAT-RTN.
ADD 1 TO CLIENT-NO.
MOVE CLIENT-NO TO CLIENT.
READ IN-FILE AT END PERFORM CLOSE-RTN.
MOVE CREDIT TO UNPAID-BAL.
MOVE MONTH TO CHECK-MONTH.
PERFORM WITH TEST BEFORE UNTIL CHECK-MONTH < 13
COMPUTE CHECK-MONTH = CHECK-MONTH - 12
END-PERFORM.
COMPUTE CHECK-MONTH = MONTH + (12 - CHECK-MONTH).
MOVE ZEROES TO X.
PERFORM PROCESS-RTN CHECK-MONTH TIMES.
WRITE OUT-REC FROM PRINT-ASTERISK AFTER 1 LINE.
PERFORM REPEAT-RTN.

PROCESS-RTN.
ADD 1 TO X.
MOVE X TO REM.

PERFORM WITH TEST BEFORE UNTIL REM <= 13
COMPUTE REM = REM - 12
END-PERFORM.

IF REM=13 OR REM = 1 THEN
PERFORM HDG-RTN
END-IF.
IF REM=13 THEN
MOVE SPACES TO OUT-REC
WRITE OUT-REC.
MOVE X TO REC-MONTH.
COMPUTE INTEREST = UNPAID-BAL * 0.015.
COMPUTE TOTAL-PAY ROUNDED= CREDIT / MONTH + INTEREST.
COMPUTE UNPAID-BAL = UNPAID-BAL - TOTAL-PAY + INTEREST.

IF UNPAID-BAL < 1 THEN
MOVE ZEROES TO UNPAID-BAL
END-IF.

IF X > MONTH THEN
WRITE OUT-REC FROM PRINT-NULL AFTER 1 LINE
ELSE
MOVE TRANSFER-LINE TO PRINT-LINE
WRITE OUT-REC FROM PRINT-LINE AFTER 1 LINE
END-IF.

HDG-RTN.
IF X > 1 THEN
WRITE OUT-REC FROM HDG-01 AFTER 2 LINE
WRITE OUT-REC FROM HDG-02 AFTER 1 LINE
WRITE OUT-REC FROM C-NAME AFTER 2 LINE
ELSE IF CLIENT-NO > 1 THEN
WRITE OUT-REC FROM HDG-01 AFTER 1 LINE
WRITE OUT-REC FROM HDG-02 AFTER 1 LINE
WRITE OUT-REC FROM C-NAME AFTER 2 LINE
ELSE
WRITE OUT-REC FROM HDG-01 BEFORE 1 LINE
WRITE OUT-REC FROM HDG-02 BEFORE 1 LINE
WRITE OUT-REC FROM C-NAME AFTER 1 LINE
END-IF.
WRITE OUT-REC FROM STREET AFTER 1 LINE.
WRITE OUT-REC FROM ZIP-CODE AFTER 1 LINES.
WRITE OUT-REC FROM HDG-03 AFTER 2 LINE.
MOVE CREDIT TO REC-CREDIT.
WRITE OUT-REC FROM HDG-04 AFTER 1 LINE.
WRITE OUT-REC FROM HDG-05 AFTER 2 LINE.

CLOSE-RTN.
CLOSE IN-FILE , OUT-FILE.
STOP RUN.

END PROGRAM MONTHLY.

程序假设产生这样的输出:

                      ABC Loans & Savings Company                          
Ayala Avenue, Makati City

The Client Name is Here:
The Client Address:
The ZiP/CITY:

SCHEDULE OF PAYMENTS
ORIGINAL AMOUNT 4291.50

MONTH INTEREST TOTAL-PAYMENT UNPAID-BALANCE
01 64.37 422.00 3933.87
02 05900 416.63 3576.24
03 05364 411.27 3218.61
04 04827 405.90 2860.98
05 04291 400.54 2503.35
06 03755 395.18 2145.72
07 03218 389.81 1788.09
08 02682 384.45 1430.46
09 02145 379.08 1072.83
10 01609 373.72 715.20
11 01072 368.35 357.57
12 00536 362.99 0.00
******************************-END OF CLIENT NO 1-******************************

但是我运行程序时的程序输出是不一样的。它看起来像这样:

                      ABC Loans & Savings Company                          
Ayala Avenue, Makati City

The Client Name is Here:
The Client Address:
The ZiP/CITY:

SCHEDULE OF PAYMENTS
ORIGINAL AMOUNT 4291.50

MONTH INTEREST TOTAL-PAYMENT UNPAID-BALANCE
01 06437 00042200 00393387
02 05900 00041663 00357624
03 05364 00041127 00321861
04 04827 00040590 00286098
05 04291 00040054 00250335
06 03755 00039518 00214572
07 03218 00038981 00178809
08 02682 00038445 00143046
09 02145 00037908 00107283
10 01609 00037372 00071520
11 01072 00036835 00035757
12 00536 00036299 00000000
******************************-END OF CLIENT NO 1-******************************

我遇到了关于小数格式和消零的问题。有什么建议吗?顺便说一句,我只是使用 DAT 文件进行输入,所以我没有使用任何 ACCEPT 或输入函数,它包含记录所需的确切字符,如下文所示:

客户名称在这里:客户地址:ZiP/CITY:0042915012

我认为问题在于工作存储打印线和传输线。

最佳答案

乍一看,我注意到您的代码有两个问题:

  1. PRINT-LINETRANSFER-LINE 的长度不同,因为数字项的 PICTURE 子句不同.例如,PRINT-LINE 使用 PIC ZZ9.99TRANSFER-LINE 使用 PIC 9(3)V99。请注意,PICTURE 子句中的小数点 . 需要一个字节的存储空间,而 V 根本不占用任何存储空间,因此PRINT-LINE中的字段比TRANSFER-LINE中的字段大一个字节。
  2. 当您执行 MOVE TRANSFER-LINE TO PRINT-LINE 时,您不会进行逐字段传输,而是将整个数据 block 作为一个数据 block 移动,因此 PICTURE - PRINT-LINE 中的子句被完全忽略。你不会遇到更大的麻烦,因为 PRINT-LINETRANSFER-LINE 占用更多的存储空间(见 1.)——如果反过来你甚至可能有存储覆盖问题(但至少应该得到编译器警告)。

要做到这一点,您可以

  • PRINT-LINE 的 level-02-items 命名为 TRANSFER-LINE 的项目,然后执行 MOVE CORRESPONDING TRANSFER-LINE TO PRINT-LINE - 但随后您必须使用限定名称来处理您的字段(INTEREST OF TRANSFER-LINE)

  • 摆脱 TRANSFER-LINE 并将您的数据直接放入 PRINT-LINE

关于output - 如何在cobol中显示十进制格式输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39119422/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com