如何正确地将c++代码转换为cobol

How to convert c++ code to cobol correctly

我已经编写了代码来获取数组中出现次数最多的数字。主函数将调用 readData,它将数据读入我传递给 readData 子例程的数组中。执行 readData 子例程后,我使用模式函数查找数组中出现次数最多的数字。

示例数组:|1|2|2|1|1|

生成的输出:1

我的c++代码如下:

#include <iostream>

using namespace std;

void mode(int array[]){
    int max = 0, num;
     int count;
    
    for(int i=0; i<5; ++i){
        count = 0;
        for(int j=0; j<5; ++j){
            if(array[i] == array[j]){
                ++count;
            }
             if(max < count){
                max = count;
                num = array[i];
            }
        }
    }
    
    cout << num << endl;
}

void arrayData(int array[]){
    for(int i=0; i<5; i++){
        cin >> array[i];
    }
    
    mode(array);
}

int main()
{
    int array[5];
    arrayData(array);

    return 0;
}

我想把上面的c++代码转换成cobol,我用的是gnuCobol。到目前为止我生成的代码如下。

IDENTIFICATION DIVISION.
PROGRAM-ID. Main.

DATA DIVISION.
    
WORKING-STORAGE SECTION.
    01 array.
        05 val PIC X(1) OCCURS 5 TIMES.

PROCEDURE DIVISION.
    CALL 'READDATA' USING array.
STOP RUN.

*> readData: fills the array with user diffined digits
IDENTIFICATION DIVISION.
PROGRAM-ID. READDATA.

WORKING-STORAGE SECTION. ***> error appears here**
    01 array.
        05 val PIC X(1) OCCURS 5 TIMES.

PROCEDURE DIVISION.
    PERFORM VARYING I FROM 1 BY 1
        UNTIL I > 5
        SET I TO 1
        ACCEPT array(I) FROM SYSIN.
        SET I UP BY 1
    END-PERFORM.
    
    CALL 'MODE' USING array.
    
EXIT PROGRAM.

*> mode: gets the number that appears most in the array
IDENTIFICATION DIVISION.
PROGRAM-ID. MODE.

WORKING-STORAGE SECTION.
    01 array.
        05 val PIC X(1) OCCURS 5 TIMES.
    01 maxv PIC(1) VALUE 0.
    01 counter PIC(1).
    01 num PIC(1).

PROCEDURE DIVISION.
    PERFORM VARYING I FROM 1 BY 1
        UNTIL I > 5
            MOVE 0 TO counter
        
            PERFORM VARYING J FROM 1 BY 1
            UNTIL J > 5
            
            IF array(I) = array(I) THEN
                SET counter UP BY 1
            END-IF
            
            IF maxv < counter THEN
                MOVE counter TO maxv
                MOVE array(I) TO num
            END-IF
            
            SET J UP BY 1
            END-PERFORM.
            
        SET I UP BY 1
    END-PERFORM.
    
    DISPLAY "Mode: "num.
EXIT PROGRAM.
 

我收到以下错误:

错误:语法错误,意外的“WORKING-STORAGE”,需要“END PROGRAM”或“PROGRAM-ID”

代码有几个问题。报告的错误似乎是缺少 END PROGRAM 将第一个程序与第二个程序分开的语句。

其他错误包括:

  • 不必要地使用 SET 语句

  • 在没有 LINKAGE SECTIONUSING 短语的情况下通过 array

  • 缺少 DATA DIVISION 个语句

  • 缺少数据项

  • 不正确的下标

  • 在范围终止符之前包含一个“分隔符”

  • 无效的 PICTURE 子句

这些问题显示在以下代码的注释中。比较原始代码和修改后的代码。


修改后的代码:

   IDENTIFICATION DIVISION.
   PROGRAM-ID. Main.

   DATA DIVISION.
        
   WORKING-STORAGE SECTION.
   01 array.
       05 val PIC X(1) OCCURS 5 TIMES.

   PROCEDURE DIVISION.
       CALL 'READDATA' USING array.
   STOP RUN.
   END PROGRAM MAIN. *> ADDED

   *> readData: fills the array with user diffined digits
   IDENTIFICATION DIVISION.
   PROGRAM-ID. READDATA.
   DATA DIVISION. *> ADDED
   WORKING-STORAGE SECTION. *> error appears here**
   01 I PIC 9.
   LINKAGE SECTION. *> ADDED
       01 array.
           05 val PIC X(1) OCCURS 5 TIMES.

   PROCEDURE DIVISION USING ARRAY. *> MODIFIED
       PERFORM VARYING I FROM 1 BY 1
           UNTIL I > 5
   *>      SET I TO 1 *> REMOVED
           ACCEPT VAL   (I) FROM SYSIN  *> MODIFIED
   *>      SET I UP BY 1 *> REMOVED
       END-PERFORM.
        
       CALL 'MO-DE' USING array.
        
       EXIT PROGRAM.
   END PROGRAM READDATA. *> ADDED

   *> mode: gets the number that appears most in the array
   IDENTIFICATION DIVISION.
   PROGRAM-ID. MO-DE. *> 'MODE' IS A RESERVED WORD
   DATA DIVISION. *> ADDED
   WORKING-STORAGE SECTION.
   01 maxv PIC 9 VALUE 0. *> MODIFIED
   01 counter PIC 9. *> MODIFIED
   01 num PIC 9. *> MODIFIED
   01 I PIC 9. *> ADDED
   01 J PIC 9. *> ADDED
   LINKAGE SECTION. *> ADDED
   01 array.
       05 val PIC X(1) OCCURS 5 TIMES.

   PROCEDURE DIVISION USING ARRAY. *> MODIFIED
       PERFORM VARYING I FROM 1 BY 1
           UNTIL I > 5
               MOVE 0 TO counter
            
               PERFORM VARYING J FROM 1 BY 1
               UNTIL J > 5
                
               IF VAL  (I) = VAL  (J) THEN *> MODIFIED
                   ADD 1 TO COUNTER *> MODIFIED
               END-IF
                
               IF maxv < counter THEN
                   MOVE counter TO maxv
                   MOVE VAL  (I) TO num
               END-IF
                
   *>          SET J UP BY 1 *> REMOVED
               END-PERFORM *> MODIFIED
                
   *>      SET I UP BY 1 *> REMOVED
       END-PERFORM.
        
       DISPLAY "Mode: "num.
       EXIT PROGRAM.
   END PROGRAM MO-DE. *> ADDED

输入:

1
5
5
2
3

输出:

Mode: 5