计算三角形面积的简单程序

发布于 2024-11-17 05:57:25 字数 2326 浏览 4 评论 0原文

我想知道我的代码有什么问题,因为当我编译它时,出现以下几行错误:

hello.cob: In paragraph 'DADOS':
hello.cob:25: 错误:语法错误,意外的“(”,需要 WORD
hello.cob:在“FIM”段落中:
hello.cob:31: 错误:语法错误,意外的 '('

这里是我的程序的来源:

   IDENTIFICATION   DIVISION.
   PROGRAM-ID.      TRIANG.
DATA DIVISION.

WORKING-STORAGE SECTION.
77  BASE    PIC 9(04) VALUE ZEROS.
77  ALTURA PIC 9(04) VALUE ZEROS.
77  HAREA   PIC 9(06) VALUE ZEROS.

SCREEN SECTION.
01  TELA.
02  BLANK SCREEN.
02  LINE 04 COLUMN 21 VALUE
"CALCULO DA AREA DE UM TRIANGULO".
02  LINE 06 COLUMN 15 VALUE
"AUTOR: Alexandre".
02  LINE 10 COLUMN 21 VALUE "ENTRE COM O VALOR DA ALTURA...".
02  LINE 12 COLUMN 21 VALUE "ENTRE COM O VALOR DA BASE...".
02  LINE 14 COLUMN 21 VALUE "ENTRE COM O VALOR DA AREA...".

PROCEDURE DIVISION.
INICIO.
    DISPLAY TELA.
DADOS.
    ACCEPT(10, 51) ALTURA.
    ACCEPT(12, 51) BASE.
CALCULO.
    MULTIPLY BASE BY ALTURA GIVING HAREA.
    DIVIDE 2 INTO HAREA.
FIM.    
    DISPLAY(14, 51) HAREA.
    DISPLAY(24, 31) "FIM DE PROGRAMA".
    STOP RUN.

感谢您的帮助。


我将这两行放入代码中:

   IDENTIFICATION   DIVISION.
   PROGRAM-ID.      TRIANG.
   DATA DIVISION.

   WORKING-STORAGE SECTION.
   77  BASE    PIC 9(04) VALUE ZEROS.
   77  ALTURA  PIC 9(04) VALUE ZEROS.
   77  HAREA   PIC 9(06) VALUE ZEROS.
   77  ESPERA  PIC 9(01) VALUE ZEROS.

   SCREEN SECTION.
   01  TELA.
   02  BLANK SCREEN.
   02  LINE 04 COLUMN 21 VALUE
   "CALCULO DA AREA DE UM TRIANGULO".
   02  LINE 06 COLUMN 15 VALUE
   "AUTOR: Alexandre".
   02  LINE 10 COLUMN 21 VALUE "ENTRE COM O VALOR DA ALTURA... ".
   02  LINE 12 COLUMN 21 VALUE "ENTRE COM O VALOR DA BASE...   ".
   02  LINE 14 COLUMN 21 VALUE "ENTRE COM O VALOR DA AREA...   ".

   PROCEDURE DIVISION.
   INICIO.
       DISPLAY TELA.
   DADOS.
       ACCEPT ALTURA AT LINE 10 COLUMN 51.
       ACCEPT BASE AT LINE 12 COLUMN 51.
   CALCULO.
       MULTIPLY BASE BY ALTURA GIVING HAREA.
       DIVIDE 2 INTO HAREA.
   FIM.
       DISPLAY HAREA LINE 14 COLUMN 51.
       ACCEPT ESPERA AT LINE 0 COLUMN 0.
       DISPLAY "FIM DE PROGRAMA" LINE 24 COLUMN 31.
       STOP RUN.

已添加

77  ESPERA  PIC 9(01) VALUE ZEROS.

ACCEPT ESPERA AT LINE 0 COLUMN 0.

但结果是 000000

I wonder, what do is wrong with my code because when I compile it the error in the following lines:

hello.cob: In paragraph 'DADOS':
hello.cob:25: Error: syntax error, unexpected '(', expecting WORD
hello.cob: In paragraph 'FIM':
hello.cob:31: Error: syntax error, unexpected '('

Here the source of my program:

   IDENTIFICATION   DIVISION.
   PROGRAM-ID.      TRIANG.
DATA DIVISION.

WORKING-STORAGE SECTION.
77  BASE    PIC 9(04) VALUE ZEROS.
77  ALTURA PIC 9(04) VALUE ZEROS.
77  HAREA   PIC 9(06) VALUE ZEROS.

SCREEN SECTION.
01  TELA.
02  BLANK SCREEN.
02  LINE 04 COLUMN 21 VALUE
"CALCULO DA AREA DE UM TRIANGULO".
02  LINE 06 COLUMN 15 VALUE
"AUTOR: Alexandre".
02  LINE 10 COLUMN 21 VALUE "ENTRE COM O VALOR DA ALTURA...".
02  LINE 12 COLUMN 21 VALUE "ENTRE COM O VALOR DA BASE...".
02  LINE 14 COLUMN 21 VALUE "ENTRE COM O VALOR DA AREA...".

PROCEDURE DIVISION.
INICIO.
    DISPLAY TELA.
DADOS.
    ACCEPT(10, 51) ALTURA.
    ACCEPT(12, 51) BASE.
CALCULO.
    MULTIPLY BASE BY ALTURA GIVING HAREA.
    DIVIDE 2 INTO HAREA.
FIM.    
    DISPLAY(14, 51) HAREA.
    DISPLAY(24, 31) "FIM DE PROGRAMA".
    STOP RUN.

Thanks for the help.


I put this 2 lines in the code:

   IDENTIFICATION   DIVISION.
   PROGRAM-ID.      TRIANG.
   DATA DIVISION.

   WORKING-STORAGE SECTION.
   77  BASE    PIC 9(04) VALUE ZEROS.
   77  ALTURA  PIC 9(04) VALUE ZEROS.
   77  HAREA   PIC 9(06) VALUE ZEROS.
   77  ESPERA  PIC 9(01) VALUE ZEROS.

   SCREEN SECTION.
   01  TELA.
   02  BLANK SCREEN.
   02  LINE 04 COLUMN 21 VALUE
   "CALCULO DA AREA DE UM TRIANGULO".
   02  LINE 06 COLUMN 15 VALUE
   "AUTOR: Alexandre".
   02  LINE 10 COLUMN 21 VALUE "ENTRE COM O VALOR DA ALTURA... ".
   02  LINE 12 COLUMN 21 VALUE "ENTRE COM O VALOR DA BASE...   ".
   02  LINE 14 COLUMN 21 VALUE "ENTRE COM O VALOR DA AREA...   ".

   PROCEDURE DIVISION.
   INICIO.
       DISPLAY TELA.
   DADOS.
       ACCEPT ALTURA AT LINE 10 COLUMN 51.
       ACCEPT BASE AT LINE 12 COLUMN 51.
   CALCULO.
       MULTIPLY BASE BY ALTURA GIVING HAREA.
       DIVIDE 2 INTO HAREA.
   FIM.
       DISPLAY HAREA LINE 14 COLUMN 51.
       ACCEPT ESPERA AT LINE 0 COLUMN 0.
       DISPLAY "FIM DE PROGRAMA" LINE 24 COLUMN 31.
       STOP RUN.

Added

77  ESPERA  PIC 9(01) VALUE ZEROS.

and

ACCEPT ESPERA AT LINE 0 COLUMN 0.

but the result is 000000

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

杀手六號 2024-11-24 05:57:25

我让程序运行起来了。几乎。我不知道如何在程序终止时保持屏幕打开,因此结果永远不可见,但至少可以运行程序并输入前两个数字:

       IDENTIFICATION   DIVISION.
       PROGRAM-ID.      TRIANG.
       DATA DIVISION.

       WORKING-STORAGE SECTION.
       77  BASE    PIC 9(04) VALUE ZEROS.
       77  ALTURA PIC 9(04) VALUE ZEROS.
       77  HAREA   PIC 9(06) VALUE ZEROS.
       77  DUMMY  PIC 9(01) VALUE ZEROS.

       SCREEN SECTION.
       01  TELA.
       02  BLANK SCREEN.
       02  LINE 04 COLUMN 21 VALUE
       "CALCULO DA AREA DE UM TRIANGULO".
       02  LINE 06 COLUMN 15 VALUE
       "AUTOR: Alexandre".
       02  LINE 10 COLUMN 21 VALUE "ENTRE COM O VALOR DA ALTURA...".
       02  LINE 12 COLUMN 21 VALUE "ENTRE COM O VALOR DA BASE...".
       02  LINE 14 COLUMN 21 VALUE "ENTRE COM O VALOR DA AREA...".

       PROCEDURE DIVISION.
       INICIO.
           DISPLAY TELA.
       DADOS.
           ACCEPT ALTURA AT LINE 10 COLUMN 51.
           ACCEPT BASE AT LINE 12 COLUMN 51.
       CALCULO.
           MULTIPLY BASE BY ALTURA GIVING HAREA.
           DIVIDE 2 INTO HAREA.
       FIM.
           DISPLAY HAREA LINE 14 COLUMN 51.
           DISPLAY "FIM DE PROGRAMA" LINE 24 COLUMN 31.
           ACCEPT DUMMY AT LINE 1 COLUMN 1.
           STOP RUN.

A much更干净更好的版本:

       IDENTIFICATION   DIVISION.
   PROGRAM-ID.      TRIANG.
   DATA DIVISION.

   WORKING-STORAGE SECTION.
   77  BASE    PIC 9(04) VALUE ZEROS.
   77  ALTURA PIC 9(04) VALUE ZEROS.
   77  HAREA   PIC 9(07) VALUE ZEROS.
   77  AGAIN   PIC X VALUE 'N'.

   SCREEN SECTION.
   01  TELA.
   02  BLANK SCREEN.
   02  LINE 04 COLUMN 21 VALUE
   "CALCULO DA AREA DE UM TRIANGULO".
   02  LINE 06 COLUMN 15 VALUE
   "AUTOR: Alexandre".
   02  LINE 10 COLUMN 21 VALUE "ENTRE COM O VALOR DA ALTURA [____]".
   02  LINE 10 COLUMN 50 PIC ZZZ9 Using ALTURA.
   02  LINE 12 COLUMN 21 VALUE "ENTRE COM O VALOR DA BASE...[____]".
   02  LINE 12 COLUMN 50 PIC ZZZ9 Using BASE.
   02  LINE 14 COLUMN 21 VALUE "            VALOR DA AREA..".
   02  LINE 14 COLUMN 48 PIC ZZZZZZ9 From HAREA.
   02  LINE 16 COLUMN 21 VALUE "                    FINIS...[N]".
   02  LINE 16 COLUMN 50 PIC X Using AGAIN.

   PROCEDURE DIVISION.
   INICIO.
       DISPLAY TELA.
       PERFORM SCREENS UNTIL AGAIN  = 'Y'
       STOP RUN.
   SCREENS.
       ACCEPT TELA.
       MULTIPLY BASE BY ALTURA GIVING HAREA.
       DIVIDE 2 INTO HAREA.
       DISPLAY TELA.

I got the program to work. Almost. I don't know how to keep the screen up when the program terminates, so the result is never visible, but at least it is possible to run the program and put in the first two numbers:

       IDENTIFICATION   DIVISION.
       PROGRAM-ID.      TRIANG.
       DATA DIVISION.

       WORKING-STORAGE SECTION.
       77  BASE    PIC 9(04) VALUE ZEROS.
       77  ALTURA PIC 9(04) VALUE ZEROS.
       77  HAREA   PIC 9(06) VALUE ZEROS.
       77  DUMMY  PIC 9(01) VALUE ZEROS.

       SCREEN SECTION.
       01  TELA.
       02  BLANK SCREEN.
       02  LINE 04 COLUMN 21 VALUE
       "CALCULO DA AREA DE UM TRIANGULO".
       02  LINE 06 COLUMN 15 VALUE
       "AUTOR: Alexandre".
       02  LINE 10 COLUMN 21 VALUE "ENTRE COM O VALOR DA ALTURA...".
       02  LINE 12 COLUMN 21 VALUE "ENTRE COM O VALOR DA BASE...".
       02  LINE 14 COLUMN 21 VALUE "ENTRE COM O VALOR DA AREA...".

       PROCEDURE DIVISION.
       INICIO.
           DISPLAY TELA.
       DADOS.
           ACCEPT ALTURA AT LINE 10 COLUMN 51.
           ACCEPT BASE AT LINE 12 COLUMN 51.
       CALCULO.
           MULTIPLY BASE BY ALTURA GIVING HAREA.
           DIVIDE 2 INTO HAREA.
       FIM.
           DISPLAY HAREA LINE 14 COLUMN 51.
           DISPLAY "FIM DE PROGRAMA" LINE 24 COLUMN 31.
           ACCEPT DUMMY AT LINE 1 COLUMN 1.
           STOP RUN.

A much cleaner better version:

       IDENTIFICATION   DIVISION.
   PROGRAM-ID.      TRIANG.
   DATA DIVISION.

   WORKING-STORAGE SECTION.
   77  BASE    PIC 9(04) VALUE ZEROS.
   77  ALTURA PIC 9(04) VALUE ZEROS.
   77  HAREA   PIC 9(07) VALUE ZEROS.
   77  AGAIN   PIC X VALUE 'N'.

   SCREEN SECTION.
   01  TELA.
   02  BLANK SCREEN.
   02  LINE 04 COLUMN 21 VALUE
   "CALCULO DA AREA DE UM TRIANGULO".
   02  LINE 06 COLUMN 15 VALUE
   "AUTOR: Alexandre".
   02  LINE 10 COLUMN 21 VALUE "ENTRE COM O VALOR DA ALTURA [____]".
   02  LINE 10 COLUMN 50 PIC ZZZ9 Using ALTURA.
   02  LINE 12 COLUMN 21 VALUE "ENTRE COM O VALOR DA BASE...[____]".
   02  LINE 12 COLUMN 50 PIC ZZZ9 Using BASE.
   02  LINE 14 COLUMN 21 VALUE "            VALOR DA AREA..".
   02  LINE 14 COLUMN 48 PIC ZZZZZZ9 From HAREA.
   02  LINE 16 COLUMN 21 VALUE "                    FINIS...[N]".
   02  LINE 16 COLUMN 50 PIC X Using AGAIN.

   PROCEDURE DIVISION.
   INICIO.
       DISPLAY TELA.
       PERFORM SCREENS UNTIL AGAIN  = 'Y'
       STOP RUN.
   SCREENS.
       ACCEPT TELA.
       MULTIPLY BASE BY ALTURA GIVING HAREA.
       DIVIDE 2 INTO HAREA.
       DISPLAY TELA.
匿名。 2024-11-24 05:57:25

结果是“00000”的原因是因为您没有验证输入。

根据您的程序,用户必须为每个值输入四位数字,如果只输入三位数字,计算将失败。

The reason your result is "00000" is because you are not validating the input.

As your program stands the user MUST enter four digits for each value, if only three digits are entered the calculation fails.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文