gpt4 book ai didi

cobol - 一个奇怪的错误 (COBOL)

转载 作者:行者123 更新时间:2023-12-04 12:24:45 26 4
gpt4 key购买 nike

大家好,这里有一大堆问题。我已经完成了一个我必须为大学作业做的程序,但是当我运行它时,输出几乎没有显示它应该显示的内容。这只发生在我运行它时。如果我在整个过程中按住 F11 到 STEP,它会显示预期的结果。通常我不会问这么大的事情,但我很难过。这是我的代码:

   ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.

SELECT SALESAMT-FILE-IN
ASSIGN TO 'SALESAMT.SEQ'
ORGANIZATION IS LINE SEQUENTIAL.

SELECT SALESMAN-FILE-IN
ASSIGN TO 'SALESMAN.SEQ'
ORGANIZATION IS LINE SEQUENTIAL.

SELECT SALESQTR-FILE-IN
ASSIGN TO 'SALESQTR.SEQ'
ORGANIZATION IS LINE SEQUENTIAL.

SELECT SALESAMT-FILE-OUT
ASSIGN TO 'SALESAMT.RPT'
ORGANIZATION IS LINE SEQUENTIAL.

DATA DIVISION.
FILE SECTION.

FD SALESMAN-FILE-IN.
01 SALESMAN-RECORD-IN.
05 SM-NUMBER-IN PIC 99.
05 SM-NAME-IN PIC X(20).

FD SALESQTR-FILE-IN.
01 SALESQTR-RECORD-IN.
05 QUARTER-YEAR PIC X.

FD SALESAMT-FILE-IN.
01 SALESAMT-RECORD-IN.
05 SM-NUMBER PIC 99.
05 PIC X.
05 MONTH-NUMBER PIC 9.
05 PIC X.
05 SALES-AMOUNT PIC 9(5).

FD SALESAMT-FILE-OUT.
01 SALESAMT-RECORD-OUT PIC X(80).

WORKING-STORAGE SECTION.
01 ARE-THERE-MORE-RECORDS PIC X(3) VALUE 'YES'.

01 REPORT-START PIC X VALUE 'Y'.

01 LINE-COUNT PIC 99 VALUE ZEROS.

01 LINE-JUMP PIC X VALUE 'Y'.

01 PAGE-NUMBER PIC 99 VALUE ZEROS.

01 QUARTER-CHECK PIC X.

01 ROUTINE-CHECK PIC 99 VALUE ZEROS.

01 SALESMAN-MATH PIC 9(5) VALUE ZEROS.

01 SALESMAN-TOTAL PIC 9(6) VALUE ZEROS.

01 FINAL-M-TOTAL-1 PIC 9(7) VALUE ZEROS.

01 FINAL-M-TOTAL-3 PIC 9(7) VALUE ZEROS.

01 FINAL-M-TOTAL-2 PIC 9(7) VALUE ZEROS.

01 FINAL-TOTAL PIC 9(7) VALUE ZEROS.

01 SM-NUM-M PIC 99 VALUE ZEROS.

01 MORE-TABLE-RECS PIC X VALUE 'Y'.

01 SPACE-LINE PIC X VALUE SPACE.

01 MONTH-NAMES
VALUE 'JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC'.
05 MONTH-TITLES OCCURS 12 TIMES PIC X(3).

01 MONTH-ARRAY.
05 THREE-MONTHS OCCURS 3 TIMES.
10 MONTH-TOTAL OCCURS 99 TIMES PIC 9(7) VALUE ZEROS.

01 SALESMAN-TABLE.
05 TABLE-ENTRIES OCCURS 99 TIMES
INDEXED BY IND-TABLE-ENTRIES.
10 SALESMAN-NUMBER PIC 99 VALUE ZEROS.
10 SALESMAN-NAME PIC X(20) VALUE SPACES.
01 SALESMAN-COUNT PIC 9(3) VALUE ZEROS.

01 WS-DATE.
05 RUN-YEAR PIC XX.
05 RUN-MONTH PIC XX.
05 RUN-DAY PIC XX.

01 HEADING-LINE-1.
05 PIC X(17) VALUE SPACES.
05 PIC X(35)
VALUE 'SALES AMOUNTS BY SALESMAN AND MONTH'.
05 PIC X(10) VALUE SPACES.
05 HL-1-DATE.
10 MONTH-2 PIC XX.
10 PIC X VALUE '/'.
10 DAY-2 PIC XX.
10 PIC X VALUE '/'.
10 YEAR-2 PIC XX.
05 PIC X(3) VALUE SPACES.
05 PAGE-1 PIC X(4) VALUE 'PAGE'.
05 PIC X(1) VALUE SPACES.
05 NUMBER-PAGE PIC Z9.

01 HEADING-LINE-2.
05 HL-NUM PIC X(3) VALUE 'NUM'.
05 HL-BLANK-A PIC XX VALUE SPACES.
05 HL-NAME PIC X(4) VALUE 'NAME'.
05 HL-BLANK-B PIC X(20) VALUE SPACES.
05 HL-MONTH-1 PIC X(3) VALUE SPACES.
05 HL-BLANK-C PIC X(8) VALUE SPACES.
05 HL-MONTH-2 PIC X(3) VALUE SPACES.
05 HL-BLANK-D PIC X(8) VALUE SPACES.
05 HL-MONTH-3 PIC X(3) VALUE SPACES.
05 HL-BLANK-E PIC X(10) VALUE SPACES.
05 HL-TOTAL PIC X(5) VALUE 'TOTAL'.

01 DETAIL-LINE.
05 DL-BLANK-A PIC X VALUE SPACES.
05 DL-NUM-COLUMN PIC 99.
05 DL-BLANK-B PIC XX VALUE SPACES.
05 DL-NAME-COLUMN PIC X(17).
05 DL-BLANK-C PIC X(4) VALUE SPACES.
05 DL-MONTH-1 PIC ZZ,Z(3).
05 DL-BLANK-D PIC X(5) VALUE SPACES.
05 DL-MONTH-2 PIC ZZ,Z(3).
05 DL-BLANK-E PIC X(5) VALUE SPACES.
05 DL-MONTH-3 PIC ZZ,Z(3).
05 DL-BLANK-F PIC X(8) VALUE SPACES.
05 DL-TOTAL PIC Z(3),Z(3).

01 TOTALS-LINE.
05 TL-WORDS PIC X(12)
VALUE 'Final Totals'.
05 TL-BLANK-A PIC X(12) VALUE SPACES.
05 MONTH-1-TOTAL PIC Z,Z(3),Z(3).
05 TL-BLANK-A PIC X(2) VALUE SPACES.
05 MONTH-2-TOTAL PIC Z,Z(3),Z(3).
05 TL-BLANK-A PIC X(2) VALUE SPACES.
05 MONTH-3-TOTAL PIC Z,Z(3),Z(3).
05 TL-BLANK-A PIC X(5) VALUE SPACES.
05 MONTH-FINAL-TOTAL PIC Z,Z(3),Z(3).

PROCEDURE DIVISION.
100-MAIN.
OPEN INPUT SALESAMT-FILE-IN, SALESMAN-FILE-IN,
SALESQTR-FILE-IN
OPEN OUTPUT SALESAMT-FILE-OUT

ACCEPT WS-DATE FROM DATE
MOVE RUN-MONTH TO MONTH-2
MOVE RUN-DAY TO DAY-2
MOVE RUN-YEAR TO YEAR-2

PERFORM 200-NEXT-PAGE

PERFORM 300-SALES-ARRAY

PERFORM 400-SALESMAN-NAME

PERFORM 500-PROCESS-FILE

PERFORM 600-FINAL-TOTALS

CLOSE SALESAMT-FILE-IN, SALESMAN-FILE-IN, SALESQTR-FILE-IN
CLOSE SALESAMT-FILE-OUT
STOP RUN.

200-NEXT-PAGE.
ADD 1 TO PAGE-NUMBER
MOVE PAGE-NUMBER TO NUMBER-PAGE
MOVE HEADING-LINE-1 TO SALESAMT-RECORD-OUT
IF REPORT-START = 'N'
WRITE SALESAMT-RECORD-OUT
AFTER ADVANCING PAGE
ELSE
MOVE 'N' TO REPORT-START
WRITE SALESAMT-RECORD-OUT
AFTER ADVANCING 1 LINE
PERFORM 210-MONTH-CHECK
END-IF.
MOVE HEADING-LINE-2 TO SALESAMT-RECORD-OUT
WRITE SALESAMT-RECORD-OUT
AFTER ADVANCING 2 LINES
MOVE ZEROS TO LINE-COUNT.

210-MONTH-CHECK.
READ SALESQTR-FILE-IN
AT END
CONTINUE
NOT AT END
PERFORM 220-MONTH-NAME
END-READ.

220-MONTH-NAME.
EVALUATE QUARTER-YEAR
WHEN = 1 MOVE MONTH-TITLES(1) TO HL-MONTH-1
MOVE MONTH-TITLES(2) TO HL-MONTH-2
MOVE MONTH-TITLES(3) TO HL-MONTH-3

WHEN = 2 MOVE MONTH-TITLES(4) TO HL-MONTH-1
MOVE MONTH-TITLES(5) TO HL-MONTH-2
MOVE MONTH-TITLES(6) TO HL-MONTH-3

WHEN = 3 MOVE MONTH-TITLES(7) TO HL-MONTH-1
MOVE MONTH-TITLES(8) TO HL-MONTH-2
MOVE MONTH-TITLES(9) TO HL-MONTH-3

WHEN = 4 MOVE MONTH-TITLES(10) TO HL-MONTH-1
MOVE MONTH-TITLES(11) TO HL-MONTH-2
MOVE MONTH-TITLES(12) TO HL-MONTH-3
END-EVALUATE.


300-SALES-ARRAY.
PERFORM UNTIL ARE-THERE-MORE-RECORDS = 'NO '
READ SALESAMT-FILE-IN
AT END
MOVE 'NO ' TO ARE-THERE-MORE-RECORDS
NOT AT END
PERFORM 310-STORE-DATA
END-READ
END-PERFORM.

310-STORE-DATA.
MOVE SM-NUMBER TO SM-NUM-M
EVALUATE MONTH-NUMBER
WHEN 1 PERFORM 320-FIRST-MONTH

WHEN 2 PERFORM 330-SECOND-MONTH

WHEN 3 PERFORM 340-THIRD-MONTH

END-EVALUATE.

320-FIRST-MONTH.
ADD SALES-AMOUNT TO
MONTH-TOTAL OF MONTH-ARRAY (1, SM-NUM-M).

330-SECOND-MONTH.
ADD SALES-AMOUNT TO
MONTH-TOTAL OF MONTH-ARRAY (2, SM-NUM-M).

340-THIRD-MONTH.
ADD SALES-AMOUNT TO
MONTH-TOTAL OF MONTH-ARRAY (3, SM-NUM-M).

400-SALESMAN-NAME.
PERFORM UNTIL MORE-TABLE-RECS = 'N'
READ SALESMAN-FILE-IN
AT END
MOVE 'N' TO MORE-TABLE-RECS
NOT AT END
PERFORM 450-TABLE-LOAD
END-READ
END-PERFORM.

450-TABLE-LOAD.
MOVE SM-NUMBER-IN TO SALESMAN-COUNT
MOVE SM-NUMBER-IN TO SALESMAN-NUMBER (SALESMAN-COUNT)
MOVE SM-NAME-IN TO SALESMAN-NAME (SALESMAN-COUNT).

500-PROCESS-FILE.
PERFORM UNTIL ROUTINE-CHECK = 99
ADD 1 TO ROUTINE-CHECK
PERFORM 510-TABLE-SEARCH
END-PERFORM.

510-TABLE-SEARCH.
SEARCH TABLE-ENTRIES
WHEN SALESMAN-NUMBER (ROUTINE-CHECK) = ROUTINE-CHECK
PERFORM 520-WRITE-FILE
WHEN SALESMAN-NUMBER (ROUTINE-CHECK) = 0
CONTINUE
END-SEARCH.

520-WRITE-FILE.
MOVE SALESMAN-NAME (ROUTINE-CHECK) TO DL-NAME-COLUMN
IF DL-NAME-COLUMN = SPACES
MOVE '*** Not Found ***' TO DL-NAME-COLUMN
END-IF
MOVE ROUTINE-CHECK TO DL-NUM-COLUMN
MOVE ROUTINE-CHECK TO SM-NUM-M
MOVE MONTH-TOTAL (1, SM-NUM-M) TO DL-MONTH-1
MOVE DL-MONTH-1 TO SALESMAN-MATH
ADD SALESMAN-MATH TO SALESMAN-TOTAL
ADD SALESMAN-MATH TO FINAL-M-TOTAL-1
ADD SALESMAN-MATH TO FINAL-TOTAL
MOVE MONTH-TOTAL (2, SM-NUM-M) TO DL-MONTH-2
MOVE DL-MONTH-2 TO SALESMAN-MATH
ADD SALESMAN-MATH TO SALESMAN-TOTAL
ADD SALESMAN-MATH TO FINAL-M-TOTAL-2
ADD SALESMAN-MATH TO FINAL-TOTAL
MOVE MONTH-TOTAL (3, SM-NUM-M) TO DL-MONTH-3
MOVE DL-MONTH-3 TO SALESMAN-MATH
ADD SALESMAN-MATH TO SALESMAN-TOTAL
ADD SALESMAN-MATH TO FINAL-M-TOTAL-3
ADD SALESMAN-MATH TO FINAL-TOTAL
IF SALESMAN-TOTAL > 0
MOVE SALESMAN-TOTAL TO DL-TOTAL
MOVE DETAIL-LINE TO SALESAMT-RECORD-OUT
WRITE SALESAMT-RECORD-OUT
AFTER ADVANCING 2 LINES
END-IF
MOVE ZEROS TO SALESMAN-TOTAL.

600-FINAL-TOTALS.
MOVE FINAL-M-TOTAL-1 TO MONTH-1-TOTAL
MOVE FINAL-M-TOTAL-2 TO MONTH-2-TOTAL
MOVE FINAL-M-TOTAL-3 TO MONTH-3-TOTAL
MOVE FINAL-TOTAL TO MONTH-FINAL-TOTAL
MOVE TOTALS-LINE TO SALESAMT-RECORD-OUT
WRITE SALESAMT-RECORD-OUT
AFTER ADVANCING 3 LINES.

对我来说,逻辑似乎是正确的,因为它确实有效,但出于某种原因,它(在我看到结果时在我看来)在运行时完全跳过了 520-WRITE-FILE。有了这个,我确实留下了一些笔记。
  • 我知道 510-TABLE-SEARCH 没有什么意义,我打算稍后更改它,但我需要先解决这个问题,目前它可以工作。除非它是主要问题,否则请不要骚扰我。
  • 如果有人问我,我愿意在 SEQ 文件中添加数据。
  • 我的代码可能有点复杂,我承认这一点,但我正在与我的老师一起尽我所能(我主要必须自己学习这些东西)。

  • 我感谢我收到的任何帮助,并感谢任何试图提前提供帮助的人。

    编辑:我使用的是名为 Micro Focus、Net Express 5.1 Academic Edition 的编译器,我的操作系统是 Windows Vista。至于程序在我运行时显示的内容,它只显示我的两个标题行,然后是我的总计行,除了第一个字段显示之外没有任何其他内容。我希望这有帮助。

    最佳答案

    我不确定这是否是问题所在,但我可以看到一个逻辑流程不会很好地工作......

    第一:400-SALESMAN-NAME将文件中的销售员记录读入工作存储表 SALESMAN-TABLE .

    该文件可能类似于:

    01Sales Guy One   
    02Lance Winslow
    03Scott Peterson
    04Willy Loman

    读取循环完成后, SALESMAN-NUMBER将等于表索引,因为
    加载表格的方式(使用 SM-NUMBER-IN 设置表格下标)。目前没问题...

    下一个: 500-PROCESS-FILE循环遍历 SALESMAN-TABLE 中的所有行通过运行下标 ROUTINE-CHECK从 1 到 99 并执行 510-TABLE-SEARCH为下标等于 SALESMAN-NUMBER的推销员写出报告...

    下一篇: SEARCH陈述。这就是一切都变得奇怪并且从不执​​行的地方 520-WRITE-FILE .
    这就是为什么。
    SEARCH语句实现线性搜索( SEARCH ALL 是二分搜索)。 SEARCH只是增加与搜索表关联的索引,然后运行一堆 WHEN测试直到其中一个“触发”或索引从表的末尾运行。您的索引 TABLE-ENTRIES表是 IND-TABLE-ENTRIES .但是你从来没有设置或引用它(这是问题的根源)。一会我会解释...

    请注意 WHEN您的 SEARCH 的一部分正在使用下标 ROUTINE-CHECK . ROUTINE-CHECK设置在 500-PROCESS-FILE .另请注意,您只能访问 520-WRITE-FILE如果 SALESMAN-NUMBER匹配 ROUTINE-CHECK 的值- 如果从输入文件中读取了具有该编号的销售员,则会执行此操作。这可能会起作用,因为您加载了表,使得行号等于 450-TABLE-LOAD 中的销售员编号。 .

    现在,如果输入文件不包含销售员 where SM-NUMBER-IN 会发生什么?等于01?

    让我们通过它,一击接一击……
    ROUTINE-CHECK设置为 1, SEARCH被调用,因为 IND-TABLE-ENTRIES与搜索表关联的索引小于表中出现的次数(它在程序加载时被初始化为零), WHEN条款被执行。

    第一个测试是 WHEN SALESMAN-NUMBER (ROUTINE-CHECK) = ROUTINE-CHECK .由于销售员 1 不存在, SALESMAN-NUMBER将为零并且测试失败 (0<>1)。

    下一个 WHEN子句被尝试并成功,因为 (0=0);但这是一个“什么都不做”的选项,因此进入另一个搜索循环 之后 IND-TABLE-ENTRIES递增 .

    通过 SEARCH 在此迭代和所有后续迭代上的结果相同ed WHEN 列表(没有一个子句匹配)...重复这个循环直到 IND-TABLE-ENTRIES增加到超出表的末尾。

    此时 SEARCH终止并且控制流回到 500-PROCESS-FILE 中的下一个循环.什么都没有打印。
    500-PROCESS-FILE然后递增 ROUTINE-CHECK由 1(现在是 2)。我们有一位推销员,他的电话是 SALESMAN-NUMBER 02 所以我们应该得到一些输出 - 对吗?错误的!但为什么?

    如果您阅读了 SEARCH动词你会发现它不会重置表索引(在这种情况下: IND-TABLE-ENTRIES )。当输入 SEARCH 时,它开始使用它拥有的任何值。您永远不会重置它,因此它已经设置在表格末尾之外。 SEARCH 只是终止并没有打印任何内容 - 再一次。

    修复问题

    鉴于您已加载 TABLE-ENTRIES首先通过销售员编号,我看不出使用 SEARCH 的目的。只需执行以下操作:
    500-PROCESS-FILE.   
    PERFORM VARYING ROUTINE-CHECK FROM 1 BY 1
    UNTIL ROUTINE-CHECK > 99
    IF SALESMAN-NUMBER (ROUTINE-CHECK) = ZERO
    CONTINUE
    ELSE
    PERFORM 520-WRITE-FILE
    END-IF
    END-PERFORM.

    为表设置一个初始化循环也可能是一个好主意,以便在您读取销售员文件之前将每个 SALESMAN-NUMBER 显式设置为零。

    如果您必须在此程序中使用 SEARCH,那么在引用要搜索的表时不要忘记设置和使用关联的表索引变量。

    关于cobol - 一个奇怪的错误 (COBOL),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4918147/

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