COBOL程序设计—9

合集下载

COBOL(Common Business Oriented Language)通用商业语言

COBOL(Common Business Oriented Language)通用商业语言

第一章COBOL 概述一、COBOL 的语言特点:COBOL:Common Business Oriented Language通用商业语言它是专门为数据管理而设计的计算机高级语言。

COBOL语言的主要特点:1、适用于数据处理,(科学计算、数据处理、过程控制、计算机辅助设计、人工智能等)数据处理的特点:算术计算量多而逻辑处理少,输入输出量大;数据项之间有清晰的层次关系;大量的分类排序;对打印报表的高要求及多样化等等2、通用性强使用是要注意它的特殊性,COBOL有较高的标准化程序硬件集中管理不同厂家生产的计算机系统所提供的COBOL是COBOL 的标准的全集或子集,设备的逻辑名可能不一样。

3、机构严谨、层次分明、比较接近于自然语言,便于初学者模仿。

二、COBOL的发展概况1959年12月在美国提出了世界上第一个COBOL语言文本并于1960年4月由美国政府印刷局正式发表称之为COBOL—60→COBOL61→COBOL65ANSI标准(美国国家标准化协会)ANSI COBOL68->(修订扩充)-〉ANSI COBOL-74-〉ANSI COBOL-85ISO 标准(国际标准化组织)ISO COBOL-72->ISO COBOL-78三、COBOL源程序结构及书写格式源程序示例:例(P8)将A和B的值相加其和放在B中源程序的结构部(DIVISION):标识部、环境部、数据部、过程部以部头开始以“.”和空格结束节(SECTION):每个节以节头作标识,以“.”和空格结束如WORKING_STORAGE SECTION (工作单元节)段(PARAGRAPH):每个短都有一个段名,以“”和空格结束如PROGRAM__ID EXAM2 句子(SENTENCE):用空格分开的多条语句称为句子最后以“.”和空格结束的一个语句序列语句(STA TEMENT):一条完整的指令用于完成一定的操作子句(CLAUSE):用于完成某一方面的特定功能如环境部中的SELECT子句。

基于COBOL的银行业务系统设计与系统性能调优

基于COBOL的银行业务系统设计与系统性能调优

基于COBOL的银行业务系统设计与系统性能调优COBOL(Common Business-Oriented Language)是一种面向商业应用的编程语言,广泛应用于银行、保险等金融领域。

在银行业务系统设计中,COBOL语言以其稳定性和可靠性成为首选。

本文将探讨基于COBOL的银行业务系统设计及系统性能调优的相关内容。

1. COBOL在银行业务系统中的应用COBOL作为一种古老的编程语言,却在银行业务系统中有着广泛的应用。

银行作为金融机构,其业务涉及到大量的数据处理、交易记录和报表生成等工作,这正是COBOL擅长的领域。

通过COBOL编写的银行业务系统,可以保证数据的准确性和稳定性,确保银行业务的正常运转。

2. 银行业务系统设计原则在设计基于COBOL的银行业务系统时,需要遵循一些原则:模块化设计:将系统拆分为多个模块,每个模块负责不同的功能,便于维护和扩展。

数据安全:对于涉及到客户隐私和资金安全的数据,需要进行严格的加密和权限控制。

事务处理:保证每笔交易都是原子操作,要么全部执行成功,要么全部失败,确保数据一致性。

性能优化:针对大量数据处理和复杂计算场景,需要进行系统性能调优,提高系统响应速度和并发处理能力。

3. COBOL程序设计范例以下是一个简单的COBOL程序设计范例,实现了一个简单的银行账户管理功能:示例代码star:编程语言:cobolIDENTIFICATION DIVISION.PROGRAM-ID. BANK-SYSTEM.DATA DIVISION.WORKING-STORAGE SECTION.01 ACCOUNT-NUMBER PIC 9(10).01 BALANCE PIC 9(10)V99 VALUE 0.01 CHOICE PIC X.PROCEDURE DIVISION.MAIN-PROCEDURE.DISPLAY "1. Deposit".DISPLAY "2. Withdraw".DISPLAY "3. Check Balance".DISPLAY "4. Exit".ACCEPT CHOICE.PERFORM PROCESS-CHOICE UNTIL CHOICE = '4'. STOP RUN.PROCESS-CHOICE.IF CHOICE = '1' THENACCEPT ACCOUNT-NUMBER.ACCEPT BALANCE.ADD BALANCE TO BALANCE.ELSE IF CHOICE = '2' THENACCEPT ACCOUNT-NUMBER.ACCEPT BALANCE.SUBTRACT BALANCE FROM BALANCE.ELSE IF CHOICE = '3' THENDISPLAY "Balance: " BALANCE.END PROGRAM BANK-SYSTEM.示例代码end4. 系统性能调优在银行业务系统中,性能是至关重要的。

COBOL程序设计—6

COBOL程序设计—6
OneLevelDown. DISPLAY ">>>> Now in OneLevelDown" PERFORM TwoLevelsDown DISPLAY ">>>> Back in OneLevelDown".
Format 1 Example.
Run of PerformFormat1
In TopLevel. Starting to run program >>>> Now in OneLevelDown >>>>>>>> Now in TwoLevelsDown. >>>> Back in OneLevelDown Back in TopLevel.
OneLevelDown. DISPLAY ">>>> Now in OneLevelDown" PERFORM TwoLevelsDown DISPLAY ">>>> Back in OneLevelDown".
Format 1 Example.
Run of PerformFormat1
In TopLevel. Starting to run program
第六章 过程部之三
软件学院 代飞 2011.秋
6.1 PERFORM语句
在程序中,常常有一部分语句需要多次执行。我们该怎 么做?
例如分别计算P1=P(1+R)
A1. MOVE 0.06 TO R.
MOVE 1000 TO P.
COMPUTE P1 = P * (1 + R). WRITE P1 AFTER 2. A2. MOVE 0.04 TO R. MOVE 100 TO P. COMPUTE P1 = P * (1 + R). WRITE P1 AFTER 2.

COBOL语法和文法

COBOL语法和文法

COBOL语法和文法REDEFINES可以用不同的变量描述同一内存。

01 PART_RECODE USAGE DISPLAY03 PART-TYPE PICTURE...03 PART_TYPE_A.05 PART_NUMBER PICTURE...05 PART_COST PICTURE...03 PART_TYPE_B REDEFINES PART_TYPE_A.05 UPC_CODE PICTURE...03 PART_TYPE_C REDEFINES PART_TYPE_A.05 ISBN PICTURE...根据PART_TYPE决定用PART_TYPE_A还是PART_TYPE_B还是PART_TYPE_C。

规则:PART_TYPE_B和PART_TYPE_C长度不能大于PART_TYPE_A。

数据项不能有VALUE。

01层不能用REDEFINES.PART_TYPE_B和PART_TYPE_C中不能有OCCURS.PICTURE定义基本项目的具体长度,格式,数据类型。

可以简写成PIC.格式字符串最多30个字符,可以包含下列字符:A 字母A-Z,a-z,空格B 插入空格P 标量字符,代表字段中不显示的小数点位置S 代数符号,实际显示取决于项目的USAGEV 小数点位置X 任何字符Z 抑制前头的0并转换成空字符0 插入09 数字0~9/ 插入/, 插入,(逗号不能作为字符串的最后一位,最后时逗号是分隔符)。

插入。

* 抑制前头的0并转换成*+ 正值插入+,负值插入-- 正值插入空格,负值插入-$ 插入美元号CR 正值插入两个空格,负值插入CRDB 正值插入两个空格,负值插入DB例子:03 VAR PIC AAAAA. STATE变量长度为5个字符,不能有数字,等价于03 STATE PIC A(5)。

输入ASDFG ,显示ASDFG03 VAR PIC XXXXX. FILLER变量长度为5个任意字符。

COBOL培训教程-9

COBOL培训教程-9

表的建立
• 表的建立就是定义一个表。表名应在数据部中说 表的建立就是定义一个表。 明。 • 表元素可以是初等项,也可以是组合项。 表元素可以是初等项,也可以是组合项。 • 引用表元素是只需一个下标的,是一维表,需要 引用表元素是只需一个下标的,是一维表, 两个下标的则是二维表。 两个下标的则是二维表。 • 可以直接从数据部的描述看出:如果在数据项描 可以直接从数据部的描述看出: 述体中有一个OCCURS子句, OCCURS子句 述体中有一个OCCURS子句,而在它的上属数据项 的描述体中没有OCCURS子句,则它是一维表, OCCURS子句 的描述体中没有OCCURS子句,则它是一维表,反 则为二维表。当然还可以定义三维表。 之,则为二维表。当然还可以定义三维表。
给表元素赋初值(二) 给表元素赋初值(
• 例1: 01 TABLE VALUE IS ZERO. 03 A OCCURS 20 TIMES PIC 9(3). • 例2 : UNIT-PRICE03 UNIT-PRICE-TABLE. 05 FILLER PIC X(10) VALUE ‘0100001350’. 05 FILLER PIC X(10) VALUE ‘0200001420’. UNIT-PRICE-TABLE03 UNIT-PRICE-TABLE-R UNIT-PRICEREDEFINES UNIT-PRICE-TABLE. 05 TABLE OCCURS 2 TIMES. PROD07 PROD-CODE PIC X(4). PROD07 PROD-PRICE PIC X(6).
标识符1 [,标识符2]… 标识符1 [,标识符2]… SET 位标1 [, 位标2] 位标1 [, 位标2] TO 标识符3 标识符3 位标3 位标3 整数 标识符 整数

02.COBOL数据表示

02.COBOL数据表示

PIC X(8). PIC X(8). PIC XX. PIC XX. PIC 9(7). PIC 9(7). PIC X(4). PIC 9(4). PIC X(4). PIC 9(4). PIC X. PIC X.
PIC X(8). PIC X(8). PIC XX. PIC XX. PIC 9(7). PIC 9(7). PIC X(4). PIC 9(4). PIC X(4). PIC 9(4). PIC X. PIC X.
HIGH-VALUE or HIGH-VALUES = LOW-VALUE or LOW-VALUES NULL ALL = = =
数据名(Data Name)
数据名(Data name)和其他编程语言中的变量名相 似;它的值能够在程序中被改变。 数据名可以认为是由程序设计者定义的某一片内存区 的符号地址,即用一个符号代表一个地址。 数据名代表一个具体的数据项。COBOL语言中指的数 据是广义的,即可以是数值,也可以是字符。 要通过类型和取值范围来进行描述。 在COBOL 程序中用到的所有数据名都要的数据部 DATA DIVISION进行描述/声明。
数据名的命名规范
包括1-30个字符 可使用字母,数字和连字符。 至少包括一个字母 第一个和最后一个字符不能是连字符“-” 中间不能包含空格符
命名原则
取的名字尽量体现所表示数据的内容 使用前缀或后缀来体现是输入/输出数据 取名要简洁直观 避免使用非正式的/个人的名字 不能使用 COBOL的保留字
小练习:判断哪些是合法数据名?
术语“elementary item”是用来描述一个原子的,不能被继续划分的 数据项 一个 elementary item 必须要有一个 picture子句. Every group or elementary item declaration must be followed by a full stop.

COBOL程序设计语言入门教程

COBOL程序设计语言入门教程

COBOL程序设计语言入门教程第一章:COBOL简介COBOL,全称为“公共业务计算机编程语言”(Common Business-Oriented Language),是一种专门针对商业应用开发的高级程序设计语言。

本章节将为读者介绍COBOL的背景、特点以及应用领域。

1.1 COBOL的背景COBOL于1960年首次发布,由美国政府、大型企业和专业团体联合开发。

当时,计算机还是新兴的技术,大多数应用主要集中在科学计算领域。

为了适应商业应用的特殊需求,COBOL 应运而生。

1.2 COBOL的特点COBOL以易读性和易维护性著称。

它采用了自然语言风格的编码,使得非专业的程序员也能轻松理解和修改代码。

此外,COBOL还具有丰富的数据处理功能和强大的文件处理能力,适用于处理大量数据的商业应用。

1.3 COBOL的应用领域COBOL广泛应用于金融、保险、银行、物流以及政府行政等领域。

由于COBOL的稳定性和成熟性,一些大型企业仍在使用COBOL开发和维护核心业务系统。

第二章:COBOL基础语法在本章节中,我们将介绍COBOL的基本语法,包括程序结构、数据类型、变量定义、运算符等内容。

2.1 程序结构COBOL程序由多个部分组成,包括程序头、数据定义、主程序和辅助程序。

程序头包含程序名称、作者、日期等信息;数据定义定义所有的变量和文件;主程序包含实际的业务逻辑;辅助程序用于处理一些通用的功能。

2.2 数据类型和变量定义COBOL的数据类型包括数字、字符串、日期、逻辑等。

变量定义使用关键字“PIC”(Picture)来指定变量的类型和长度,例如“PIC 9(4)”表示一个4位数的整数变量。

2.3 运算符COBOL支持常见的算术运算符(加、减、乘、除)和逻辑运算符(与、或、非)。

此外,COBOL还提供了特殊的字符串连接运算符和条件运算符,用于处理字符串和条件语句。

第三章:COBOL程序设计在本章节中,我们将介绍COBOL程序的设计方法和常用技巧,包括文件处理、条件语句、循环结构以及子程序的使用。

COBOL编程基础教程:COBOL简介、应用场景和目标受众说明书

COBOL编程基础教程:COBOL简介、应用场景和目标受众说明书

About the T utorialCOBOL stands for Common Business-Oriented Language. The US Department of Defense, in a conference, formed CODASYL (Conference on Data Systems Language) to develop a language for business data processing needs which is now known as COBOL.COBOL is used for writing application programs and we cannot use it to write system software. The applications like those in defense domain, insurance domain, etc. which require huge data processing make extensive use of COBOL. AudienceThis tutorial is designed for software programmers who would like to learn the basics of COBOL. It provides enough understanding on COBOL programming language from where you can take yourself to a higher level of expertise. PrerequisitesBefore proceeding with this tutorial, you should have a basic understanding of computer programming terminologies and JCL. A basic understanding of any of the programming languages will help you understand the concepts of COBOL programming and move fast on the learning track.Copyright & DisclaimerCopyright 2014 by Tutorials Point (I) Pvt. Ltd.All the content and graphics published in this e-book are the property of Tutorials Point (I) Pvt. Ltd. The user of this e-book is prohibited to reuse, retain, copy, distribute or republish any contents or a part of contents of this e-book in any manner without written consent of the publisher.We strive to update the contents of our website and tutorials as timely and as precisely as possible, however, the contents may contain inaccuracies or errors. Tutorials Point (I) Pvt. Ltd. provides no guarantee regarding the accuracy, timeliness or completeness of our website or its contents including this tutorial. If you discover any errors on our website or inthistutorial,******************************************T able of ContentsAbout the Tutorial (i)Audience (i)Prerequisites (i)Copyright & Disclaimer (i)Table of Contents ...................................................................................................................................... i i 1. OVERVIEW . (1)Introduction to COBOL (1)Evolution of COBOL (1)Importance of COBOL (1)Features of COBOL (2)2. ENVIORNMENT SETUP (3)Installing COBOL on Windows/Linux (3)Compiling COBOL Programs (3)Executing COBOL Programs (4)Executing COBOL-DB2 programs (4)Try it Option Online (6)3. PROGRAM STRUCTURE (7)Divisions (8)4. BASIC SYNTAX (12)Character Set (12)Coding Sheet (13)Character Strings (14)Comment (14)Literal (16)COBOL Word (17)5. DATA TYPES (19)Data Name (19)Level Number (20)Picture Clause (21)Value Clause (22)6. BASIC VERBS (24)Input / Output Verbs (24)Initialize Verb (25)Move Verb (26)Legal Moves (28)Add Verb (28)Subtract Verb (30)Multiply Verb (32)Divide Verb (33)Compute Statement (34)7. DATA LAYOUT (36)Redefines Clause (36)Renames Clause (37)Usage Clause (38)Copybooks (40)8. CONDITIONAL STATEMENTS (41)IF Condition Statement (41)Relation Condition (42)Sign Condition (44)Class Condition (45)Condition-name Condition (46)Combined Condition (48)Evaluate Verb (49)9. LOOP STATEMENTS (51)Perform Thru (51)Perform Until (52)Perform Times (53)Perform Varying (54)GO TO Statement (55)10. STRING HANDLING (57)Inspect (57)String (59)Unstring (60)11. TABLE PROCESSING (63)Table Declaration (63)Subscript (65)Index (66)Set Statement (67)Search (69)Search All (69)12. FILE HANDLING (72)Field (72)Record (73)File (73)13. FILE ORGANIZATION (74)Sequential File Organization (74)Relative File Organization (75)14. FILE ACCESS MODE (77)Sequential Access (77)Random Access (78)Dynamic Access (79)15. FILE HANDLING VERBS (81)Open Verb (81)Read Verb (82)Write Verb (85)Rewrite Verb (87)Delete Verb (89)Start Verb (90)Close Verb (91)16. SUBROUTINES (92)Call Verb (92)Call By Reference (92)Call By Content (94)Types of Call (95)17. INTERNAL SORT (96)Sort Verb (96)Merge Verb (98)18. DATABASE INTERFACE (101)Embedded SQL (101)DB2 Application Programming (101)Host Variables (101)SQLCA (102)SQL Queries (103)Cursors (109)19. INTERVIEW QUESTIONS (112)What is Next? (116)1. OVERVIEWIntroduction to COBOLCOBOL is a high-level language. One must understand the way COBOL works. Computers only understand machine code, a binary stream of 0s and 1s. COBOL code must be converted into machine code using a compiler. Run the program source through a compiler. The compiler first checks for any syntax errors and then converts it into machine language. The compiler creates an output file which is known as load module. This output file contains executable code in the form of 0s and 1s. Evolution of COBOLDuring 1950s, when the businesses were growing in the western part of the world, there was a need to automate various processes for ease of operation and this gave birth to a high-level programming language meant for business data processing.∙In 1959, COBOL was developed by CODASYL (Conference on Data Systems Language).∙The next version, COBOL-61, was released in 1961 with some revisions.∙In 1968, COBOL was approved by ANSI as a standard language for commercial use (COBOL-68).∙It was again revised in 1974 and 1985 to develop subsequent versions named COBOL-74 and COBOL-85 respectively.∙In 2002, Object-Oriented COBOL was released, which could use encapsulated objects as a normal part of COBOL programming.Importance of COBOL∙COBOL was the first widely used high-level programming language. It is an English-like language which is user friendly. All the instructions can be coded in simple English words.∙COBOL is also used as a self-documenting language.∙COBOL can handle huge data processing.7∙COBOL is compatible with its previous versions.∙COBOL has effective error messages and so, resolution of bugs is easier. Features of COBOLStandard LanguageCOBOL is a standard language that can be compiled and executed on machines such as IBM AS/400, personal computers, etc.Business OrientedCOBOL was designed for business-oriented applications related to financial domain, defense domain, etc. It can handle huge volumes of data because of its advanced file handling capabilities.Robust LanguageCOBOL is a robust language as its numerous debugging and testing tools are available for almost all computer platforms.Structured LanguageLogical control structures are available in COBOL which makes it easier to read and modify. COBOL has different divisions, so it is easy to debug.82. ENVIORNMENT SETUPInstalling COBOL on Windows/LinuxThere are many Free Mainframe Emulators available for Windows which can be used to write and learn simple COBOL programs.One such emulator is Hercules, which can be easily installed on Windows by following a few simple steps as given below:∙Download and install the Hercules emulator, which is available from the Hercules' home site :www.hercules-390.eu∙Once you have installed the package on Windows machine, it will create a folder like C:/hercules/mvs/cobol.∙Run the Command Prompt (CMD) and reach the directory C:/hercules/mvs/cobol on CMD.∙The complete guide on various commands to write and execute a JCL and COBOL programs can be found at:/hercules/installmvs/instmvs2.htmHercules is an open-source software implementation of the mainframe System/370 and ESA/390 architectures, in addition to the latest 64-bit z/Architecture. Hercules runs under Linux, Windows, Solaris, FreeBSD, and Mac OS X.A user can connect to a mainframe server in a number of ways such as thin client, dummy terminal, Virtual Client System (VCS), or Virtual Desktop System (VDS). Every valid user is given a login id to enter into the Z/OS interface (TSO/E or ISPF). Compiling COBOL ProgramsIn order to execute a COBOL program in batch mode using JCL, the program needs to be compiled, and a load module is created with all the sub-programs. The JCL uses the load module and not the actual program at the time of execution. The load libraries are concatenated and given to the JCL at the time of execution using JCLLIB or STEPLIB.9There are many mainframe compiler utilities available to compile a COBOL program. Some corporate companies use Change Management tools like Endevor, which compiles and stores every version of the program. This is useful in tracking the changes made to the program.//COMPILE JOB,CLASS=6,MSGCLASS=X,NOTIFY=&SYSUID//*//STEP1 EXEC IGYCRCTL,PARM=RMODE,DYNAM,SSRANGE//SYSIN DD DSN=MYDATA.URMI.SOURCES(MYCOBB),DISP=SHR//SYSLIB DD DSN=MYDATA.URMI.COPYBOOK(MYCOPY),DISP=SHR//SYSLMOD DD DSN=MYDATA.URMI.LOAD(MYCOBB),DISP=SHR//SYSPRINT DD SYSOUT=*//*IGYCRCTL is an IBM COBOL compiler utility. The compiler options are passed using the PARM parameter. In the above example, RMODE instructs the compiler to use relative addressing mode in the program. The COBOL program is passed using the SYSIN parameter. Copybook is the library used by the program in SYSLIB. Executing COBOL ProgramsGiven below is a JCL example where the program MYPROG is executed using the input file MYDATA.URMI.INPUT and produces two output files written to the spool. //COBBSTEP JOB CLASS=6,NOTIFY=&SYSUID////STEP10 EXEC PGM=MYPROG,PARM=ACCT5000//STEPLIB DD DSN=MYDATA.URMI.LOADLIB,DISP=SHR//INPUT1 DD DSN=MYDATA.URMI.INPUT,DISP=SHR//OUT1 DD SYSOUT=*//OUT2 DD SYSOUT=*//SYSIN DD *//CUST1 1000//CUST2 1001/*10The load module of MYPROG is located in MYDATA.URMI.LOADLIB. This is important to note that the above JCL can be used for a non-DB2 COBOL module only. Executing COBOL-DB2 programsFor running a COBOL-DB2 program, a specialized IBM utility is used in the JCL and the program; DB2 region and required parameters are passed as input to the utility. The steps followed in running a COBOL-DB2 program are as follows:∙When a COBOL-DB2 program is compiled, a DBRM (Database Request Module) is created along with the load module. The DBRM contains the SQL statements of the COBOL programs with its syntax checked to be correct.∙The DBRM is bound to the DB2 region (environment) in which the COBOL will run. This can be done using the IKJEFT01 utility in a JCL.∙After the bind step, the COBOL-DB2 program is run using IKJEFT01 (again) with the load library and the DBRM library as the input to the JCL.//STEP001 EXEC PGM=IKJEFT01//*//STEPLIB DD DSN=MYDATA.URMI.DBRMLIB,DISP=SHR//*//input files//output files//SYSPRINT DD SYSOUT=*//SYSABOUT DD SYSOUT=*//SYSDBOUT DD SYSOUT=*//SYSUDUMP DD SYSOUT=*//DISPLAY DD SYSOUT=*//SYSOUT DD SYSOUT=*//SYSTSPRT DD SYSOUT=*//SYSTSIN DD *DSN SYSTEM(SSID)RUN PROGRAM(MYCOBB) PLAN(PLANNAME) PARM(parameters to cobol program) - LIB('MYDATA.URMI.LOADLIB')11END/*In the above example, MYCOBB is the COBOL-DB2 program run using IKJEFT01. Please note that the program name, DB2 Sub-System Id (SSID), and DB2 Plan name are passed within the SYSTSIN DD statement. The DBRM library is specified in the STEPLIB.Try it Option OnlineYou really do not need to set up your own environment to start learning COBOL programming language. Reason is very simple, we have already set up COBOL Programming environment online, so that you can compile and execute all the available examples online at the same time, when you are doing your theory work. This gives you confidence in what you are reading and to check the result with different options. Feel free to modify any example and execute it online.Try the following example using our Try it option available alongside the code in our website.IDENTIFICATION DIVISION.PROGRAM-ID. HELLO.PROCEDURE DIVISION.DISPLAY 'Hello World'.STOP RUN.When you compile and execute the above program, it produces the following result: Hello WorldFor some of the examples given in this tutorial, you will find a Try it option in our website code sections at the top right corner that will take you to the online compiler. So just make use of it and enjoy your learning. Try it option would work only with the code compatible with OpenCOBOL. The programs that require JCL (Input file, Output file or Parameters) for execution would not run on Try it option.123. PROGRAM STRUCTUREA COBOL program structure consists of divisions as shown in the following image:A brief introduction of these divisions is given below:∙Sections are the logical subdivision of program logic. A section is a collection of paragraphs.∙Paragraphs are the subdivision of a section or division. It is either a user-defined or a predefined name followed by a period, and consists of zero or more sentences/entries.∙Sentences are the combination of one or more statements. Sentences appear only in the Procedure division. A sentence must end with a period.∙Statements are meaningful COBOL statements that perform some processing.∙Characters are the lowest in the hierarchy and cannot be divisible.You can co-relate the above-mentioned terms with the COBOL program in the following example:PROCEDURE DIVISION.A0000-FIRST-PARA SECTION.FIRST-PARAGRAPH.13ACCEPT WS-ID - Statement-1 -----|MOVE '10' TO WS-ID - Statement-2 |-- Sentence - 1DISPLAY WS-ID - Statement-3 -----|.DivisionsA COBOL program consists of four divisions.Identification DivisionIt is the first and only mandatory division of every COBOL program. The programmer and the compiler use this division to identify the program. In this division, PROGRAM-ID is the only mandatory paragraph. PROGRAM-ID specifies the program name that can consist 1 to 30 characters.Try the following example using the Try it option online.IDENTIFICATION DIVISION.PROGRAM-ID. HELLO.PROCEDURE DIVISION.DISPLAY 'Welcome to Tutorialspoint'.STOP RUN.Given below is the JCL to execute the above COBOL program.//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C//STEP1 EXEC PGM=HELLOWhen you compile and execute the above program, it produces the following result: Welcome to TutorialspointEnvironment DivisionEnvironment division is used to specify input and output files to the program. It consists of two sections:Configuration section provides information about the system on which the program is written and executed. It consists of two paragraphs:14o Source computer : System used to compile the program.o Object computer : System used to execute the program.∙Input-Output section provides information about the files to be used in the program. It consists of two paragraphs:o File control : Provides information of external data sets used in the program.o I-O control : Provides information of files used in the program.ENVIRONMENT DIVISION.CONFIGURATION SECTION.SOURCE-COMPUTER. XXX-ZOS.OBJECT-COMPUTER. XXX-ZOS.INPUT-OUTPUT SECTION.FILE-CONTROL.SELECT FILEN ASSIGN TO DDNAMEORGANIZATION IS SEQUENTIAL.Data DivisionData division is used to define the variables used in the program. It consists of four sections:∙File section is used to define the record structure of the file.∙Working-Storage section is used to declare temporary variables and file structures which are used in the program.∙Local-Storage section is similar to Working-Storage section. The only difference is that the variables will be allocated and initialized every time a program starts execution.∙Linkage section is used to describe the data names that are received from an external program.COBOL ProgramIDENTIFICATION DIVISION.PROGRAM-ID. HELLO.15ENVIRONMENT DIVISION.INPUT-OUTPUT SECTION.FILE-CONTROL.SELECT FILEN ASSIGN TO INPUT.ORGANIZATION IS SEQUENTIAL.ACCESS IS SEQUENTIAL.DATA DIVISION.FILE SECTION.FD FILEN01 NAME PIC A(25).WORKING-STORAGE SECTION.01 WS-STUDENT PIC A(30).01 WS-ID PIC 9(5).LOCAL-STORAGE SECTION.01 LS-CLASS PIC 9(3).LINKAGE SECTION.01 LS-ID PIC 9(5).PROCEDURE DIVISION.DISPLAY 'Executing COBOL program using JCL'.STOP RUN.The JCL to execute the above COBOL program is as follows://SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C//STEP1 EXEC PGM=HELLO//INPUT DD DSN=ABC.EFG.XYZ,DISP=SHRWhen you compile and execute the above program, it produces the following result: Executing COBOL program using JCLProcedure Division16Procedure division is used to include the logic of the program. It consists of executable statements using variables defined in the data division. In this division, paragraph and section names are user-defined.There must be at least one statement in the procedure division. The last statement to end the execution in this division is either STOP RUN which is used in the calling programs or EXIT PROGRAM which is used in the called programs.IDENTIFICATION DIVISION.PROGRAM-ID. HELLO.DATA DIVISION.WORKING-STORAGE SECTION.01 WS-NAME PIC A(30).01 WS-ID PIC 9(5) VALUE '12345'.PROCEDURE DIVISION.A000-FIRST-PARA.DISPLAY 'Hello World'.MOVE 'TutorialsPoint' TO WS-NAME.DISPLAY "My name is : "WS-NAME.DISPLAY "My ID is : "WS-ID.STOP RUN.JCL to execute the above COBOL program://SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C//STEP1 EXEC PGM=HELLOWhen you compile and execute the above program, it produces the following result:Hello WorldMy name is : TutorialsPointMy ID is : 12345174. BASIC SYNTAXCharacter Set'Characters' are lowest in the hierarchy and they cannot be divided further. The COBOL Character Set includes 78 characters which are shown below: Character DescriptionA-Z Alphabets(Upper Case)a-z Alphabets (Lower Case)0-9 NumericSpace+ Plus Sign- Minus Sign or Hyphen* Asterisk/ Forward Slash$ Currency Sign, Comma; Semicolon18. Decimal Point or Period" Quotation Marks( Left Parenthesis) Right Parenthesis> Greater than< Less than: Colon' Apostrophe= Equal SignCoding SheetThe source program of COBOL must be written in a format acceptable to the compilers. COBOL programs are written on COBOL coding sheets. There are 80 character positions on each line of a coding sheet.Character positions are grouped into the following five fields:Positions Field Description1-6 Column Numbers Reserved for line numbers.7 Indicator It can have Asterisk (*) indicating comments,Hyphen (-) indicating continuation and Slash (/ ) indicating form feed.198-11 Area A All COBOL divisions, sections, paragraphs andsome special entries must begin in Area A.12-72 Area B All COBOL statements must begin in area B.73-80 Identification Area It can be used as needed by the programmer.ExampleThe following example shows a COBOL coding sheet:000100 IDENTIFICATION DIVISION. 000100000200 PROGRAM-ID. HELLO. 000101000250* THIS IS A COMMENT LINE 000102000300 PROCEDURE DIVISION. 000103000350 A000-FIRST-PARA. 000104000400 DISPLAY “Coding Sheet”. 000105 000500 STOP RUN. 000106JCL to execute the above COBOL program://SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C//STEP1 EXEC PGM=HELLOWhen you compile and execute the above program, it produces the following result:Coding Sheet20Character StringsCharacter strings are formed by combining individual characters. A character string can be a∙Comment,∙Literal, or∙COBOL word.All character strings must be ended with separators. A separator is used to separate character strings.Frequently used separators : Space, Comma, Period, Apostrophe, Left/Right Parenthesis, and Quotation mark.CommentA comment is a character string that does not affect the execution of a program. It can be any combination of characters.There are two types of comments:Comment LineA comment line can be written in any column. The compiler does not check a comment line for syntax and treats it for documentation.Comment EntryComment entries are those that are included in the optional paragraphs of an Identification Division. They are written in Area B and programmers use it for reference.The text highlighted in Bold are the commented entries in the following example: 000100 IDENTIFICATION DIVISION. 000100000150 PROGRAM-ID. HELLO. 000101000200 AUTHOR. TUTORIALSPOINT. 000102000250* THIS IS A COMMENT LINE 00010321000300 PROCEDURE DIVISION. 000104000350 A000-FIRST-PARA. 000105000360/ First Para Begins - Documentation Purpose 000106000400 DISPLAY “Comment line”. 000107 000500 STOP RUN. 000108JCL to execute above COBOL program://SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C//STEP1 EXEC PGM=HELLOWhen you compile and execute the above program, it produces the following result: Comment Line22End of ebook previewIf you liked what you saw…Buy it from our store @ https://23。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
第九章 表的建立和查找
软件学院 代飞 2011.秋
9.1 表的概念
COBOL语言的表(TABLE)相当于C语言中的数 组(ARRAY),但有一些区别。
表处理:
表的建立
——定义数组
表元素的处理——数组元素的处理
多个数据项地位和作用是相同的,是否都需要独 立的数据名?
有一个居民记录,内包含几个
表元素可以是初等项,也可以是组合项
01 PRODUCT-RECORD.
03
PRODUCT OCCURS 20 TIMES.
04 QUANTITY-OF-PRODUCTION PIC 9(6). 04 QUANTITY-OF-SALES PIC 9(6).
04 QUANTITY-OF-HAND PIC 9(6).
9.7.2 位标名的指定方法
对于如下的:
A
(3) or A (S)
S可能是下标,也可能是位标
如何区别呢?
01 A.
03 T OCCURS 5 INDEXED BY I PIC X(2).
I为位标,而且是表T的位标 T (I),系统知道按位标方式处理,即把I的值作为相
对地址来处理。
注意,位标名不需要再专门进行描述,如下面对I再次 声明是错误的: 77 I PIC 99. 不能这么做
下面给出表定义的完整语法图,请大家再次注意
indexed的位置
整数1 T IMES 1 数据名 DEPENDING 层号 OCCURS 整数1 TO 整数2 TIMES FILLER ON 数据名2 ASCENDING 数据名3...... IS KEY DESCENDING INDEXED BY 位标名 ......其他数据描述项子句 1
5.
表元素可以为组合项。在建立一个多维表的同时, 也建立了一维或二维等表。
6.
有的COBOL标准只允许建立三维表。而IBM主机 COBOL无此限制。
7.
只有当OCCURS所说明的数据是初等项,才能在
该数据项的描述中使用PIC子句
8.
不能用VALUE子句对表赋初值
9.
多维表的元素在内存中是以行排列。
三个初等项都是“数量”(生产数量、销售数量、库
存数量)。数据描述都是9(6),它们的地位和描述
又都相同,因此,可以进一步简化:
01 PRODUCT-RECORD.
03
PRODUCT是一维表 QUANTITY是二维表
PRODUCT OCCURS 20 TIMES.
04 QUANTITY OCCURS 3 PIC 9(6).
9.3 可变长表
思考:学生选课,每个学生在每学期选课数量可 能不同。但是有每学期最高选课数限制。如何处 理? C语言:
根据最高选课数建立数组
COBOL
可变长表
我们希望是这样的:
ห้องสมุดไป่ตู้
01 STUDENT-SCORE-RECORD.
03 NAME PIC X(10). 03 QTY-OF-COURSE PIC 9(2). 03 SCORE OCCURS 1 TO 15 DEPENDING ON QTY-OF-COURSE PIC 9(3)
注意:
1.
如果想使用位标,必须在表建立的同时使用 INDEXED BY 短语来指出位标。否则当做一般的 下标。 位标是特殊数据项,不能参与计算;不能用输入 语句(READ)改变其值;也不能将它的值直接打 印或者显示;也不是记录的一部分,不能在文件 节定义,也不必在工作单元节中定义。
多维表的每一维均可以指定位标,按维定义,按 维使用
04
QUANTITY OCCURS 3 TIMES PIC 9(6).
引用:QUANTITY (3,8,2)
示例:有如下程序 01 TABLE.
02
A1 OCCURS 5 TIMES.
03 B1 PIC X(3). 03 B2 OCCURS 10 TIMES PIC 9(4). 03 B3 OCCURS 8 TIMES.
01 UNIT-PRICE-TABLE-R REDEFINES
UNIT-PRICE-TABLE. 02 TABLE-R OCCURS 5. 03 PROD-CODE PIC 9(4). 03 PROD-PRICE PIC 9(6).
9.6 举例
IDENTIFICATION DIVISION. PROGRAM-ID. EXAM2A4. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. DATA DIVISION. WORKING-STORAGE SECTION. 01 UNIT-PRICE-TABLE. 02 FILLER PIC X(10) VALUE '0010000200'. 02 FILLER PIC X(10) VALUE '0020000500'. 02 FILLER PIC X(10) VALUE '0030000300'. 02 FILLER PIC X(10) VALUE '0040000250'. 02 FILLER PIC X(10) VALUE '0050005000'. 01 UNIT-PRICE-TABLE-R REDEFINES UNIT-PRICE-TABLE. 02 TABLE-R OCCURS 5. 03 PROD-CODE PIC 9(4). 03 PROD-PRICE PIC 9(6). 77 I PIC 9. 77 CODE-E PIC ZZZZ. 77 PRICE-E PIC $$$$$$. PROCEDURE DIVISION. S. PERFORM VARYING I FROM 1 BY 1 UNTIL I > 5 MOVE PROD-CODE (I) TO CODE-E MOVE PROD-PRICE (I) TO PRICE-E DISPLAY I DISPLAY 'CODE:', CODE-E, ' IS ', PRICE-E END-PERFORM STOP RUN.
一般格式:
OCCURS 整数1 TO 整数2 TIMES
DEPENDING ON 数据名1
数据名1可以在表所在的记录中描述,也可以不在本
记录中描述,也就是说可以在工作单元节其他部分
描述。但不能是“表”的一部分!
9.4 表元素的引用
表必须在数据部先定义,才能在过程部引用。 使用表名和序号来引用。例如SCORE (2)
OCCURS子句的最简单格式

• 1. 2. 3. 4.
OCCURS
说明:
整数 TIMES
OCCURS后面只能是正整数;
不能出现在77层(66,88层也不行); 不能用于01层; 如果OCCURS描述一个初等项,则“重复出现” 的是初等项。如果用OCCURS描述一个组合项, 则“重复出现”的是组合项。
居民的号码、性别、午龄、地
址等数据。需要为每一居民单 独起一名字,并分别描述。见 右图
如果有100个人呢,如果人更
多呢?
01 PRODUCT-RECORD.
02
PRODUCT-1
QUANTITY-OF-PRODUCTION PIC 9(6) 生产数 QUANTITY-OF-SALES PIC 9(6) 销售数
2.
3.
4.
思考,如何将位标值转储到其他数据项?
二维表需要两个下标。例如QUANTITY (3, 2)
例如,有下列示例:
01 TABLE.
02 B OCCURS 8.
03 C1 PIC X(8). 03 C2 PIC X(2). (1)不能直接引用 表名而不加下标

MOVE B TO A.
错误
(2)如果表元素是组合项,则引用其下属项也必须使 用下标
只要确定表名和序号(出现号,occurs number), 就可唯一地确定一个表元素。
下标:元素的位置序号 位标:元素的相对地址位置
9.2表的建立
如果一个数据名是表,则必须指出这个“表”包含 多少个元素
01 STUDENT-RECORD
02 02
NAME PIC X(20). COURSE OCCURS 5 TIMES PIC 9(3).
再看个例子
01 T VALUE ‘ABCDEFGHIJ’. 02 Q OCCURS 3 PIC X(3).
T Q(1) A B C D Q(2) E F G Q(3) H I
第二种办法,联合使用OCCURS和REDIFINES子句 来给各个表元素赋初值。 例如:
产品号 0010 0020 单价 000200 000500
初值即可。
第二种方法:联合使用OCCURS子句和REDEFINES子
句来给各个表元素赋初值!
第一种方法:对包括所有表元素的整个表赋给 个初值。这时可以对表的描述体上面一层的数据 项赋一个初值即可。
例加,有个表A:
01 TABLE.
01 TABLE VALUE IS ZERO.
02 A OCCURS 20 PIC 9(3).
9.7 用位标法引用表元素
9.7.1 位标的概念
下标——元素在表中的序号 位标——元素在表中的相对位置
编译系统根据下标号计算出需引用表元素的地址。 每次使用都要进行一次地址的计算。
C语言的类比 int x[10];
for(int i=0;i<10;i++)
{ x[i]=0; … *(x+i)=0; }
A
(B (2) )
(6)带下标的数据名又称为下标数据名
9.5 给表元素赋初值
不能在表的定义中同时用OCCURS子句和VLAUE 子句给表元素赋初值。 运行中,用循环语句,通过MOVE等语句赋值
相关文档
最新文档