Cobol代码通过工具自动生成java代码 展示版

打印 上一主题 下一主题

主题 861|帖子 861|积分 2583

本例是通过工具将cobol代码自动生成java代码。生成后的java代码是按照java编程风格生成的,完全屏蔽了cobol的特性。
一个cobol代码生成了4个java代码,分别说文件定义,变量定义,语句代码。
生成的代码有可执行程序,本例只展示生成的代码。
cobol代码
  1.       ******************************************************************
  2.       * Author:
  3.       * Date:
  4.       * Purpose:
  5.       * Tectonics: cobc
  6.       ******************************************************************
  7.        IDENTIFICATION DIVISION.
  8.       *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
  9.        PROGRAM-ID. YOUR-PROGRAM-NAME.
  10.        ENVIRONMENT DIVISION.
  11.       *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
  12.        CONFIGURATION SECTION.
  13.       *-----------------------
  14.        INPUT-OUTPUT SECTION.
  15.        FILE-CONTROL.
  16.            SELECT INPUTFILE1 ASSIGN TO
  17.              "E:\source\cobol\testdata\src\testfile\input.txt"
  18.              status fs.
  19.            SELECT OUTPUT1 ASSIGN TO
  20.              "E:\source\cobol\testdata\src\testfile\out.txt".
  21.       *-----------------------
  22.        DATA DIVISION.
  23.        FILE SECTION.
  24.        FD INPUTFILE1.
  25.        01  INRECORD.
  26.            02  ID1  PIC 9999.
  27.            02  NAME1 PIC XXXXXXXX.
  28.            02  KEMU1  PIC 9999.
  29.            02  CHENGJI1 PIC 99V9.
  30.        FD OUTPUT1.
  31.        01 OUTRECORD.
  32.            02 ID2 PIC 9999.
  33.            02 NAME2 PIC XXXXXXXX.
  34.            02 SUM2 PIC 999V9.
  35.            02 HEGE PIC X.
  36.       *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
  37.       *-----------------------
  38.        WORKING-STORAGE SECTION.
  39.        01 test1.
  40.            02 aa comp-2.
  41.        77  EOFMOD PIC 9.
  42.        77  FS PIC XX.
  43.        01  testred pic xxxx.
  44.        01  testred2   REDEFINES testred.
  45.            02 testred21 pic 9999.
  46.       *-----------------------
  47.        PROCEDURE DIVISION.
  48.       *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
  49.        DISPLAY "start exec ".
  50.        MAIN-PROCEDURE.
  51.       **
  52.       * The main procedure of the program
  53.       **
  54.            move all 'a' to test1.
  55.            display test1 "b".
  56.            DISPLAY "Hello world"
  57.            PERFORM CREATFILEDATA.
  58.       *合计学生di为1的成绩综合并输入到出力文件中
  59.            perform SUMCHEGNJI.
  60.             STOP RUN.
  61.        CREATFILEDATA.
  62.            OPEN OUTPUT INPUTFILE1.
  63.       * 1
  64.            MOVE 1 TO ID1.
  65.            ADD 1 TO 1 GIVING ID1.
  66.            MOVE '张三' to NAME1.
  67.            MOVE 1 TO KEMU1.
  68.            MOVE 80.5 TO CHENGJI1.
  69.            WRITE INRECORD.
  70.            DISPLAY 'TEST1'.
  71.       * 1
  72.            MOVE 1 TO ID1.
  73.            MOVE '张三' to NAME1.
  74.            MOVE 2 TO KEMU1.
  75.            MOVE 90 TO CHENGJI1.
  76.            DISPLAY 'TEST21' '.'.
  77.            IF CHENGJI1 <= 0 THEN
  78.                DISPLAY 'ERROR' ' END'
  79.                STOP RUN
  80.            END-IF.
  81.            WRITE INRECORD.
  82.            DISPLAY 'WRITE OK'.
  83.            CLOSE INPUTFILE1.
  84.            exit.
  85.        SUMCHEGNJI.
  86.       *    MOVE 1 TO ID2.
  87.       *    MOVE '张三' to NAME2.
  88.            MOVE INRECORD TO OUTRECORD.
  89.            move 0 to SUM2.
  90.            move ' ' to HEGE.
  91.            MOVE 0 TO EOFMOD.
  92.            MOVE '00' TO FS.
  93.            OPEN INPUT INPUTFILE1.
  94.            perform until FS NOT = '00'
  95.                READ INPUTFILE1
  96.                DISPLAY FS
  97.                IF FS = '00' THEN
  98.                    IF ID1 = 1 THEN
  99.                        ADD CHENGJI1 TO SUM2
  100.                   END-IF
  101.                END-IF
  102.            END-PERFORM.
  103.            CLOSE INPUTFILE1.
  104.            DISPLAY SUM2 .
  105.            PERFORM OUTDATA.
  106.        OUTDATA SECTION.
  107.            OPEN OUTPUT OUTPUT1.
  108.            IF SUM2 >= 120 THEN
  109.                MOVE 'A' TO HEGE
  110.            ELSE
  111.                MOVE 'D' TO HEGE
  112.            END-IF.
  113.            WRITE OUTRECORD.
  114.            CLOSE OUTPUT1.
  115.        text2.
  116.            exit.
  117.       ** add other procedures here
  118.        END PROGRAM YOUR-PROGRAM-NAME.
复制代码
  工具转换生成的java代码
1:文件结构
  1. package cobol.filevar;
  2. import java.math.BigDecimal;
  3. import comm.BaseFile;
  4. import comm.StringDataUtils;
  5. /**
  6. * a 工具转换生成类
  7. * a 数据文件使用的数据结构定义
  8. * @author lihaibo
  9. *
  10. */
  11. public class Inputfile1 extends BaseFile{
  12.     public int id1 = 0;
  13.     public String name1 = "";
  14.     public int kemu1 = 0;
  15.     public BigDecimal chengji1 = new BigDecimal(0);
  16.     public Inputfile1(String fileName) {
  17.         super(fileName);
  18.     }
  19.     public String getData() {
  20.         String rowData = "";
  21.         rowData += StringDataUtils.getData(id1, true);
  22.         rowData += StringDataUtils.getData(name1, false);
  23.         rowData += StringDataUtils.getData(kemu1, false);
  24.         rowData += StringDataUtils.getData(chengji1, false);
  25.         return rowData;
  26.     }
  27.     public void setData(String rowData) {
  28.         String[] arr = StringDataUtils.split(rowData);
  29.         id1 = StringDataUtils.getInt(arr, 0);
  30.         name1 = StringDataUtils.getStr(arr, 1);
  31.         kemu1 = StringDataUtils.getInt(arr, 2);
  32.         chengji1 = StringDataUtils.getBigDecimal(arr, 3);
  33.     }
  34. }
复制代码
  文件的定义2
  1. package cobol.filevar;
  2. import java.math.BigDecimal;
  3. import comm.BaseFile;
  4. import comm.StringDataUtils;
  5. /**
  6. * a 工具转换生成类
  7. * a 数据文件使用的数据结构定义
  8. * @author lihaibo
  9. *
  10. */
  11. public class Output1 extends BaseFile{
  12.     public int id2 = 0;
  13.     public String name2 = "";
  14.     public BigDecimal sum2 = new BigDecimal(0);
  15.     public String hege = "";
  16.     public Output1(String fileName) {
  17.         super(fileName);
  18.     }
  19.     public String getData() {
  20.         String rowData = "";
  21.         rowData += StringDataUtils.getData(id2, true);
  22.         rowData += StringDataUtils.getData(name2, false);
  23.         rowData += StringDataUtils.getData(sum2, false);
  24.         rowData += StringDataUtils.getData(hege, false);
  25.         return rowData;
  26.     }
  27.     public void setData(String rowData) {
  28.         String[] arr = StringDataUtils.split(rowData);
  29.         id2 = StringDataUtils.getInt(arr, 0);
  30.         name2 = StringDataUtils.getStr(arr, 1);
  31.         sum2 = StringDataUtils.getBigDecimal(arr, 2);
  32.         hege = StringDataUtils.getStr(arr, 3);
  33.     }
  34. }
复制代码
  
变量定义
  1. package cobol.src;
  2. import cobol.filevar.Inputfile1;
  3. import cobol.filevar.Output1;
  4. /**
  5. * a 工具转换生成类
  6. * a 程序用变量定义
  7. * @author lihaibo
  8. *
  9. */
  10. public class YourProgramNameVar{
  11.     // 文件对象变量
  12.     public Inputfile1 inputfile1 = new Inputfile1("E:\\source\\cobol\\testdata\\src\\testfile\\input.txt");
  13.     public Output1 output1 = new Output1("E:\\source\\cobol\\testdata\\src\\testfile\\out.txt");
  14.     // work内部对象变量
  15.     public int aa = 0;
  16.     public int eofmod = 0;
  17.     public String fs = "";
  18.     public int testred = 0;
  19.     // Param参数对象变量
  20. }
复制代码
  
执行代码
  1. package cobol.src;
  2. import java.math.BigDecimal;
  3. import cobol.filevar.Inputfile1;
  4. import cobol.filevar.Output1;
  5. import comm.FileOpenEnum;
  6. /**
  7. * a 工具转换生成类
  8. * a 程序转换后代码
  9. * @author lihaibo
  10. *
  11. */
  12. public class YourProgramNameSrc{
  13.     // 程序使用的变量对象
  14.     public YourProgramNameVar yourProgramNameVar = new YourProgramNameVar();
  15.     // 外部接口方法
  16.     public void mainExec(){
  17.         System.out.println("start exec ");
  18.         mainProcedure();
  19.     }
  20.     private int mainProcedure(){
  21.         yourProgramNameVar.aa = 0;
  22.         System.out.println(String.valueOf(yourProgramNameVar.aa) + "b");
  23.         System.out.println("Hello world");
  24.         if(creatfiledata() == 1){
  25.             return 1;
  26.         }
  27.         sumchegnji();
  28.         return 1;
  29.     }
  30.     private int creatfiledata(){
  31.         yourProgramNameVar.inputfile1.open(FileOpenEnum.OUTPUT);
  32.         yourProgramNameVar.inputfile1.id1 = 1;
  33.         yourProgramNameVar.inputfile1.id1 = 1 + 1;
  34.         yourProgramNameVar.inputfile1.name1 = "张三";
  35.         yourProgramNameVar.inputfile1.kemu1 = 1;
  36.         yourProgramNameVar.inputfile1.chengji1 = new BigDecimal(80.5);
  37.         yourProgramNameVar.inputfile1.writeLine(yourProgramNameVar.inputfile1.getData());
  38.         System.out.println("TEST1");
  39.         yourProgramNameVar.inputfile1.id1 = 1;
  40.         yourProgramNameVar.inputfile1.name1 = "张三";
  41.         yourProgramNameVar.inputfile1.kemu1 = 2;
  42.         yourProgramNameVar.inputfile1.chengji1 = new BigDecimal(90);
  43.         System.out.println("TEST21" + ".");
  44.         if(   yourProgramNameVar.inputfile1.chengji1.compareTo(new BigDecimal(0)) <= 0){
  45.             System.out.println("ERROR" + " END");
  46.             return 1;
  47.         }
  48.         yourProgramNameVar.inputfile1.writeLine(yourProgramNameVar.inputfile1.getData());
  49.         System.out.println("WRITE OK");
  50.         yourProgramNameVar.inputfile1.close();
  51.         return 0;
  52.     }
  53.     private void sumchegnji(){
  54.         yourProgramNameVar.output1.id2 = yourProgramNameVar.inputfile1.id1;
  55.         yourProgramNameVar.output1.name2 = yourProgramNameVar.inputfile1.name1;
  56.         yourProgramNameVar.output1.sum2 = new BigDecimal(0);
  57.         yourProgramNameVar.output1.hege = " ";
  58.         yourProgramNameVar.eofmod = 0;
  59.         yourProgramNameVar.fs = "00";
  60.         yourProgramNameVar.inputfile1.open(FileOpenEnum.INPUT);
  61.         while(!(   yourProgramNameVar.fs.compareTo("00") != 0)) {
  62.             yourProgramNameVar.inputfile1.readLine();
  63.             yourProgramNameVar.fs = yourProgramNameVar.inputfile1.statu1;
  64.             System.out.println(yourProgramNameVar.fs);
  65.             if(   yourProgramNameVar.fs.compareTo("00") == 0){
  66.                 if(   yourProgramNameVar.inputfile1.id1 == 1){
  67.                     yourProgramNameVar.output1.sum2 = yourProgramNameVar.output1.sum2.add(yourProgramNameVar.inputfile1.chengji1);
  68.                 }
  69.             }
  70.         }
  71.         yourProgramNameVar.inputfile1.close();
  72.         System.out.println(String.valueOf(yourProgramNameVar.output1.sum2));
  73.         outdata();
  74.     }
  75.     private void outdata(){
  76.         yourProgramNameVar.output1.open(FileOpenEnum.OUTPUT);
  77.         if(   yourProgramNameVar.output1.sum2.compareTo(new BigDecimal(120)) >= 0){
  78.             yourProgramNameVar.output1.hege = "A";
  79.         }else{
  80.             yourProgramNameVar.output1.hege = "D";
  81.         }
  82.         yourProgramNameVar.output1.writeLine(yourProgramNameVar.output1.getData());
  83.         yourProgramNameVar.output1.close();
  84.         outdataText2();
  85.     }
  86.     private void outdataText2(){
  87.     }
  88. }
复制代码
  
 

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

半亩花草

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表