IBM主机技术一本通
上QQ阅读APP看本书,新人免费读10天
设备和账号都新为新人

6.9 算术运算语句

算术运算语句的通用语法如:

绝大多数COBOL语句都可以套用上面的语法格式,比如:

        ADD TRAN-AMT TO TranTotal.
        ADD AC-INT TO BALANCE GIVING TRAN-TOTAL
        .
        SUBTRACT AC-INT FROM BALANCE
        SUBTRACT AC-INT FROM BALANCE GIVING TOTAL-BALANCE
        .
        MULTIPLY ACCU-BALANCE BY INT-RATE
        MULTIPLY ACCU-BALANCE BY INT-RATE GIVING AC-INT
        .
        DIVIDE TOTAL-AMT BY TRAN-COUNT GIVING AVERAGE-AMT
        .

但上面的语法规则不适合COMPUTE语句和带有REMINDER选项的DIVIDE语句,我们在后面将对它们做单独的介绍。

6.9.1 四舍五入(ROUNDED)选项

四舍五入(ROUNDED)选项用于带有小数点的十进制数运算。当计算结果在小数点对其后最右边的数字需要截断时就会生效。当要截断的最左边的小数位的值是5或大于5时,就会加1到结果的数据项中。

6.9.2 数据溢出(ON SIZE ERROR)选项

当进行十进制数算术运算后,如果小数点对齐后导致左边或右边的数据截断就会出现数据溢出的状态。如果在算术运算语句中指定了四舍五入(ROUNDER)选项,则只有最高位出现截断(溢出)时才会出现溢出状态。

6.9.3 带有余数(REMAINDER)选项的DIVIDE语句

下面是带有余数(REMAINDER)选项的除法(DIVIDE)语句的语法格式,除了多了余数选项外,其他部分与上面的通用格式是一致的。

6.9.4 综合运算(COMPUTE)语句

综合运算(COMPUTE)语句让你可以像做数学题一样随意使用算术表达式完成你要求的计算,下面是综合运算(COMPUTE)语句的语法。

算术综合运算的优先规则是:

● 幂(** = POWER(N n))。

● 乘(* = MULTIPLY (X))和除(/ = DIVIDE (÷))。

● 加(+ = ADD (+))和减(- = SUBTRACT (-))。

6.9.5 程序例子(算术运算)

我们依然使用演示报表(Magic Report)来说明COBOL语言中各种算术运算指令的使用方法。程序的第79行到第136行是加法的例子,对应于报表的第3行到第8行。请注意,对应于每一个算术运算,在报表中我们都有两行与其对应,前面一行显示的是算术运算前各个变量的值,而第2行则显示算术运算后各个变量的值。

第1个加法(第79行到第96行)是正常的,既没有溢出,也没有四舍五入;第2个加法(第97行到第113行)出现了溢出,因为我们定义的数字都是一位整数的,而当结果大于9.99时就会出现溢出,这时运算就出错了(出现了SIZE ERROR状态),所以你看到报表中的变量值都没有变化;第3个加法(第114行到第132行)使用了四舍五入(Rounded)选项,表明要对计算结果四舍五入,从运算结果看,确实四舍五入了,因为WS-X的值是1,685时,计算的结果由原来的3.01变成了3.02。

接下来的第133行到第137行是用来清除Z变量的值的,以免它出现在没有使用到Z变量的报表中。在第46、48、62和64行,使用了本书中没有介绍的BLANK WHEN ZERO短语,它的功能是,当数字型的数据的值为零时,不显示零而显示为空格,这在报表中经常用到。由于篇幅的限制,我们不可能对COBOL中的每个命令都介绍,即使我们介绍的命令,其语法格式也是简化了的,因为我们希望读者能在有限的时间内获取最大的信息量,对于过于繁复的语法描述,若读者确实需要的,就要查阅相关的参考手册了。

第138行到第178行演示的是减法命令,对应于报表的第9行到第12行。第1个减法是再普通不过的,而第2个减法则使用了四舍五入选项,导致的结果也随着WS-X的值由1.68变成1.685而产生了变化。

第180行到第237行演示的是乘法的例子,对应于报表的第13行到第18行。第1个乘法是正常的情形,第2个乘法运算则出现了溢出,因为两个数的乘积达到了2位整数,超过了变量的数据定义,这是你在程序设计中要经常注意的问题。第1是要确保定义的数据能够存放运算的结果,第2是不能假定运算是没有问题的,肯定不会溢出的,这就是为什么我们在所有的运算中都加上了ON SIZE ERROR短语的原因,只有这样,一旦出现溢出的情况,程序就能采取办法处理,避免造成不可收拾的结果(比如,错误的报表发送到了客户手里而要挨家挨户发道歉信)。第3个乘法则显示四舍五入的功能,变化依然是由X的值为1.685而导致的运算结果的不同。

除法的例子出现在第239行到第304行,对应于报表的第19行到第24行。第1个是正常的除法,第2个显示了余数(Remainder)选项的使用,第3个例子说明了四舍五入的用法。因为跟上面的情况差不多,我们就不重复了。

综合运算(Compute)命令出现在程序的第310行到第354行,对应于报表的第25行到第28行。它是最灵活的算术运算方式,无论多复杂的算术表达式,它都能给你搞定。有了上面的介绍,例子中的两个情形相信你能够很好地理解它们。

下面是完整的算术运算的程序代码,你可以慢慢研究。

        000001  IDENTIFICATION DIVISION.
        000002 *
        000003  PROGRAM-ID. ARITHME.
        000004  AUTHOR. NEWMAN LV.
        000005 *
        000006  ENVIRONMENT DIVISION.
        000007 *
        000008  INPUT-OUTPUT SECTION.
        000009 *
        000010  DATA DIVISION.
        000011 *
        000012
        000013  FILE SECTION.
        000014 *
        000015  WORKING-STORAGE SECTION.
        000016 *
        000017  01 RPT-HEADING.
        000018     03 FILLER          PIC X(20) VALUE SPACES.
        000019     03 RPT-HEAD        PIC X(25) VALUE '*MAGIC REPORT(ARITHME)*'.
        000020     03 FILLER           PIC X(27) VALUE SPACES.
        000021  01 RPT-SUB-HEADING.
        000022     03 FILLER           PIC X(10) VALUE '*AIR EXPR*'.
        000023     03 FILLER           PIC X(01) VALUE SPACE.
        000024     03 FILLER           PIC X(25) VALUE 'COBOL CMD*'.
        000025     03 FILLER           PIC X(01) VALUE SPACE.
        000026     03 FILLER           PIC X(05) VALUE 'X VAR'.
        000027     03 FILLER           PIC X(01) VALUE SPACE.
        000028     03 FILLER           PIC X(05) VALUE 'Y VAR'.
        000029     03 FILLER           PIC X(01) VALUE SPACE.
        000030     03 FILLER           PIC X(05) VALUE 'Z VAR'.
        000031     03 FILLER           PIC X(01) VALUE SPACE.
        000032     03 FILLER           PIC X(05) VALUE 'R VAR'.
        000033     03 FILLER           PIC X(01) VALUE SPACE.
        000034     03 FILLER           PIC X(06) VALUE 'RNDED?'.
        000035     03 FILLER           PIC X(01) VALUE SPACE.
        000036     03 FILLER           PIC X(06) VALUE 'S ERR?'.
        000037  01 RPT-DETAIL-BEFORE.
        000038     03 WS-AIR-EXP      PIC X(10) VALUE SPACES.
        000039     03 FILLER           PIC X(01) VALUE SPACE.
        000040     03 WS-COBOL-CMD    PIC X(25) VALUE SPACES.
        000041     03 FILLER           PIC X(01) VALUE SPACE.
        000042     03 WS-X-VALUE-B    PIC 9.999-.
        000043     03 FILLER           PIC X(01) VALUE SPACE.
        000044     03 WS-Y-VALUE-B    PIC 9.99-.
        000045     03 FILLER           PIC X(01) VALUE SPACE.
        000046     03 WS-Z-VALUE-B    PIC 9.99- BLANK WHEN ZERO.
        000047     03 FILLER           PIC X(01) VALUE SPACE.
        000048     03 WS-R-VALUE-B    PIC 9.99- BLANK WHEN ZERO.
        000049     03 FILLER           PIC X(04) VALUE SPACE.
        000050     03 FILLER           PIC X(01) VALUE SPACES.
        000051     03 FILLER           PIC X(04) VALUE SPACE.
        000052     03 FILLER           PIC X(01) VALUE SPACE.
        000053  01 RPT-DETAIL-AFTER.
        000054     03 FILLER           PIC X(10) VALUE SPACES.
        000055     03 FILLER           PIC X(01) VALUE SPACE.
        000056     03 FILLER           PIC X(25) VALUE SPACES.
        000057     03 FILLER           PIC X(01) VALUE SPACE.
        000058     03 WS-X-VALUE-A    PIC 9.999-.
        000059     03 FILLER           PIC X(01) VALUE SPACE.
        000060     03 WS-Y-VALUE-A    PIC 9.99-.
        000061     03 FILLER           PIC X(01) VALUE SPACE.
        000062     03 WS-Z-VALUE-A    PIC 9.99- BLANK WHEN ZERO.
        000063     03 FILLER           PIC X(01) VALUE SPACE.
        000064     03 WS-R-VALUE-A    PIC 9.99- BLANK WHEN ZERO.
        000065     03 FILLER           PIC X(04) VALUE SPACE.
        000066     03 WS-ROUNDED      PIC X(01) VALUE SPACES.
        000067     03 FILLER           PIC X(04) VALUE SPACE.
        000068     03 WS-SIZE-ERROR   PIC X(01) VALUE SPACE.
        000069  01 WS-ITEMS.
        000070     03 WS-X             PIC 9V9(3) VALUE 0.
        000071     03 WS-Y             PIC 9V9(2) VALUE 0.
        000072     03 WS-Z             PIC 9V9(2) VALUE 0.
        000073     03 WS-R             PIC 9V9(2) VALUE 0.
        000074 *
        000075  PROCEDURE DIVISION.
        000076 * SHOW HEADING AND SUBHEADING
        000077      DISPLAY RPT-HEADING
        000078      DISPLAY RPT-SUB-HEADING
        000079 * ADD
        000080      MOVE 'X + Y'               TO WS-AIR-EXP
        000081      MOVE 'ADD WS-X TO WS-Y'   TO WS-COBOL-CMD
        000082      MOVE 1.68                  TO WS-X
        000083                                    WS-X-VALUE-B
        000084      MOVE 1.33                  TO WS-Y
        000085                                    WS-Y-VALUE-B
        000086      MOVE 'N'                   TO WS-SIZE-ERROR
        000087      ADD WS-X TO WS-Y
        000088          ON SIZE ERROR
        000089            MOVE 'Y'             TO WS-SIZE-ERROR
        000090      END-ADD
        000091      MOVE WS-X                  TO WS-X-VALUE-A
        000092      MOVE WS-Y                  TO WS-Y-VALUE-A
        000093      MOVE 'N'                   TO WS-ROUNDED
        000094      DISPLAY RPT-DETAIL-BEFORE
        000095      DISPLAY RPT-DETAIL-AFTER
        000096      .
        000097      MOVE 'X + Y'               TO WS-AIR-EXP
        000098      MOVE 'ADD WS-X TO WS-Y'   TO WS-COBOL-CMD
        000099      MOVE 9.68                  TO WS-X
        000100                                    WS-X-VALUE-B
        000101      MOVE 1.33                  TO WS-Y
        000102                                    WS-Y-VALUE-B
        000103      MOVE 'N'                   TO WS-SIZE-ERROR
        000104      ADD WS-X TO WS-Y
        000105          ON SIZE ERROR
        000106            MOVE 'Y'             TO WS-SIZE-ERROR
        000107      END-ADD
        000108      MOVE WS-X                  TO WS-X-VALUE-A
        000109      MOVE WS-Y                  TO WS-Y-VALUE-A
        000110      MOVE 'N'                   TO WS-ROUNDED
        000111      DISPLAY RPT-DETAIL-BEFORE
        000112      DISPLAY RPT-DETAIL-AFTER
        000113      .
        000114      MOVE 'Z = X + Y'          TO WS-AIR-EXP
        000115      MOVE 'ADD X Y GIVING Z ROUNDED' TO WS-COBOL-CMD
        000116      MOVE 1.685                 TO WS-X
        000117                                    WS-X-VALUE-B
        000118      MOVE 1.33                  TO WS-Y
        000119                                    WS-Y-VALUE-B
        000120      MOVE 5.66                  TO WS-Z
        000121                                    WS-Z-VALUE-B
        000122      MOVE 'N'                   TO WS-SIZE-ERROR
        000123      ADD WS-X WS-Y GIVING WS-Z ROUNDED
        000124          ON SIZE ERROR
        000125            MOVE 'Y'             TO WS-SIZE-ERROR
        000126      END-ADD
        000127      MOVE WS-X                  TO WS-X-VALUE-A
        000128      MOVE WS-Y                  TO WS-Y-VALUE-A
        000129      MOVE WS-Z                  TO WS-Z-VALUE-A
        000130      MOVE 'Y'                   TO WS-ROUNDED
        000131      DISPLAY RPT-DETAIL-BEFORE
        000132      DISPLAY RPT-DETAIL-AFTER
        000133      MOVE ZEROS                 TO WS-Z
        000134                                    WS-Z-VALUE-B
        000135                                    WS-Z-VALUE-A
        000136      .
        000137 * SUBTRACT
        000138      MOVE 'X - Y'               TO WS-AIR-EXP
        000139      MOVE 'SUBTRACT Y FROM X'  TO WS-COBOL-CMD
        000140      MOVE 1.68                  TO WS-X
        000141                                    WS-X-VALUE-B
        000142      MOVE 1.33                  TO WS-Y
        000143                                    WS-Y-VALUE-B
        000144      MOVE 'N'                   TO WS-SIZE-ERROR
        000145      SUBTRACT WS-Y FROM WS-X
        000146          ON SIZE ERROR
        000147            MOVE 'Y'            TO WS-SIZE-ERROR
        000148      END-SUBTRACT
        000149      MOVE WS-X                 TO WS-X-VALUE-A
        000150      MOVE WS-Y                 TO WS-Y-VALUE-A
        000151      MOVE 'N'                  TO WS-ROUNDED
        000152      DISPLAY RPT-DETAIL-BEFORE
        000153      DISPLAY RPT-DETAIL-AFTER
        000154      .
        000155      MOVE 'Z = X - Y'          TO WS-AIR-EXP
        000156      MOVE 'SUBTRACT Y FROM X GIVING Z ROUNDED' TO WS-COBOL-CMD
        000157      MOVE 1.685                 TO WS-X
        000158                                    WS-X-VALUE-B
        000159      MOVE 1.33                  TO WS-Y
        000160                                    WS-Y-VALUE-B
        000161      MOVE 5.66                  TO WS-Z
        000162                                    WS-Z-VALUE-B
        000163      MOVE 'N'                   TO WS-SIZE-ERROR
        000164      SUBTRACT WS-Y FROM WS-X GIVING WS-Z ROUNDED
        000165          ON SIZE ERROR
        000166            MOVE 'Y'             TO WS-SIZE-ERROR
        000167      END-SUBTRACT
        000168      MOVE WS-X                  TO WS-X-VALUE-A
        000169      MOVE WS-Y                  TO WS-Y-VALUE-A
        000170      MOVE WS-Z                  TO WS-Z-VALUE-A
        000171      MOVE 'Y'                   TO WS-ROUNDED
        000172      DISPLAY RPT-DETAIL-BEFORE
        000173      DISPLAY RPT-DETAIL-AFTER
        000174      .
        000175      MOVE ZEROS                 TO WS-Z
        000176                                    WS-Z-VALUE-B
        000177                                    WS-Z-VALUE-A
        000178      .
        000179 * MULTIPLY
        000180      MOVE 'X * Y'               TO WS-AIR-EXP
        000181      MOVE 'MULTIPLY X BY Y '   TO WS-COBOL-CMD
        000182      MOVE 1.68                  TO WS-X
        000183                                    WS-X-VALUE-B
        000184      MOVE 1.33                  TO WS-Y
        000185                                    WS-Y-VALUE-B
        000186      MOVE 'N'                   TO WS-SIZE-ERROR
        000187      MULTIPLY WS-X BY WS-Y
        000188          ON SIZE ERROR
        000189            MOVE 'Y'             TO WS-SIZE-ERROR
        000190      END-MULTIPLY
        000191      MOVE WS-X                  TO WS-X-VALUE-A
        000192      MOVE WS-Y                  TO WS-Y-VALUE-A
        000193      MOVE 'N'                   TO WS-ROUNDED
        000194      DISPLAY RPT-DETAIL-BEFORE
        000195      DISPLAY RPT-DETAIL-AFTER
        000196      .
        000197      MOVE 'X * Y'               TO WS-AIR-EXP
        000198      MOVE 'MULTIPLY WS-X TO WS-Y' TO WS-COBOL-CMD
        000199      MOVE 9.68                  TO WS-X
        000200                                    WS-X-VALUE-B
        000201      MOVE 1.33                  TO WS-Y
        000202                                    WS-Y-VALUE-B
        000203      MOVE 'N'                   TO WS-SIZE-ERROR
        000204      MULTIPLY WS-X BY WS-Y
        000205          ON SIZE ERROR
        000206            MOVE 'Y'             TO WS-SIZE-ERROR
        000207      END-MULTIPLY
        000208      MOVE WS-X                  TO WS-X-VALUE-A
        000209      MOVE WS-Y                  TO WS-Y-VALUE-A
        000210      MOVE 'N'                   TO WS-ROUNDED
        000211      DISPLAY RPT-DETAIL-BEFORE
        000212      DISPLAY RPT-DETAIL-AFTER
        000213      .
        000214      MOVE 'Z = X * Y'          TO WS-AIR-EXP
        000215      MOVE 'MULTIPLY X Y GIVING Z ROUNDED' TO WS-COBOL-CMD
        000216      MOVE 1.685                 TO WS-X
        000217                                    WS-X-VALUE-B
        000218      MOVE 1.33                  TO WS-Y
        000219                                    WS-Y-VALUE-B
        000220      MOVE 5.66                  TO WS-Z
        000221                                    WS-Z-VALUE-B
        000222      MOVE 'N'                   TO WS-SIZE-ERROR
        000223      MULTIPLY WS-X BY WS-Y GIVING WS-Z ROUNDED
        000224          ON SIZE ERROR
        000225            MOVE 'Y'             TO WS-SIZE-ERROR
        000226      END-MULTIPLY
        000227      MOVE WS-X                  TO WS-X-VALUE-A
        000228      MOVE WS-Y                  TO WS-Y-VALUE-A
        000229      MOVE WS-Z                  TO WS-Z-VALUE-A
        000230      MOVE 'Y'                   TO WS-ROUNDED
        000231      DISPLAY RPT-DETAIL-BEFORE
        000232      DISPLAY RPT-DETAIL-AFTER
        000233      .
        000234      MOVE ZEROS                 TO WS-Z
        000235                                    WS-Z-VALUE-B
        000236                                    WS-Z-VALUE-A
        000237      .
        000238 * DIVIDE
        000239      MOVE 'X / Y'               TO WS-AIR-EXP
        000240      MOVE 'DIVIDE X BY Y   '    TO WS-COBOL-CMD
        000241      MOVE 1.68                  TO WS-X
        000242                                    WS-X-VALUE-B
        000243      MOVE 1.33                  TO WS-Y
        000244                                    WS-Y-VALUE-B
        000245      MOVE 3.77                  TO WS-Z
        000246                                    WS-Z-VALUE-B
        000247      MOVE 'N'                   TO WS-SIZE-ERROR
        000248      DIVIDE WS-X BY WS-Y GIVING WS-Z
        000249          ON SIZE ERROR
        000250            MOVE 'Y'             TO WS-SIZE-ERROR
        000251      END-DIVIDE
        000252      MOVE WS-X                  TO WS-X-VALUE-A
        000253      MOVE WS-Y                  TO WS-Y-VALUE-A
        000254      MOVE WS-Z                  TO WS-Z-VALUE-A
        000255      MOVE 'N'                   TO WS-ROUNDED
        000256      DISPLAY RPT-DETAIL-BEFORE
        000257      DISPLAY RPT-DETAIL-AFTER
        000258      .
        000259      MOVE 'X / Y'               TO WS-AIR-EXP
        000260            MOVE ‘DIVIDE X BY Y REMINDER' TO WS-COBOL-CMD
        000261      MOVE 9.68                  TO WS-X
        000262                                    WS-X-VALUE-B
        000263      MOVE 1.33                  TO WS-Y
        000264                                    WS-Y-VALUE-B
        000265      MOVE 1.97                  TO WS-R
        000266                                    WS-R-VALUE-B
        000267      MOVE 3.77                  TO WS-Z
        000268                                    WS-Z-VALUE-B
        000269      MOVE 'N'                   TO WS-SIZE-ERROR
        000270      DIVIDE WS-X BY WS-Y GIVING WS-Z REMAINDER WS-R
        000271          ON SIZE ERROR
        000272            MOVE 'Y'             TO WS-SIZE-ERROR
        000273      END-DIVIDE
        000274      MOVE WS-X                  TO WS-X-VALUE-A
        000275      MOVE WS-Y                  TO WS-Y-VALUE-A
        000276      MOVE WS-Z                  TO WS-Z-VALUE-A
        000277      MOVE WS-R                  TO WS-R-VALUE-A
        000278      MOVE 'N'                   TO WS-ROUNDED
        000279      DISPLAY RPT-DETAIL-BEFORE
        000280      DISPLAY RPT-DETAIL-AFTER
        000281      MOVE ZEROS                 TO WS-R
        000282                                    WS-R-VALUE-B
        000283                                    WS-R-VALUE-A
        000284      .
        000285      MOVE 'Z = X / Y'              TO WS-AIR-EXP
        000286      MOVE 'DIVIDE X BY Y GIVING Z ROUNDED' TO WS-COBOL-CMD
        000287      MOVE 1.685                 TO WS-X
        000288                                    WS-X-VALUE-B
        000289      MOVE 1.33                  TO WS-Y
        000290                                    WS-Y-VALUE-B
        000291      MOVE 5.66                  TO WS-Z
        000292                                    WS-Z-VALUE-B
        000293      MOVE 'N'                   TO WS-SIZE-ERROR
        000294      DIVIDE WS-X BY WS-Y GIVING WS-Z ROUNDED
        000295          ON SIZE ERROR
        000296            MOVE 'Y'             TO WS-SIZE-ERROR
        000297      END-DIVIDE
        000298      MOVE WS-X                  TO WS-X-VALUE-A
        000299      MOVE WS-Y                  TO WS-Y-VALUE-A
        000300      MOVE WS-Z                  TO WS-Z-VALUE-A
        000301      MOVE 'Y'                   TO WS-ROUNDED
        000302      DISPLAY RPT-DETAIL-BEFORE
        000303      DISPLAY RPT-DETAIL-AFTER
        000304      .
        000305      MOVE ZEROS                 TO WS-Z
        000306                                    WS-Z-VALUE-B
        000307                                    WS-Z-VALUE-A
        000308      .
        000309 * COMPUTE
        000310      MOVE 'R = X*Y+Z'          TO WS-AIR-EXP
        000311      MOVE 'COMPUTE R= X*Y + Z     ' TO WS-COBOL-CMD
        000312      MOVE 1.685                 TO WS-X
        000313                                    WS-X-VALUE-B
        000314      MOVE 1.79                  TO WS-Y
        000315                                    WS-Y-VALUE-B
        000316      MOVE 3.97                  TO WS-R
        000317                                    WS-R-VALUE-B
        000318      MOVE 5.66                  TO WS-Z
        000319                                    WS-Z-VALUE-B
        000320      MOVE 'N'                   TO WS-SIZE-ERROR
        000321      COMPUTE WS-R = WS-X * WS-Y + WS-Z
        000322          ON SIZE ERROR
        000323            MOVE 'Y'             TO WS-SIZE-ERROR
        000324      END-COMPUTE
        000325      MOVE WS-X                  TO WS-X-VALUE-A
        000326      MOVE WS-Y                  TO WS-Y-VALUE-A
        000327      MOVE WS-R                  TO WS-R-VALUE-A
        000328      MOVE WS-Z                  TO WS-Z-VALUE-A
        000329      MOVE 'N'                   TO WS-ROUNDED
        000330      DISPLAY RPT-DETAIL-BEFORE
        000331      DISPLAY RPT-DETAIL-AFTER
        000332      .
        000333      MOVE 'R = X*Y+Z'          TO WS-AIR-EXP
        000334      MOVE 'COMPUTE R= X*Y + Z ROUNDED' TO WS-COBOL-CMD
        000335      MOVE 1.685                 TO WS-X
        000336                                    WS-X-VALUE-B
        000337      MOVE 1.79                  TO WS-Y
        000338                                    WS-Y-VALUE-B
        000339      MOVE 3.97                  TO WS-R
        000340                                    WS-R-VALUE-B
        000341      MOVE 5.66                  TO WS-Z
        000342                                    WS-Z-VALUE-B
        000343      MOVE 'N'                   TO WS-SIZE-ERROR
        000344      COMPUTE WS-R ROUNDED = WS-X * WS-Y + WS-Z
        000345          ON SIZE ERROR
        000346            MOVE 'Y'             TO WS-SIZE-ERROR
        000347      END-COMPUTE
        000348      MOVE WS-X                  TO WS-X-VALUE-A
        000349      MOVE WS-Y                  TO WS-Y-VALUE-A
        000350      MOVE WS-R                  TO WS-R-VALUE-A
        000351      MOVE WS-Z                  TO WS-Z-VALUE-A
        000352      MOVE 'Y'                   TO WS-ROUNDED
        000353      DISPLAY RPT-DETAIL-BEFORE
        000354      DISPLAY RPT-DETAIL-AFTER
        000355      .
        000356 *
        000357      STOP RUN.
        6.9.6  程序运行结果(算术运算)
        下面显示的是算术运算程序的运行结果,对着上面的程序,读起来更有味道。
        000001                   *MAGIC REPORT(ARITHME)*
        000002  *AIR EXPR* COBOL CMD*                 X VAR Y VAR Z VAR R VAR RNDED? S ERR?
        000003  X + Y     ADD WS-X TO WS-Y            1.680  1.33
        000004                                         1.680  3.01               N   N
        000005  X + Y     ADD WS-X TO WS-Y            9.680  1.33
        000006                                         9.680  1.33               N   Y
        000007  Z = X + Y ADD X Y GIVING Z ROUNDED    1.685  1.33  5.66
        000008                                         1.685  1.33  3.02         Y   N
        000009  X - Y     SUBTRACT Y FROM X           1.680  1.33
        000010                                         0.350  1.33               N   N
        000011  Z = X - Y SUBTRACT Y FROM X GIVING    1.685  1.33  5.66
        000012                                         1.685  1.33  0.36         Y   N
        000013  X * Y     MULTIPLY X BY Y             1.680  1.33
        000014                                         1.680  2.23               N   N
        000015  X * Y     MULTIPLY WS-X TO WS-Y       9.680  1.33
        000016                                         9.680  1.33               N   Y
        000017  Z = X * Y MULTIPLY X Y GIVING Z ROU   1.685  1.33  5.66
        000018                                         1.685  1.33  2.24         Y   N
        000019  X / Y     DIVIDE X BY Y               1.680  1.33  3.77
        000020                                         1.680  1.33  1.26         N   N
        000021  X / Y     DIVIDE X BY Y REMINDER      9.680  1.33  3.77  1.97
        000022                                         9.680  1.33  7.27  0.01   N   N
        000023  Z = X / Y DIVIDE X BY Y GIVING Z RO   1.685  1.33  5.66
        000024                                         1.685  1.33  1.27         Y   N
        000025  R = X*Y+Z COMPUTE R= X*Y + Z          1.685  1.79  5.66  3.97
        000026                                         1.685  1.79  5.66  8.67   N   N
        000027  R = X*Y+Z COMPUTE R= X*Y + Z ROUNDE   1.685  1.79  5.66  3.97
        000028                                         1.685  1.79  5.66  8.68   Y   N