8月 02, 2015

COBOL 算術運算


        ID DIVISION.
        PROGRAM-ID. P06999.
        ENVIRONMENT DIVISION.
        DATA DIVISION.
        WORKING-STORAGE SECTION.
        01 A PIC 99.
        01 B PIC 9(2) VALUE 2.
        01 C PIC 99 VALUE 30.
        01 R PIC 99 VALUE 0.
        01 DIV PIC 99V99 VALUE 00.00.
        PROCEDURE DIVISION.
        MAIN SECTION.
        MAIN-PARA.
            DISPLAY 'INPUT NUMBER'.

            ACCEPT A.
            ADD 10 A TO R.
            DISPLAY '10 + ' A '=' R.
            MOVE 20 TO R.
            SUBTRACT 10 A FROM R.
            DISPLAY '10 - ' A '=' R.
            MOVE 10 TO R.
            MULTIPLY A BY R.
            DISPLAY A ' * 10 = ' R.
            DIVIDE A BY 2 GIVING DIV.
            DISPLAY A ' / 2 =' DIV.
            MOVE 2 TO R.
            MULTIPLY A BY R GIVING DIV.
            DISPLAY A ' * 2 =' DIV.
            SUBTRACT A FROM 2 GIVING DIV.
            DISPLAY A ' - 2 =' DIV.
            ADD A TO 2 GIVING DIV ON SIZE ERROR DISPLAY 'TOO MUCH'.
            DISPLAY A ' + 2 =' DIV.
            DIVIDE 4 BY 3 GIVING DIV.
            DISPLAY '4 / 3 = ' DIV.
            COMPUTE DIV ROUNDED = 4 / 3.
            DISPLAY '4 / 3 = ' DIV.
            COMPUTE DIV = A + 2.
            DISPLAY A ' + 2 = ' DIV.
            COMPUTE DIV = A - 2.
            DISPLAY A ' - 2 = ' DIV.
            COMPUTE DIV = A / 2.
            DISPLAY A ' / 2 = ' DIV.
            COMPUTE DIV = A * 2 ON SIZE ERROR DISPLAY 'NUM IS OVER'.
            DISPLAY A ' * 2 = ' DIV.
            EXIT PROGRAM.
      

COMPUTE 输出变量 ROUNDED = 运算表达式
ROUNDED 四舍五入和输出项定义有关系
如一输出项定义99.99,一输入项为12.356
则输出为12.36,小数后第三位向前进位
如一输出项定义99,一输入项为12.356
则输出为12,小数后第第一位向前进位

沒有留言: