1

我是 COBOL 编程的新手,我在可能应该是微不足道的事情上遇到困难。我想找到用户输入的最小值和最大值。当用户点击 0 时,应显示最大值、最小值和平均值。平均值很简单,但最小值和最大值让我想知道。如果这是 JAVA 或其他语言,我会做一些比较 MAX INT 值的场景。不幸的是 COBOL 中的 High-Value 和 Low-Value 不是整数值????所以我决定把用户的条目放在一个表中,然后使用内部函数来做我需要的事情。但是,一旦我尝试这样计算:

 compute Min-Result = Function Min (Num-Field(ALL))

我收到一条错误消息,上面写着“语法错误,全部出乎意料”。在这一点上,我完全不知道该怎么做以及为什么会出现此错误。我正在使用 OpenCOBOL 1.1 Mingw。这是我的完整代码。任何帮助将不胜感激。什么都可以。我还确保没有超过 72 行。

     identification division.
       program-id.  lab1a.
      * no envionrment division since there are no files needed, etc.
       data division.
       working-storage section.

      * declaring proper variables to store integer values
       01  Max-Result        PIC S9(5).
       01  Min-Result        PIC S9(5).
       01  Count-Val         PIC 9  Value 0.
       01  Running-Tot       PIC S9(10)v99.
       01  First-Zero        PIC 9  Value 1.
       01  Final-Format-Avg       PIC ZZZZZ9.9999.
       01  Avg-Ent              PIC S9(5)v9999.
       01  Calc-Table.
            03  Table-Record  Occurs 1 to 500 times
                              depending on Entered-Num.
                05  Num-Field    PIC S9(5).
       01  Entered-Num       PIC S9(5).

       procedure division.
       000-Main.



           perform with test after until Entered-Num = 0
              display "Enter a 4-digit number (0 to stop): "
                 with no advancing
              accept Entered-Num

              add 1 to Count-Val
              add Entered-Num to Running-Tot

              display Running-Tot
              display Count-Val
              move Entered-Num to Num-Field(Count-Val)



      * this way every time the user enters a non zero number it will be re-assigned
      * to the variable Ending-Num. If they enter zero the if condition is skipped, the
      * loop condition is tested at the top and is ended.


           end-perform.
           subtract 1 from Count-Val
           display Count-Val
           display " "  
           display " "    
      *WATCH FOR TRUNCATION ERROR.....
           Divide Running-Tot By Count-Val Giving Avg-Ent
           move Avg-Ent to Final-Format-Avg

      *******WHY DOES THIS NOT WORK???????*********************** 
           compute Min-Result = Function Min (Num-Field(ALL))
           compute Max-Result = Function Max (Num-Field(ALL))


           if First-Zero = 0
           display "The first number you entered was zero. 
 &                  Next time enter a different one."
           else                
           display "The lowest value entered: " Min-Result             
           display "The highest value entered: " Max-Result
           display "The average value entered: " 
                    Final-Format-Avg
           end-if

           stop run.
4

2 回答 2

3

OpenCOBOL 内部函数当前不支持 ALL,这是一个在书籍上用于实现的特性。

于 2013-02-06T20:13:04.807 回答
2

您将“Entered-Num”作为您的 Occurs取决于字段。Entered-Num 在您使用该功能时为零。它应该是 Count-Val。

那不是问题,但你问了。

查看 2009 OpenCobol Programmer's Guid,我找不到支持 ALL 的确认。

保持“最低值”和“最高值”并根据需要与输入的数字进行比较/替换会更简单/更快。

于 2013-02-06T09:25:36.997 回答