gpt4 book ai didi

cobol - 有没有办法在 COBOL 中参数化函数?

转载 作者:行者123 更新时间:2023-12-01 04:13:44 24 4
gpt4 key购买 nike

我正在编码例程,例如:

READ-A.
READ FILE-A
AT END
MOVE 1 TO EOF-A
NOT AT END
ADD 1 TO CN-READ-A
END-READ.
F-READ-A. EXIT.

读取多个文件,我想知道是否有一种方法可以编写一个能够从变量中读取文件名的例程,这样我就不必为每个文件编写相同的代码。谢谢!

最佳答案

如上所述的一种解决方案是使用多个程序或嵌套程序,为此
我在下面包含了一个示例,即解决方案 1。

另一个解决方案是 COBOL 类,这可能不符合您的喜好,但我喜欢它们,因此我提供了一个示例,即解决方案 2。

解决方案1:

   program-id. TestProgram.
working-storage section.
01 file-name pic x(128).
01 file-lines pic 9(9).
procedure division.
move 0 to file-lines
move "d:\rts_win32.txt" to file-name
call "program1" using file-name file-lines
display file-lines
stop run
end program TestProgram.

   program-id. Program1.

file-control.
select file-a assign to myfile
organization is line sequential.

data division.
fd file-a.
01 file-a-line pic x(80).

working-storage section.
01 EOF-A pic 9 value 0.
linkage section.
01 lk-filename pic x(128).
01 CN-READ-A pic 9(9).
procedure division using lk-filename
CN-READ-A.

move lk-filename to myfile
open input file-a

perform READ-A until EOF-A equals 1
close file-a
goback.

READ-A.
READ FILE-A
AT END
MOVE 1 TO EOF-A
NOT AT END
ADD 1 TO CN-READ-A
END-READ.
F-READ-A.
EXIT.


end program Program1.

解决方案2

  program-id. TestProgram.:
working-storage section.
01 file-counter type FileLineCounter.

   procedure division.
set file-counter to new type FileLineCounter("d:\rts_win32.txt")
display file-counter::LineCount
stop run
end program TestProgram.

   class-id FileLineCounter.

   file-control.
select file-a assign to myfile
organization is line sequential.

data division.
fd file-a.
01 file-a-line pic x(80).

working-storage section.

01 cn-read-a binary-long property as "LineCount".

method-id New.
01 EOF-A pic 9 value 0.
procedure division using by value filename as string.

set myfile to filename
open input file-a

perform READ-A until EOF-A equals 1
close file-a
goback.

READ-A.
READ FILE-A
AT END
MOVE 1 TO EOF-A
NOT AT END
ADD 1 TO CN-READ-A
END-READ.
F-READ-A.
EXIT.

end method.

end class.

关于cobol - 有没有办法在 COBOL 中参数化函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4819272/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com