《COBOL算术运算.ppt》由会员分享,可在线阅读,更多相关《COBOL算术运算.ppt(28页珍藏版)》请在三一办公上搜索。
1、COBOL for Mainframe Fundamentals,Ch4 COBOL算术运算,算术运算语句,ADDSUBTRACTMULTIPLYDIVIDECOMPUTE,ADD 语句1,语法1:ADD identifier-1(or Literals),identifier-2,TO identifier-n,ROUNDEDON SIZE ERROR statement-1NOT ON SIZE ERROR statement-2END-ADD例子:ADD NUM1 TO NUM2.ADD NUM1,10 TO NUM2,NUM3.ADD NUM1,NUM2 TO NUM3ON SIZE
2、ERROR PERFORM 100-ERROR.,ADD 语句2,语法2:ADD identifier-1(or Literals),identifier-2,TO identifier-n GIVING identifier-m ROUNDEDON SIZE ERROR statement-1NOT ON SIZE ERROR statement-2END-ADD例子:ADD NUM1 TO NUM2 GIVING NUM3.ADD NUM1,NUM2 GIVING NUM3.ADD NUM1,10 TO NUM2 GIVING NUM3.,例子,ADD 语句3,语法3:ADD CORR i
3、dentifier-1 TO identifier-2,ROUNDEDON SIZE ERROR statement-1NOT ON SIZE ERROR statement-2END-ADD例子:03 CURRENT-MONTH.05 PAYPIC 9(3)V9(2).05 TAXPIC 9(2)V9(2).03 YTD.05 PAYPIC 9(3)V9(2).05 TAXPIC 9(2)V9(2).ADD CORR CURRENT-MONTH TO YTD.,ROUNDED,Receiving FieldActual ResultTruncated ResultRounded Result
4、 PIC 9(3)V9.123.45 123.4123.5 PIC 9(3).123.45 123 123,SIZE ERROR,Receiving FieldActual ResultSIZE ERROR PIC 9(3)V9.123.45 Y PIC 9(3)V9.1234.5Y PIC 9(3).123N PIC 9(3).1234Y PIC 9(3)V9 Not Rounded123.45 Y PIC 9(3)V9 Rounded123.45N PIC 9(3)V9 Rounded1234.56Y,SUBTRACT 语句1,语法1:SUBTRACT identifier-1(or Li
5、terals),identifier-2,FROM identifier-n,ROUNDEDON SIZE ERROR statement-1NOT ON SIZE ERROR statement-2END-SUBTRACT例子:SUBTRACT NUM1 FROM NUM2.,SUBTRACT 语句2,语法2:SUBTRACT identifier-1(or Literals),identifier-2,FROM identifier-nGIVING identifier-mROUNDEDON SIZE ERROR 语句-1NOT ON SIZE ERROR 语句-2END-SUBTRACT
6、例子:SUBTRACT NUM1 FROM NUM2 GIVING NUM3.,例子:,SUBTRACT 语句3,语法3:SUBTRACT CORR identifier-1 FROM identifier-2,ROUNDEDON SIZE ERROR statement-1NOT ON SIZE ERROR statement-2END-SUBTRACT例子:SUBTRACT CORR REC1 FROM REC2.,MULTIPLY 语句1,语法1:MULTIPLY identifier-1(or Literals),BY identifier-2,ROUNDEDON SIZE ERROR
7、 statement-1NOT ON SIZE ERROR statement-2END-MULTIPLY例子:MULTIPLY NUM1 BY NUM2.MULTIPLY 100 by num.,MULTIPLY 语句2,语法2:MULTIPLY identifier-1(or Literals),BY identifier-2 GIVING identifier-3ROUNDEDON SIZE ERROR statement-1NOT ON SIZE ERROR statement-2END-MULTIPLY例子:MULTIPLY NUM1 BY NUM2 GIVING NUM3.,例子,
8、DIVIDE 语句1,语法1:DIVIDE identifier-1(or Literals)INTO identifier-2ROUNDEDON SIZE ERROR statement-1NOT ON SIZE ERROR statement-2END-DIVIDE例子:DIVIDE NUM1 INTO NUM2.,DIVIDE 语句2,语法2:DIVIDE identifier-1(or Literals)INTO identifier-2GIVING identifier-3ROUNDEDON SIZE ERROR statement-1NOT ON SIZE ERROR statem
9、ent-2END-DIVIDE例子:DIVIDE NUM1 INTO NUM2 GIVING NUM3.,DIVIDE 语句3,语法3:DIVIDE identifier-1(or Literals)BY identifier-2(or Literals)GIVING identifier-3ROUNDEDON SIZE ERROR statement-1NOT ON SIZE ERROR statement-2END-DIVIDE例子:DIVIDE NUM1 BY NUM2 GIVING NUM3.,DIVIDE 语句4,语法4:DIVIDE identifier-1(or Literals
10、)INTO identifier-2(or Literals)GIVING identifier-3ROUNDEDREMAINDER identifier-4ON SIZE ERROR statement-1NOT ON SIZE ERROR statement-2END-DIVIDE例子:DIVIDE NUM1 INTO NUM2 GIVING NUM3 REMAINDER NUM4.,DIVIDE 语句5,语法5:DIVIDE identifier-1(or Literals)BY identifier-2(or Literals)GIVING identifier-3ROUNDEDREM
11、AINDER identifier-4ON SIZE ERROR statement-1NOT ON SIZE ERROR statement-2END-DIVIDE例子:DIVIDE NUM1 BY NUM2 GIVING NUM3 REMAINDER NUM4.,例子,COMPUTE 语句,语法:COMPUTE identifier-1 ROUNDED,identifier-2 ROUNDED,=arithmetic-expressionON SIZE ERROR statement-1NOT ON SIZE ERROR statement-2END-COMPUTE例子:COMPUTE R
12、ESULT=NUM1*NUM2+NUM3-NUM4.COMPUTE PRICE=UNITPRICE/RATE*100.,算术表达式符号,代数符号:+,-乘方:*乘法:*除法:/加、减:+,-,内置函数,语法:FUNCTION function-name(argument1,argument2,.)例子:MOVE FUNCTION SUM(X,Y,Z)TO SUM-XYZ.,日期函数,例子 结果FUNCTION CURRENT-DATE 2005100709052100+0800FUNCTION DATE-OF-INTEGER(145732)20000101FUNCTION DAY-OF-INT
13、EGER(145732)2000001FUNCTION INTEGER-OF-DATE(19980721)145203FUNCTION INTEGER-OF-DAY(1998054)145055FUNCTION WHEN-COMPILED 200510070905210000000,算术函数,例子结果FUNCTION ACOS(0.717)0.771307 FUNCTION LOG10(10)1.000000FUNCTION SUM(17,21,4.5,-6)36.5FUNCTION SQRT(11)3.316625FUNCTION FACTORIAL(4)24FUNCTION INTEGER
14、(4.5)4FUNCTION INTEGER(-3.5)-4FUNCTION INTEGER-PART(4.5)4FUNCTION INTEGER-PART(-3.5)-3FUNCTION REM(11.0,5)1.0FUNCTION RANDOM0.773515,算术函数,例子结果FUNCTION CHAR(65)AFUNCTION LENGTH(ABC9AX)6FUNCTION LOWER-CASE(ABC)abcFUNCTION UPPER-CASE(abc)ABCFUNCTION NUMVAL(+709.)709.0FUNCTION NUMVAL(52 DB)-52.0FUNCTION NUMVAL-C(+$709.50)709.50FUNCTION ORD(A)65FUNCTION REVERSE(ABCD)DCBA,范围函数,例子结果FUNCTION MAX(-21,15,2.4)15FUNCTION MIN(-21,15,2.4)-21FUNCTION ORD-MAX(-21,15,2.4)2FUNCTION ORD-MIN(-21,15,2.4)1FUNCTION RANGE(11,2,7,3,1)10,