使用 COBOL 反转字符串

问题描述 投票:0回答:1

我正在尝试反转 COBOL 中给定的字符串。

IBM 文档
我在这里参考了IBM文档:https://www.ibm.com/docs/en/cobol-zos/6.1?topic=functions-transforming-reverse-order-reverse

我的代码未按预期工作
这是我的代码:

       identification division.
       program-id. solution.
       data division.
       linkage section.
       01  str.
           05 len      pic 99.
           05 chars    pic a(30).
       01  result.
           05 len      pic 99.
           05 chars    pic a(30).
       procedure division using str result.
          move function reverse(str) to result.
          goback.
       end program solution.

测试
这是我用来验证我的功能的测试 - 它失败了,但我不知道我的代码出了什么问题:

       identification division.
       program-id. tests.

       data division.
       working-storage section.
       01  a-disp      pic z(7)9.
       01  b-disp      pic z(7)9.
       01  expected.
           05  len     pic 99.
           05  chars   pic a(30).
       01  str.
           05  len     pic 99.
           05  chars   pic a(20).
       01  result.
           05  len     pic 99.
           05  chars   pic a(30).

       procedure division.
           testsuite 'Fixed Tests.'.
           move 'world' to chars of str
           move length of 'world' to len of str
           move 'dlrow' to chars of expected
           move length of 'dlrow' to len of expected
           perform dotest

           move 'hello' to chars of str
           move length of 'hello' to len of str
           move 'olleh' to chars of expected
           move length of 'olleh' to len of expected
           perform dotest
           
           move 0 to len of str
           move 0 to len of expected
           perform dotest

           move 'h' to chars of str
           move length of 'h' to len of str
           move 'h' to chars of expected
           move length of 'h' to len of expected
           perform dotest

           end tests.

       dotest.
           testcase 'Testing ' '"' chars of str(1:len of str) '"'.
           call 'solution' using str result
           evaluate len of result
               when <> len of expected
                   move len of result to a-disp
                   move len of expected to b-disp
                   initialize assertion-message
                   string 'Expected and actual have different lengths' 
                      line-feed
                      'expected: ' function trim(b-disp)
                      line-feed
                      'actual:   ' function trim(a-disp)
                       into assertion-message
                   perform assert-false
               when 0
                   initialize assertion-message
                   perform assert-true
               when other
                   expect chars of result(1:len of result) 
                          to be 
                          chars of expected(1:len of expected).
           end-evaluate
           .

       end program tests.
cobol
1个回答
0
投票

当然这是行不通的。 FUNCTION REVERSE 采用“字符串”,而不是具有长度和“字符串”的组。

使用 FUNCTION REVERSE (str(1:len)) 或简单地使用 FUNCTION REVERSE (FUNCTION TRIM (str))。

© www.soinside.com 2019 - 2024. All rights reserved.