000010 id division.
000020*
000030 program-id. rpt0001.
000040*
000050 environment division.
000060*
000070 input-output section.
000080*
000090 file-control.
000100*
000110 select custmast assign to "F:\Cobol\Report\custmast.dat".
000120 select salesrpt assign to "F:\Cobol\Report\salesrpt.rpt".
000130*
000140 data division.
000150*
000160 file section.
000170*文件记录描述
000180 fd custmast.
000190 01 customer-master-record.
000200 05 cm-branch-number pic 9(2).
000210 05 cm-salesrep-number pic 9(2).
000220 05 cm-customer-number pic 9(5).
000230 05 cm-customer-name pic x(20).
000240 05 cm-sales-this-ytd pic s9(5)v9(2).
000250 05 cm-sales-last-ytd pic s9(5)v9(2).
000260*报表项表述 指明该输出文件格式是报表。
000270 fd salesrpt
000280 label record is omitted
000290 report is rep.
000300*
000310 working-storage section.
000320*读取控制开关
000330 01 switches.
000340 05 custmast-eof-switch pic x values "N".
000350*当前日期和时间
000360 01 current-date-and-time.
000370 05 cd-year pic 9(4).
000380 05 cd-month pic 99.
000390 05 cd-day pic 99.
000400 05 cd-hours pic 99.
000410 05 cd-minutes pic 99.
000420 05 filler pic x(9).
000430*当前日期
000440 01 fmt-cur-date.
000450 05 fcd-month pic 9(2).
000460 05 filler pic x(1) values "/".
000470 05 fcd-day pic 9(2).
000480 05 filler pic x(1) value "/".
000490 05 fcd-year pic 9(4).
000500*当前时间
000510 01 fmt-cur-time.
000520 05 fct-hours pic 99.
000530 05 filler pic x value ":".
000540 05 fct-minutes pic 99.
000550*报表段
000560 report section.
000570*报表总览、报表头、页头、控制头、细目、控制尾、页尾、报表尾(报表的核心)
000580 rd rep
000590 controls are final
000600 page limits are 66 lines
000610 heading 3
000620 first detail 9
000630 last detail 52
000640 footing 61.
000650 01 rep-head type is report heading
000660 next group is next page.
000670 02 line 15 column 40 pic x(50)
000680 value is "The Report of Customer".
000690 02 line 50 column 45 pic x(40)
000700 value is 'Neusoft 2007.1.18'.
000710 01 page-head type is page heading.
000720 02 line 4.
000730 03 column 10 pic x(5)
000740 value is "DATE:".
000750 03 column 18 pic x(10)
000760 source is fmt-cur-date.
000770 03 column 35 pic x(25)
000780 value is "Year-To-Date SALES REPORT".
000790 03 column 100 pic x(5)
000800 value is "PAGE:".
000810 03 column 108 pic ZZ9
000820 source is page-counter.
000830 02 line 6.
000840 03 column 10 pic x(5)
000850 value is "TIME:".
000860 03 column 18 pic x(5)
000870 source is fmt-cur-time.
000880 03 column 100 pic x(7)
000890 value is "RPT0002".
000900 01 detail-head type is control heading final
000910 next group is plus 2.
000920 02 line plus 2.
000930 03 column 10 pic x(4)
000940 value is "CUST".
000950 03 column 50 pic x(10)
000960 value is " SALES ".
000970 03 column 70 pic x(10)
000980 value is " SALES ".
000990 02 line plus 1.
001000 03 column 10 pic x(4)
001010 value is "NUM ".
001020 03 column 20 pic x(20)
001030 value is "CUSTOMER NAMES ".
001040 03 column 50 pic x(10)
001050 value is " THIS YTD ".
001060 03 column 70 pic x(10)
001070 value is " LAST YTD ".
001080 01 detail-line type is detail.
001090 02 line plus 1.
001100 03 column 10 pic 9(5)
001110 source cm-customer-number.
001120 03 column 20 pic x(20)
001130 source cm-customer-name.
001140 03 column 50 pic s9(5)v9(2)
001150 source cm-sales-this-ytd.
001160 03 column 70 pic s9(5)v9(2)
001170 source cm-sales-last-ytd.
001180 01 detail-foot type is control footing final
001190 next group is next page.
001200 02 line plus 2.
001210 03 column 20 pic x(5)
001220 value is " SUM:".
001230 03 column 50 pic z,zzz,zzz.99
001240 sum cm-sales-this-ytd.
001250 03 column 70 pic z,zzz,zzz.99
001260 sum cm-sales-last-ytd.
001270 01 report-end type is report footing.
001280 02 line 25 next page.
001290 03 column 50 pic x(14)
001300 value is "Report END".
001310 02 line 35.
001320 03 column 50 pic x(10)
001330 value is "Writer :".
001340 03 column 60 pic x(12)
001350 value is "YuMing Mao.".
001360*过程部
001370 procedure division.
001380*初始化报表并输出
001390 000-prepare-sales-report.
001400 open input custmast
001410 output salesrpt.
001420 perform 100-format-report-heading.
001430 initiate rep.
001440 perform 200-prepare-sales-lines
001450 until custmast-eof-switch = "Y".
001460 perform 300-end-report.
001470 close custmast salesrpt.
001480 stop run.
001490*格式化控制头
001500 100-format-report-heading.
001510 move function current-date to current-date-and-time.
001520 move cd-month to fcd-month.
001530 move cd-day to fcd-day.
001540 move cd-year to fcd-year.
001550 move cd-hours to fct-hours.
001560 move cd-minutes to fct-minutes.
001570*读取记录并生成细目
001580 200-prepare-sales-lines.
001590 perform 210-read-customer-record.
001600 if custmast-eof-switch = "N"
001610 perform 220-print-customer-line
001620 end-if.
001630*读取记录
001640 210-read-customer-record.
001650 read custmast
001660 at end
001670 move "Y" to custmast-eof-switch.
001680*生成细目
001690 220-print-customer-line.
001700 generate detail-line.
001710*结束报表编辑
001720 300-end-report.
001730 terminate rep.
六、比较
可以看出,以上两种方式在处理相同的问题时代码量是差不多的。只是前者把大量的代码放在过程部中去了,通过赋值等语句把报表的各个区域“画”出来;而后者则是通过大量的报表描述把报表设计出来,过程部里只调用了几条语句:初始化、生成细目、结束报表。前者对于处理单一的分类报表还算游刃有余,但是对于多维度的统计,交叉统计,条件统计等复杂的操作显然力不从心。这里我们只给出了报表语句的简单应用,一般而言使用报表语句生成报表的大致步骤如下:
1)在File Section中增加如下语句
FD Report-file
.....
Report is 报表名
2)在File Section下增加如下语句
Report Section
RD 报表名
.....
01 ...
....
01...
....
3)利用Value、Source、Sum等统计语句填充上面的字段;
4)生成报表之前在过程部中初始化
Initiate Rep
该语句把求和计数器和行计数器置0,页计数器置1
5)读记录,生成细目
Generate 细目名/报表名
6)结束报表
Terminate 报表名
注意:复杂报表可以使用中间的统计变量;可以在过程部中的Declaratives段中进行深入的处理。
七、不足
1、先天不足。Cobol语言由于一开始就是给大机使用的,而大机从开始到现在一直都是字符界面,因而并没有注重字体大小、颜色、类型的变化。而现代报表的花哨程度自然是Cobol所无法实现的。
2、和数据库的结合。Cobol语言似乎对处理文件和处理数据库差不多。而现在的编程语言很难想象允许这样频繁地操作文件,和数据库的结合产生报表将是cobol在现在环境下需要发挥威力的地方。