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

发布于 2024-12-15 16:35:35 字数 155 浏览 3 评论 0原文

我想实现下面的

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

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

I want to acheive the below

a string of pic X(5) contains A1992 and is incremented to A9999 , after it reaches A9999 , the A should be replaced by B and the other characters should be reinitialized to 0000 ie B0000 , this should happen until Z9999 , is it possible somehow ?

or if you could show me how to increment A till Z that would be suffice

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

痞味浪人 2024-12-22 16:35:35

您需要对此进行一些手动字符操作。有几个部分,首先,您需要处理数字部分的简单加法,然后您需要处理其翻转以增加 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

这是您可以做到的一种方法。

请注意,上面的代码采用了一些快捷方式(又名卑鄙的黑客)——比如将一个填充单元放入 Odometer-Char 数组中,这样我就不必对其进行范围检查。除了示例和想法之外,您不会想将其用于任何其他用途。

You will need to do some manual character manipulation on this one. There are several parts, first, you need to handle the simple addition of the numeric portion, then you need to handle the rollover of that to increment the alpha portion.

Data structures similar to this might be helpful:

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.

This would be used with a simple perform loop like so:

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

That is one way you could do it.

Please note, the code above took some shortcuts (aka skanky hacks) -- like putting a pad cell in the Odometer-Char array so I don't have to range check it. You wouldn't want to use this for anything but examples and ideas.

故事还在继续 2024-12-22 16:35:35

我可能会使用嵌套的执行循环来做到这一点。

储存:

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.

I'd probably do this with a nested perform loop.

Storage:

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).                     

Procedure:

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.
铃予 2024-12-22 16:35:35

只是无法控制自己...这个怎么样...

   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                  
       .                       

这将打印从 A1992 开始到 Z9999 的所有“数字”。

基本上窃取了 Marcus_33 的代码并稍微修改了一下。如果您愿意,请投票给他的答案,而不是我的答案

Just can't help myself... How about this...

   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                  
       .                       

This will print all the "numbers" beginning with A1992 through to Z9999.

Basically stole Marcus_33's code and twiked it a tiny bit more. If you feel so inclined please upvote his answer, not mine

说好的呢 2024-12-22 16:35:35

对于混淆的 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 并直接递增低位。但这个解决方案不会更短......

For lovers of obfuscated COBOL, here's the shortest (portable) version I can think of (assuming a compiler with Intrinsic Functions):

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.

Tested on OpenVMS/COBOL. I shorten the value to X(3) since it's boring to watch run. A non-portable version (if you are aware of the Endianness of your platform) is to redefined the prefix as a S9(4) COMP and increment the low-order bits directly. But that solution wouldn't be any shorter...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文