COBOL问题 - 来自初学者的问题,请指导

时间:2011-11-15 01:22:42

标签: cobol

我想实现以下

一串pic X(5)包含A1992并且递增到A9999,在达到A9999后,A应该被B替换,其他字符应该重新初始化为0000,即B0000,这应该发生到Z9999,是有可能吗?

或者如果你能告诉我如何增加A到Z就足够了

4 个答案:

答案 0 :(得分:2)

您需要对此进行一些手动角色操作。有几个部分,首先,您需要处理数字部分的简单添加,然后您需要处理它的翻转以增加alpha部分。

与此类似的数据结构可能会有所帮助:

01 Some-Work-Area.
  02 Odometer-Char-Vals      pic x(27) value 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.
  02 Odometer-Char occurs 27 pic x.
  02 Odo-Char-Ndx            pic s9(8) binary.

01 My-Odometer.
    88 End-Odometer-Value    value 'Z9999'.
  02 My-Odometer-X           pic X.
  02 My-Odometer-9           pic 9999.
    88 Carry-Is-True         value 9999.

这将与一个简单的执行循环一起使用,如下所示:

Move 0 to My-Odometer-9
Move 1 to Odo-Char-Ndx
Move Odometer-Char-Vals (Odo-Char-Ndx) to My-Odometer-X

Perform until End-Odometer-Value
   Add 1 to My-Odometer-9
   Display My-Odometer
   If Carry-Is-True
      Move 0 to My-Odometer-9
      Add 1 to Odo-Char-Ndx
      Move Odometer-Char-Vals (Odo-Char-Ndx) to My-Odometer-X
   End-If
End-Perform

这是你可以做到的一种方式。

请注意,上面的代码采用了一些快捷方式(也就是笨拙的黑客) - 就像把垫单元放在里程表-Char数组中一样,所以我不需要进行范围检查。除了示例和想法之外,你不会想要使用它。

答案 1 :(得分:2)

我可能会使用嵌套的执行循环。

存储

01  ws-counter-def
    03  ws-counter-def-alpha-list      pic x(27) value 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.
    03  ws-counter-def-num             pic 9(4) comp-3.

01  ws-counter redefines ws-counter-def
    03  ws-counter-alpha occurs 27 times indexed by counter-idx   pic x.
    03  ws-counter-num                 pic 9(4) comp-3.      

01  ws-variable                        
    03  ws-variable-alpha              pic X
    03  ws-variable-num                pic X(4).                     

步骤:

Initialize counter-idx.
Move 1992 to ws-counter-num.

Perform varying counter-idx from 1 by 1 until counter-idx > 26
  move ws-counter-alpha(counter-idx) to ws-variable-alpha
  perform until ws-counter-num = 9999
        add 1 to ws-counter-
        move ws-counter-num to ws-variable-num.
        *do whatever it is you need to do to the pic X(5) value in ws-variable*
  end-perform
  move zeros to ws-counter-num
end-perform.

答案 2 :(得分:1)

无法帮助自己......这个怎么样......

   IDENTIFICATION DIVISION.                                         
   PROGRAM-ID. EXAMPLE.                                             
   DATA DIVISION.                                                   
   WORKING-STORAGE SECTION.                                         
   01.                                                              
       02 ALL-LETTERS  PIC X(26) VALUE 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.
       02 LETTERS REDEFINES ALL-LETTERS.                            
          03 LETTER    PIC X OCCURS 26 INDEXED BY I. 
   01  START-NUMBER    PIC 9(4).              
   01  COUNTER.                                                     
       02 COUNTER-LETTER    PIC X.                                  
       02 COUNTER-NUMBER    PIC 9(4).                                
   PROCEDURE DIVISION.
       MOVE 1992 TO START-NUMBER                                              
       PERFORM VARYING I FROM 1 BY 1 UNTIL I > LENGTH OF ALL-LETTERS                   
           MOVE LETTER (I) TO COUNTER-LETTER                        
           PERFORM TEST AFTER VARYING COUNTER-NUMBER FROM START-NUMBER BY 1 
                                UNTIL COUNTER-NUMBER = 9999         
              DISPLAY COUNTER - or whatever else you need to do with the counter...             
           END-PERFORM
           MOVE ZERO TO START-NUMBER                                              
       END-PERFORM                                                  
       GOBACK                  
       .                       

这会打印从A1992Z9999开头的所有“数字”。

基本上偷走了Marcus_33的代码,然后又多了一点。如果你有这种倾向,请提出他的回答,而不是我的回答

答案 3 :(得分:0)

对于混淆了COBOL的爱好者,这里是我能想到的最短(便携)版本(假设编译器具有内部函数):

IDENTIFICATION DIVISION.
PROGRAM-ID. so.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 ws-counter value "A00".
   03 ws-alpha   pic x.
   03 ws-number  pic 99.
PROCEDURE DIVISION.
1.
     Perform with test after until ws-counter > "Z99"
       Display ws-counter, " " with no advancing
       Add 1 To ws-number
          On size error
             Move zero to ws-number
             perform with test after until ws-alpha is alphabetic-upper or > "Z"
                 Move Function Char (Function Ord( ws-alpha ) + 1) to ws-alpha
             end-perform 
       End-add
    End-perform.
END PROGRAM so.

在OpenVMS / COBOL上测试。我将值缩短为X(3),因为看起来很无聊。非可移植版本(如果您了解平台的Endianness)是将前缀重新定义为S9(4) COMP并直接递增低位。但是那个解决方案不会更短......