《COBOL程序语言.ppt》由会员分享,可在线阅读,更多相关《COBOL程序语言.ppt(84页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、COBOL语言程序设计许毅第二章 COBOL程序设计初步 v本章包括下面一些COBOL基本规则的介绍:变量,结构,字符型和常量值的分配和显示基本的运算语句2.1 变量,组合项,字符型和常量的使用 v大多数高级编程语言对代表变量,或组合项,字符和常量的数据的定义是类似的。v在COBOL程序中数据可以是字母,字符和数字,包括双字节的数据集(DBCS),国际化的字符或数字。v也可以定义索引名字和被“USAGE POINTER,USAGE FUNCTION-POINTER,USAGE PROCEDURE-POINTER,或USAGE OBJECT REFERENCE 等诸如指针、函数指针以及对象引用等
2、所描述的数据项。vCOBOL程序将所有数据定义放到程序的“DATA DIVISION”中 1、变量变量v变量也被称为数据项,在一个程序中它的值的是可以改变的。数据项在定义时应给定其名字、内存中的长度和类型。v例如,如果在你的程序中,一个顾客的名字是一个由字母和数字组成的数据项,你可以这样编码:DATA DIVISION.01 CUSTOMER-NAME PIC X(20).01 ORIGINAL-CUSTOMER-NAME PIC X(20).PROCEDURE DIVISION.MOVE CUSTOMER-NAME TO ORIGINAL-CUSTOMER-NAME.2、变量、数据项和组合项
3、、变量、数据项和组合项v相关的数据项可能是一个层次数据结构的某些部分。在第一章提过,没有任何子数据项的一个数据项被称为基本项(初等项)。由一个或多个子数据项组成的数据项被称为一个组合项。一个组合项可以是一个包含字母数字的组合项。DATA DIVISION.FILE SECTION.FD CUSTOMER-FILE RECORD CONTAINS 45 CHARACTERS.01 CUSTOMER-RECORD.05 CUSTOMER-NAME.10 LAST-NAME PIC X(17).10 FILLER PIC X.10 INITIALS PIC XX.05 PART-ORDER.10 P
4、ART-NAME PIC X(15).10 PART-COLOR PIC X(10).WORKING-STORAGE SECTION.01 ORIG-CUSTOMER-NAME.05 SURNAME PIC X(17).05 INITIALS PIC X(3).01 INVENTORY-PART-NAME PIC X(15).PROCEDURE DIVISION.MOVE CUSTOMER-NAME TO ORIG-CUSTOMER-NAME MOVE PART-NAME TO INVENTORY-PART-NAME.3、常量v常量是一个数值或其他字符构成的序列,它的值就是被赋给的字符序列本身
5、。我们可以在PROCEDURE DIVISION中用一个常量代表数据的值。例如:vMOVE“NAME IS NOT VALID”TO CUSTOMER-NAME v还可以和一个数字常量进行比较,例如:01 PART-NUMBER PIC 9(5).IF PART-NUMBER=03519 THEN DISPLAY“PART NUMBER WAS FOUND”.v这个例子中,“NAME IS NOT DISPLAY”是一个字符常量,03519是一个数值常量。4、表意常量、表意常量v作为保留字来提供的确定的、通用的常量称为表意常量:ZERO,SPACE,HIGH-VALUE,LOW-VALUE,Q
6、UOTE,NULL,和ALL字。因为他们代表固定值,表意常量不需要在数据部定义。v 例如:Move Spaces To Report-Header 2.2 过程部初步 过程部的几个特点:v一、过程部是程序中的第四部分,它以部头PROCEDURE DIVISION开头。部头从A区(第811列)开始书写。过程部下面分若干节,节下面分段,段由若干句子组成,句子以句号“”和空格结束。句子由若干语句组成,语句间可用分号和空格或用空格分隔。PROCEDURE DIVISION.(过程部头)A SECTION.(A节)A1.MOVE 1 TO I (A1段)MOVE 2 TO J.A2.ADD I TO J
7、.(A2段)B SECTION.(B节)v 二、过程部的语句都以一个动词(verb)开始,如MOVE,OPEN,READ,WRITE,IF(IF在COBOL中也作为动词对待)。它表示计算机应执行的操作。如:MOVE 100 TO X.ADD Y TO Z.IF X 100 DISPLAY X END-IF.IF X 100 DISPLAY X.v三、语句中的动词后面一般要跟一个操作的对象。操作对象可以是数据名或文件名。如:MOVE X TO Y表示将X的值送到Y去。操作的对象是数据项,它们是内存中某一段单元中的内容。READ,OPEN,CLOSE等语句的操作对象是外部文件,在这些动词后跟的是文
8、件名,表示对与文件名相联系的外部文件进行某种操作。如OPEN ABC,CLOSE ABC,READ ABC等。ABC是文件名。v四、过程部的语句一律从B区开始书写即从12列以后开始书写。一个语句可以任意写在一行或几行上。续行也应从B区开始。2.3 基本输入输出语句v1、接收语句ACCEPTACCEPT 标识符标识符 FROME 助记名助记名(1)标识符(identifier)必须唯一标识一个数据项的数据名,不能唯一标识一个数据项的不是标识符vADD C OF B1 TO D OF B1.vADD C OF B2 TO D OF B2.ACCEPT语句后面只能跟一个标识符ACCEPT将中断程序的
9、运行等待输入,会影响程序的速度v2、显示语句DISPLAYDISPLAY T.DISPLAY T1,T2.DISPLAY TODAY IS SUNDAY.vUPON表示在什么设备上显示数据。没有UPON则在隐含设备上显示。v如果为了将数据写到目的位置而不是写到系统的逻辑输出上,需要使用带有一个目的位置的UPON子句而不是SYSOUT。例如,在Z/OS中下面的语句往SYSPUNCH DD语句中指定的文件中写入数据。vDisplay“Hello”upon syspunch.v3、使用ACCEPT获取系统日期和时间vACCEPT 标识符 FROM requestvrequest可以为如下取值:TIM
10、E,这时返回一形为9(8)含义为hhmmsstt,其中hh表示小时(00-23),mm表示分钟(00-59),ss表示秒(00-59),tt表示毫秒(00-99)DATE,这时返回一形如S9(6)含义为yymmdd,yy表示年,mm表示月,dd表示日。注意这里年用的是两位数表示,当年千年虫的原因之一。DAY,这时返回一形如S9(5)含义为yyddd,yy表示年,ddd表示这时今年的第几天(001-366)。年份表示和上面相同。DAY-OF-WEEK,这时返回一形如S9(1),用1-7表示今天是星期几。v消灭千年虫(子程序调用)2.4 算术运算语句v1、加法语句ADD加法语句举例:ADD A T
11、O B 表示:A+B=BADD 15 TO C 15+C=CADD A,B TO C A+B+C=CADD 15,25 TO C 15+25+C=CADD A,B GIVING C A+B=CADD 15,25 GIVING T 15+25=TADD A,B TO C,D A+B+C=C;A+B+D=D(1)ADD A TO B.ADD A,B TO C.(2)ADD A,B GIVING C.(3)ADD A,B TO C,D.注意vTO和GIVING后面只能跟数据名vTO前后的数据名的次序vGIVING后面可以跟几个数据名v参加运算的只能是数值量,其值不能超过限制。加法语句的一般格式v2、
12、减法语句SUBTRACT减法语句举例:SUBTRACT B FROM A.A-B=ASUBTRACT B,C FROME A.A-B-C=ASUBTRACT B,C FROM A,T A-B-C=A T-B-C=T SUBTRACT B,C FROM A GIVING X.A-B-C=Xv注意1、2、3和4的区别,4中A的值不发生变化GIVING后面不能跟常量若不含GIVING部分,则FROM后面也不能跟常量减法语句的一般格式v3、乘法语句MULTIPLYMULTIPLY A BY B.AB=BMULTIPLY 0.5 BY B.0.5B=BMULTIPLY A BY B GIVING C.A
13、B=C MULTIPLY 1.5 BY 3 GIVING C,A.1.53=C 1.53=AMULTIPLY A BY B,C AB=B AC=C注意:v当不带GIVING时,BY后面不能跟常量v带GIVING部分时,BY后面可以是常数,而GIVING后面不能是常量乘法语句的一般格式v4、除法语句DIVIDE几个示例DIVIDE A INTO B.B/A=BDIVIDE A INTO B GIVING C.B/A=CDIVIDE A BY B GIVING C.A/B=C注意INTO 和 BY 的区别!除法语句的一般格式允许GIVING后面跟几个标识符vDIVIDE A BY B GIVING
14、 C,D,EvA/B=C=D=E如果除不尽,则将多余的位数截去注意下面的错误用法:vDIVIDE A BY B.语句不完整vDIVIDE A BY 0 GIVING C.被0除vDIVIDE A INTO 3.常量v(一)一个语句只能进行一种单一运算v(二)加减法可以进行两个以上的数值量运算,但是乘除法只能在两个量之间进行v(三)GIVING的存在与否将四种运算分成两种类型v5、四种算术语句小结例 2-4 IDENTIFICATION DIVISION.PROGRAM-ID.EX2A4.ENVIRONMENT DIVISION.DATA DIVISION.WORKING-STORAGE SEC
15、TION.77 N PIC 99.PROCEDURE DIVISION.S.ACCEPT N.ADD 3 TO N.DIVIDE 5 INTO N.SUBTRACT 2 FROM N.MULTIPLY 8 BY N.DISPLAY N.STOP RUN.v6、计算语句COMPUTEv(一)算术表达式由算术初等量(数值常量、数值型数据项)、算术运算符、括号所钩针的式子。算术表达式中的运算次序(优先级)括号()高单目运算符(正负号)乘方(*)乘(*)、除(/)加(+)、减(-)低v(二)计算语句的一般形式COMPUTE 标识符1,标识符2=算术表达式如:vCOMPUTE A=B*2+C*X.vCO
16、MPUTE T=A.vCOMPUTE P=3.vCOMPUTE A,B=3*5.例 2.5 IDENTIFICATION DIVISION.PROGRAM-ID.EXAM2A5.ENVIRONMENT DIVISION.DATA DIVISION.WORKING-STORAGE SECTION.77 X PIC 9999.77 A PIC 99.77 B PIC 99.77 C PIC 99.PROCEDURE DIVISION.S.ACCEPT A.ACCEPT B.ACCEPT C.COMPUTE X=(A+B)*2/C.DISPLAY X.STOP RUN.2.5 传送语句MOVEv1、
17、传送语句的作用相当于其他语言的赋值语句,MOVE语句实现数据的传送v2、传送规则MOVE A TO B如果接收和发送项在数据部中描述的类型和长度相同,按字节一一对应传送如果接收项与发送项长度不相同,而二者都是数值数据项,则按“小数点对齐”原则处理。如果是整数,则认为小数点在最后一位数字之后。如接收项长度大于发送项,则多余位补零。如接收项长度小于发送项,则产生截断。对字母或字符数据(非数值型数据)的传送,按左对齐原则处理。如接收项长度长于发送项的长度则多余位置填空格,如接收项的长度小于发送项的长度,则从右端截断MOVE语句可以将一初等项内容传送给另一初等数据项,也可以将一组合项内容传送给一初等数
18、据项,也可以将一初等项内容传送给一组合项。MOVE A TO BMOVE A1 TO B1MOVE A2 TO B2MOVE A3 TO B3MOVE A1 TO BMOVE A TO B1以上语句都是合法语句。若A是组合项,T是初等项,下图传送仍然合法v3、MOVE语句的一般格式v示例:2.6 条件语句IF分支结构IF AMOUNT IS GREATER THAN 2000 THEN DISPLAY AMOUNTEND-IFIF A=0 DISPLAY A.v1、关系运算符v2、关系运算规则数值之间的比较字母型数据之间的 比较(字典顺序由小到大)字符型数据的比较v字符代码vASCII vs.
19、EBCDICvASCII A9vEBCDIC A9PROCEDURE DIVISION.Calculator.ACCEPT Num1.ACCEPT Num2.ACCEPT Operator IF Operator=+THEN ADD Num1,Num2 GIVING Result END-IF IF Operator=*THEN MULTIPLY Num1 BY Num2 GIVING Result END-IF.DISPLAY Result is=,Result.STOP RUN.v3、IF语句的第二种形式vIF 条件 语句组1 ELSE 语句组2IF QUANTITY 1000MULTIT
20、Y 0.99 BY PRICEELSE MULTITY 0.97 BY PRICEEND-IFMULTIPLY PRICE BY QUANTITY GIVING TOTAL.v在COBOL-85以前,没有END-IF,上面的示例如下:IF QUANTITY 1000MULTITY 0.99 BY PRICEELSE MULTITY 0.97 BY PRICE.MULTIPLY PRICE BY QUANTITY GIVING TOTAL.复习:句子(Sentence)和语句(Statement)v(1)MOVE A TO BMOVE C TO DMOVE E TO F.v(2)MOVE A T
21、O B.MOVE C TO D.MOVE E TO F.2.7 PERFOM语句v在COBOL程序中构建重复结构最方便的办法就是使用PERFORM语句。PERFORM是用来构建其他语言常见的WHILE或者REPEAT结构PERFORM WITH TEST BEFORE|AFTER UNTIL 测试条件 循环体END-PERFORM2.8 转移语句 GO TOvGO TO语句改变程序的正常执行程序,例如:A1.ACCEPT B.ACCEPT C.ADD B TO C.A2.DISPLAY C.GO TO A1.vGO TO语句的一般格式GO TO 过程名GO TO 过程名1,过程名2过程名n D
22、EPENDING ON 标识符第二种格式的GO TO语句根据标识符的值决定转移到不同的过程名处,如:v GO TO A,B,C,D,E DEPENDING ON IvI=1时转移到A,I=2时转移到B,以此类推。如果I的值没有对应的过程,则顺序往下执行。例 2.7例 2.7 IDENTIFICATION DIVISION.PROGRAM-ID.EXAM2A7.ENVIRONMENT DIVISION.DATA DIVISION.WORKING-STORAGE SECTION.77 M PIC 999.77 I PIC 9.77 AMOUNT PIC 9999.PROCEDURE DIVISIO
23、N.S.ACCEPT M.DIVIDE 50 INTO M GIVING I.ADD 1 TO I.GO TO A,B,C,D,E DEPENDING I.A.COMPUTE AMOUNT=10*M.GO TO F.B.COMPUTE AMOUNT=9*M.GO TO F.C.COMPUTE AMOUNT=8*M.GO TO F.D.COMPUTE AMOUNT=7*M.GO TO F.E.COMPUTE AMOUNT=6*M.F.DISPLAY M,AMOUNT.STOP RUN.2.9 停止语句STOPvSTOP RUNvSTOP 常数补充:结构化程序设计要点vPrograms are w
24、ritten using three main programming constructs.vSequence.vSelection.vIteration.Programs are written using three Programs are written using three main programming constructsmain programming constructs.Num1 Num2 Result OperatorIDENTIFICATION DIVISION.PROGRAM-ID.Selection-ProgramSelection-Program.AUTHO
25、R.Michael Coughlan.DATA DIVISION.WORKING-STORAGE SECTION.01 Num1 PIC 9 VALUE ZEROS.01 Num2 PIC 9 VALUE ZEROS.01 Result PIC 99 VALUE ZEROS.01 Operator PIC X VALUE SPACE.PROCEDURE DIVISION.CalculatorCalculator.ACCEPT Num1.ACCEPT Num2.ACCEPT Operator IF Operator=+THEN ADD Num1,Num2 GIVING Result END-IF
26、 IF Operator=*THEN MULTIPLY Num1 BY Num2 GIVING Result END-IF.DISPLAY Result is=,Result.STOP RUN.0000DATA_IDENTIFICATION DIVISION.PROGRAM-ID.Selection-Program.AUTHOR.Michael Coughlan.DATA DIVISION.WORKING-STORAGE SECTION.01 Num1 PIC 9 VALUE ZEROS.01 Num2 PIC 9 VALUE ZEROS.01 Result PIC 99 VALUE ZERO
27、S.01 Operator PIC X VALUE SPACE.PROCEDURE DIVISION.Calculator.ACCEPT Num1 ACCEPT Num1.ACCEPT Num2.ACCEPT Operator IF Operator=+THEN ADD Num1,Num2 GIVING Result END-IF IF Operator=*THEN MULTIPLY Num1 BY Num2 GIVING Result END-IF.DISPLAY Result is=,Result.STOP RUN.Num1 Num2 Result Operator7000DATA_IDE
28、NTIFICATION DIVISION.PROGRAM-ID.Selection-Program.AUTHOR.Michael Coughlan.DATA DIVISION.WORKING-STORAGE SECTION.01 Num1 PIC 9 VALUE ZEROS.01 Num2 PIC 9 VALUE ZEROS.01 Result PIC 99 VALUE ZEROS.01 Operator PIC X VALUE SPACE.PROCEDURE DIVISION.Calculator.ACCEPT Num1.ACCEPT Num2ACCEPT Num2.ACCEPT Opera
29、tor IF Operator=+THEN ADD Num1,Num2 GIVING Result END-IF IF Operator=*THEN MULTIPLY Num1 BY Num2 GIVING Result END-IF.DISPLAY Result is=,Result.STOP RUN.Num1 Num2 Result Operator7300DATA_IDENTIFICATION DIVISION.PROGRAM-ID.Selection-Program.AUTHOR.Michael Coughlan.DATA DIVISION.WORKING-STORAGE SECTIO
30、N.01 Num1 PIC 9 VALUE ZEROS.01 Num2 PIC 9 VALUE ZEROS.01 Result PIC 99 VALUE ZEROS.01 Operator PIC X VALUE SPACE.PROCEDURE DIVISION.Calculator.ACCEPT Num1.ACCEPT Num2.ACCEPT OperatorACCEPT Operator IF Operator=+THEN ADD Num1,Num2 GIVING Result END-IF IF Operator=*THEN MULTIPLY Num1 BY Num2 GIVING Re
31、sult END-IF.DISPLAY Result is=,Result.STOP RUN.Num1 Num2 Result Operator7300DATA+IDENTIFICATION DIVISION.PROGRAM-ID.Selection-Program.AUTHOR.Michael Coughlan.DATA DIVISION.WORKING-STORAGE SECTION.01 Num1 PIC 9 VALUE ZEROS.01 Num2 PIC 9 VALUE ZEROS.01 Result PIC 99 VALUE ZEROS.01 Operator PIC X VALUE
32、 SPACE.PROCEDURE DIVISION.Calculator.ACCEPT Num1.ACCEPT Num2.ACCEPT Operator IF Operator=+THEN IF Operator=+THEN ADD Num1,Num2 GIVING Result END-IFEND-IF IF Operator=*THEN MULTIPLY Num1 BY Num2 GIVING Result END-IF.DISPLAY Result is=,Result.STOP RUN.Num1 Num2 Result Operator7300DATA+IDENTIFICATION D
33、IVISION.PROGRAM-ID.Selection-Program.AUTHOR.Michael Coughlan.DATA DIVISION.WORKING-STORAGE SECTION.01 Num1 PIC 9 VALUE ZEROS.01 Num2 PIC 9 VALUE ZEROS.01 Result PIC 99 VALUE ZEROS.01 Operator PIC X VALUE SPACE.PROCEDURE DIVISION.Calculator.ACCEPT Num1.ACCEPT Num2.ACCEPT Operator IF Operator=+THEN AD
34、D Num1,Num2 GIVING ResultADD Num1,Num2 GIVING Result END-IF IF Operator=*THEN MULTIPLY Num1 BY Num2 GIVING Result END-IF.DISPLAY Result is=,Result.STOP RUN.Num1 Num2 Result Operator7310DATA+IDENTIFICATION DIVISION.PROGRAM-ID.Selection-Program.AUTHOR.Michael Coughlan.DATA DIVISION.WORKING-STORAGE SEC
35、TION.01 Num1 PIC 9 VALUE ZEROS.01 Num2 PIC 9 VALUE ZEROS.01 Result PIC 99 VALUE ZEROS.01 Operator PIC X VALUE SPACE.PROCEDURE DIVISION.Calculator.ACCEPT Num1.ACCEPT Num2.ACCEPT Operator IF Operator=+THEN ADD Num1,Num2 GIVING Result END-IF IF Operator=*THENIF Operator=*THEN MULTIPLY Num1 BY Num2 GIVI
36、NG Result END-IFEND-IF.DISPLAY Result is=,Result.STOP RUN.Num1 Num2 Result Operator7310DATA+IDENTIFICATION DIVISION.PROGRAM-ID.Selection-Program.AUTHOR.Michael Coughlan.DATA DIVISION.WORKING-STORAGE SECTION.01 Num1 PIC 9 VALUE ZEROS.01 Num2 PIC 9 VALUE ZEROS.01 Result PIC 99 VALUE ZEROS.01 Operator
37、PIC X VALUE SPACE.PROCEDURE DIVISION.Calculator.ACCEPT Num1.ACCEPT Num2.ACCEPT Operator IF Operator=+THEN ADD Num1,Num2 GIVING Result END-IF IF Operator=*THEN MULTIPLY Num1 BY Num2 GIVING Result END-IF.DISPLAY Result is=,Result DISPLAY Result is=,Result.STOP RUN.Num1 Num2 Result Operator7310DATA+IDE
38、NTIFICATION DIVISION.PROGRAM-ID.Iteration-ProgramIteration-Program.AUTHOR.Michael Coughlan.DATA DIVISIONDATA DIVISION.WORKING-STORAGE SECTION.01 Num1 PIC 9 VALUE ZEROS.01 Num2 PIC 9 VALUE ZEROS.01 Result PIC 99 VALUE ZEROS.01 Operator PIC X VALUE SPACE.PROCEDURE DIVISION.Calculator.PERFORM 5 TIMES A
39、CCEPT Num1 ACCEPT Num2 ACCEPT Operator IF Operator=+THEN ADD Num1,Num2 GIVING Result END-IF IF Operator=*THEN MULTIPLY Num1 BY Num2 GIVING Result END-IF DISPLAY Result is=,Result END-PERFORM.STOP RUN.DATANum1 Num2 Result Operator0000DATA_IDENTIFICATION DIVISION.PROGRAM-ID.Iteration-Program.AUTHOR.Mi
40、chael Coughlan.DATA DIVISION.WORKING-STORAGE SECTION.01 Num1 PIC 9 VALUE ZEROS.01 Num2 PIC 9 VALUE ZEROS.01 Result PIC 99 VALUE ZEROS.01 Operator PIC X VALUE SPACE.PROCEDURE DIVISION.Calculator.PERFORM 5 TIMES PERFORM 5 TIMES ACCEPT Num1 ACCEPT Num2 ACCEPT Operator IF Operator=+THEN ADD Num1,Num2 GI
41、VING Result END-IF IF Operator=*THEN MULTIPLY Num1 BY Num2 GIVING Result END-IF DISPLAY Result is=,Result END-PERFORM END-PERFORM.STOP RUN.DATANum1 Num2 Result Operator0000DATA_IDENTIFICATION DIVISION.PROGRAM-ID.Iteration-Program.AUTHOR.Michael Coughlan.DATA DIVISION.WORKING-STORAGE SECTION.01 Num1
42、PIC 9 VALUE ZEROS.01 Num2 PIC 9 VALUE ZEROS.01 Result PIC 99 VALUE ZEROS.01 Operator PIC X VALUE SPACE.PROCEDURE DIVISION.Calculator.PERFORM 5 TIMES ACCEPT Num1 ACCEPT Num1 ACCEPT Num2 ACCEPT Operator IF Operator=+THEN ADD Num1,Num2 GIVING Result END-IF IF Operator=*THEN MULTIPLY Num1 BY Num2 GIVING
43、 Result END-IF DISPLAY Result is=,Result END-PERFORM.STOP RUN.DATANum1 Num2 Result Operator5000DATA_IDENTIFICATION DIVISION.PROGRAM-ID.Iteration-Program.AUTHOR.Michael Coughlan.DATA DIVISION.WORKING-STORAGE SECTION.01 Num1 PIC 9 VALUE ZEROS.01 Num2 PIC 9 VALUE ZEROS.01 Result PIC 99 VALUE ZEROS.01 O
44、perator PIC X VALUE SPACE.PROCEDURE DIVISION.Calculator.PERFORM 5 TIMES ACCEPT Num1 ACCEPT Num2ACCEPT Num2 ACCEPT Operator IF Operator=+THEN ADD Num1,Num2 GIVING Result END-IF IF Operator=*THEN MULTIPLY Num1 BY Num2 GIVING Result END-IF DISPLAY Result is=,Result END-PERFORM.STOP RUN.DATANum1 Num2 Re
45、sult Operator5300DATA_IDENTIFICATION DIVISION.PROGRAM-ID.Iteration-Program.AUTHOR.Michael Coughlan.DATA DIVISION.WORKING-STORAGE SECTION.01 Num1 PIC 9 VALUE ZEROS.01 Num2 PIC 9 VALUE ZEROS.01 Result PIC 99 VALUE ZEROS.01 Operator PIC X VALUE SPACE.PROCEDURE DIVISION.Calculator.PERFORM 5 TIMES ACCEPT
46、 Num1 ACCEPT Num2 ACCEPT Operator ACCEPT Operator IF Operator=+THEN ADD Num1,Num2 GIVING Result END-IF IF Operator=*THEN MULTIPLY Num1 BY Num2 GIVING Result END-IF DISPLAY Result is=,Result END-PERFORM.STOP RUN.DATANum1 Num2 Result Operator5300DATA*IDENTIFICATION DIVISION.PROGRAM-ID.Iteration-Progra
47、m.AUTHOR.Michael Coughlan.DATA DIVISION.WORKING-STORAGE SECTION.01 Num1 PIC 9 VALUE ZEROS.01 Num2 PIC 9 VALUE ZEROS.01 Result PIC 99 VALUE ZEROS.01 Operator PIC X VALUE SPACE.PROCEDURE DIVISION.Calculator.PERFORM 5 TIMES ACCEPT Num1 ACCEPT Num2 ACCEPT Operator IF Operator=+THEN IF Operator=+THEN ADD
48、 Num1,Num2 GIVING Result END-IFEND-IF IF Operator=*THEN MULTIPLY Num1 BY Num2 GIVING Result END-IF DISPLAY Result is=,Result END-PERFORM.STOP RUN.DATANum1 Num2 Result Operator5300DATA*IDENTIFICATION DIVISION.PROGRAM-ID.Iteration-Program.AUTHOR.Michael Coughlan.DATA DIVISION.WORKING-STORAGE SECTION.0
49、1 Num1 PIC 9 VALUE ZEROS.01 Num2 PIC 9 VALUE ZEROS.01 Result PIC 99 VALUE ZEROS.01 Operator PIC X VALUE SPACE.PROCEDURE DIVISION.Calculator.PERFORM 5 TIMES ACCEPT Num1 ACCEPT Num2 ACCEPT Operator IF Operator=+THEN ADD Num1,Num2 GIVING Result END-IF IF Operator=*THEN IF Operator=*THEN MULTIPLY Num1 B
50、Y Num2 GIVING Result END-IFEND-IF DISPLAY Result is=,Result END-PERFORM.STOP RUN.DATANum1 Num2 Result Operator5300DATA*IDENTIFICATION DIVISION.PROGRAM-ID.Iteration-Program.AUTHOR.Michael Coughlan.DATA DIVISION.WORKING-STORAGE SECTION.01 Num1 PIC 9 VALUE ZEROS.01 Num2 PIC 9 VALUE ZEROS.01 Result PIC