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

时间:2011-06-24 23:48:28

标签: cobol gnucobol

我想知道,我的代码出了什么问题,因为当我将它编译成以下行中的错误时:

hello.cob:在'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

2 个答案:

答案 0 :(得分:2)

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

       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.

更清洁更好的版本:

       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.

答案 1 :(得分:2)

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

当你的程序站立时,用户必须为每个值输入四位数,如果只输入三位数,则计算失败。