1

我是 COBOL 的新手,我们最新的培训活动是我们将尝试获得如下三个项目的输出:

  1. 0000/2013
  2. 00012345
  3. 12345**

我已经尝试使用下面的代码,但它显然没有给我想要的输出。它不显示零。

IDENTIFICATION DIVISION.

PROGRAM-ID. ACTIVITY4.

ENVIRONMENT DIVISION.

DATA DIVISION.

WORKING-STORAGE SECTION.

01 NUM1 PIC z(4)9(4) VALUE 2013.
01 NUM2 PIC Z(3)9(5) VALUE 12345.
01 NUM3 PIC 9(5) VALUE 12345.
01 E-NUM1 PIC *(2)9 VALUE 0.

PROCEDURE DIVISION.

DISPLAY-VALUES.

DISPLAY-ZEROES.

MOVE E-NUM1 TO NUM3.

 DISPLAY "1) "NUM1.

 DISPLAY "2) "NUM2.

 DISPLAY "3) "NUM3.

 STOP RUN.
4

3 回答 3

2

It looks to me like Activity 4 is about formatting data for output. You need to produce:

0000/2013
00012345
12345**

The first requires the insertion of a slash. COBOL has a slash insertion symbol, '/'. The second is to ensure no zero-suppression, which is the behaviour when a PIC 9(n) field is output, so nothing really to do other than get the length right. The third apparently draws you to the "*" replacement edit field, which is the intention of the question I guess, but the * editing symbol replaces leading zeros only.

One simple way to get the trailing asterisks is to use the data-definition. Define a group item, which is what you will DISPLAY. Subordinate to that, define your number (PIC 9(5)) and follow it immediately with a FILLER (named field if you like) PIC XX which has a VALUE of "**" (or ALL "*", a little extreme for a two-byte field though).

IDENTIFICATION DIVISION.

PROGRAM-ID. ACTIVITY4.

ENVIRONMENT DIVISION.

DATA DIVISION.

WORKING-STORAGE SECTION.

01 NUM1 PIC 9(4)/9(4).
01 NUM2 PIC 9(8).
01 NUM3-OUT. 
    05  NUM3 PIC 9(5). 
    05  FILLER PIC XX VALUE "**".

PROCEDURE DIVISION.

DISPLAY-VALUES.

DISPLAY-ZEROES.
 MOVE 1234 TO NUM1

 DISPLAY "1) " 
             ">"
             NUM1
             "<"
 MOVE 12345 TO NUM2

 DISPLAY "2) "
             ">"
             NUM2
             "<"
 MOVE 12345 TO NUM3
 DISPLAY "3) "
             ">"
             NUM3-OUT
             "<"

 STOP RUN
 .

I have never put a VALUE on an edited field. The editing is carried out when the field is a target of a COBOL verb. In the COBOLs I use this would not effect the edit, it would just have that literal value. I don't know about GNU OpenCOBOL (formerly OpenCOBOL).

于 2013-10-21T08:22:05.103 回答
0

关于这个12345**问题:如果没有其他方法,您可以将数字连接到星号,STRING如下所示:

WORKING-STORAGE SECTION.

01 NUM3 PIC X(7).
01 NUM3-NUMBERS PIC 9(5) VALUE 12345.

PROCEDURE DIVISION.

STRING NUM3-NUMBERS "**" DELIMITED BY SIZE INTO NUM3.
DISPLAY "3) "NUM3.

此代码的作用是在您的号码末尾添加 2 个星号。(但您的 NUM3 现在是字母数字图片而不是数字图片)。

我刚刚在一本旧的 cobol 书中发现的另一件事是*检查保护字符。我从未尝试过,但在这里它声明它应该以这种方式使用:

01 NUM3 PIC 9(5)*(2).

试试看!

于 2013-10-22T14:10:16.663 回答
0

声明数字时去掉 Z。

Z 抑制 COBOL 中数字中的前导零。

只需将变量声明更改为:

01 NUM1 PIC 9(8) VALUE 00002013.
01 NUM2 PIC 9(8) VALUE 00012345.

(您实际上不需要数字中的前面的零,它们只是为了向您展示整个 PIC。)

有关图片语句的更详细描述以及不同字符修饰符及其作用的快速摘要,请参阅此内容。

起初我没有发现其他问题,但是您创建的其他变量应该是

01 E-NUM1 PIC 9(2) VALUE 0.
于 2013-10-21T06:42:53.730 回答