cobol学习之六数值的运算

2019-04-15 16:49发布

首先来一段代码: 000010 IDENTIFICATION DIVISION. 000011 PROGRAM-ID. LESSON01-1. 000020 ENVIRONMENT DIVISION. 000030 DATA DIVISION. 000040 WORKING-STORAGE SECTION. 000050 77 N PIC 99. 000060 PROCEDURE DIVISION. 000070 ACCEPT N. 000080 ADD 3 TO N. 000081 DIVIDE 5 INTO N. 000082 SUBTRACT 2 FROM N. 000083 MULTIPLY 8 BY N. 000090 DISPLAY N. 000100 STOP RUN.
加法语句(ADD)
例:ADD A TO B. B=B+A 表示数据项A的值和数据项B的值相加,结果置于B中。
减法语句(SUBTRACT)
SUBTRACT Y FROM Z. Z=Z-Y
乘法语句(MULTIPLY)
例:
MULTIPLY  A  BY  B.  B=B*A
MULTIPLY  A  BY  B  GIVING C. C=A*B
除法语句(DIVIDE)
例:DIVIDE Z INTO X.  X=X/Z
DIVIDE 2 INTO Z  GIVING  X. X=Z/2


四种算术语句的小结
1.一个语句只能进行一种单一的运算,
  不能在一个语句中实现两种不同的运算.


2.加法和减法语句可以进行两个以上数值量的计算.


3.四种算术语句都有两种形式,
  即带有GIVING部分和不带GIVING.

另外:前面加上COMPUTE 就可以像其他语言一样进行数值运算 例如 COMPUTE A = Z / C. COMPUTE Q = ( A + B ) * D. 
000010 IDENTIFICATION DIVISION. 000020 PROGRAM-ID. STUDY3-103. 000030 ENVIRONMENT DIVISION. 000040 DATA DIVISION. 000050 WORKING-STORAGE SECTION. 000060 77 R PIC 9(2). 000070 77 L PIC 99V9. 000080 77 S PIC 9(5)V9(2). 000090 77 R-P PIC 9(2). 000100 77 L-P PIC 99.9. 000110 77 S-P PIC *(5).9(2). 000111 77 A PIC 9V99. 000120 PROCEDURE DIVISION. 000121 MOVE 3.14 TO A 000130 ACCEPT R. 000140 COMPUTE L = ( R * 2 ) * A. 000150 COMPUTE S = R * R * A . 000160 MOVE R TO R-P. 000170 MOVE L TO L-P. 000180 MOVE S TO S-P. 000190 DISPLAY 'R = ' R-P. 000200 DISPLAY 'L = ' L-P. 000210 DISPLAY 'S = ' S-P.