本例是通过工具将cobol代码自动生成java代码。生成后的java代码是按照java编程风格生成的,完全屏蔽了cobol的特性。

一个cobol代码生成了4个java代码,分别说文件定义,变量定义,语句代码。

生成的代码有可执行程序,本例只展示生成的代码。

cobol代码

      ******************************************************************
      * Author:
      * Date:
      * Purpose:
      * Tectonics: cobc
      ******************************************************************
       IDENTIFICATION DIVISION.
      *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
       PROGRAM-ID. YOUR-PROGRAM-NAME.
       ENVIRONMENT DIVISION.
      *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
       CONFIGURATION SECTION.
      *-----------------------
       INPUT-OUTPUT SECTION.
       FILE-CONTROL.
           SELECT INPUTFILE1 ASSIGN TO
             "E:sourcecoboltestdatasrctestfileinput.txt"
             status fs.
           SELECT OUTPUT1 ASSIGN TO
             "E:sourcecoboltestdatasrctestfileout.txt".
      *-----------------------
       DATA DIVISION.
       FILE SECTION.
       FD INPUTFILE1.
       01  INRECORD.
           02  ID1  PIC 9999.
           02  NAME1 PIC XXXXXXXX.
           02  KEMU1  PIC 9999.
           02  CHENGJI1 PIC 99V9.
       FD OUTPUT1.
       01 OUTRECORD.
           02 ID2 PIC 9999.
           02 NAME2 PIC XXXXXXXX.
           02 SUM2 PIC 999V9.
           02 HEGE PIC X.

      *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
      *-----------------------
       WORKING-STORAGE SECTION.
       01 test1.
           02 aa comp-2.
       77  EOFMOD PIC 9.
       77  FS PIC XX.
       01  testred pic xxxx.
       01  testred2   REDEFINES testred.
           02 testred21 pic 9999.
      *-----------------------
       PROCEDURE DIVISION.
      *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
       DISPLAY "start exec ".
       MAIN-PROCEDURE.
      **
      * The main procedure of the program
      **
           move all 'a' to test1.
           display test1 "b".
           DISPLAY "Hello world"
           PERFORM CREATFILEDATA.

      *合计学生di为1的成绩综合并输入到出力文件中
           perform SUMCHEGNJI.
            STOP RUN.

       CREATFILEDATA.
           OPEN OUTPUT INPUTFILE1.

      * 1
           MOVE 1 TO ID1.
           ADD 1 TO 1 GIVING ID1.
           MOVE '张三' to NAME1.
           MOVE 1 TO KEMU1.
           MOVE 80.5 TO CHENGJI1.
           WRITE INRECORD.
           DISPLAY 'TEST1'.
      * 1
           MOVE 1 TO ID1.
           MOVE '张三' to NAME1.
           MOVE 2 TO KEMU1.
           MOVE 90 TO CHENGJI1.

           DISPLAY 'TEST21' '.'.
           IF CHENGJI1 <= 0 THEN
               DISPLAY 'ERROR' ' END'
               STOP RUN
           END-IF.

           WRITE INRECORD.
           DISPLAY 'WRITE OK'.
           CLOSE INPUTFILE1.
           exit.
       SUMCHEGNJI.
      *    MOVE 1 TO ID2.
      *    MOVE '张三' to NAME2.
           MOVE INRECORD TO OUTRECORD.
           move 0 to SUM2.
           move ' ' to HEGE.
           MOVE 0 TO EOFMOD.
           MOVE '00' TO FS.

           OPEN INPUT INPUTFILE1.
           perform until FS NOT = '00'

               READ INPUTFILE1
               DISPLAY FS
               IF FS = '00' THEN
                   IF ID1 = 1 THEN
                       ADD CHENGJI1 TO SUM2
                  END-IF
               END-IF
           END-PERFORM.

           CLOSE INPUTFILE1.
           DISPLAY SUM2 .
           PERFORM OUTDATA.
       OUTDATA SECTION.
           OPEN OUTPUT OUTPUT1.
           IF SUM2 >= 120 THEN
               MOVE 'A' TO HEGE
           ELSE
               MOVE 'D' TO HEGE
           END-IF.
           WRITE OUTRECORD.
           CLOSE OUTPUT1.
       text2.
           exit.

      ** add other procedures here
       END PROGRAM YOUR-PROGRAM-NAME.

  工具转换生成的java代码

1:文件结构

package cobol.filevar;

import java.math.BigDecimal;
import comm.BaseFile;
import comm.StringDataUtils;

/**
 * a 工具转换生成类
 * a 数据文件使用的数据结构定义
 * @author lihaibo
 *
 */
public class Inputfile1 extends BaseFile{
    public int id1 = 0;
    public String name1 = "";
    public int kemu1 = 0;
    public BigDecimal chengji1 = new BigDecimal(0);

    public Inputfile1(String fileName) {
        super(fileName);
    }

    public String getData() {
        String rowData = "";

        rowData += StringDataUtils.getData(id1, true);
        rowData += StringDataUtils.getData(name1, false);
        rowData += StringDataUtils.getData(kemu1, false);
        rowData += StringDataUtils.getData(chengji1, false);
        return rowData;
    }

    public void setData(String rowData) {
        String[] arr = StringDataUtils.split(rowData);

        id1 = StringDataUtils.getInt(arr, 0);
        name1 = StringDataUtils.getStr(arr, 1);
        kemu1 = StringDataUtils.getInt(arr, 2);
        chengji1 = StringDataUtils.getBigDecimal(arr, 3);
    }

}

  文件的定义2

package cobol.filevar;

import java.math.BigDecimal;
import comm.BaseFile;
import comm.StringDataUtils;

/**
 * a 工具转换生成类
 * a 数据文件使用的数据结构定义
 * @author lihaibo
 *
 */
public class Output1 extends BaseFile{
    public int id2 = 0;
    public String name2 = "";
    public BigDecimal sum2 = new BigDecimal(0);
    public String hege = "";

    public Output1(String fileName) {
        super(fileName);
    }

    public String getData() {
        String rowData = "";

        rowData += StringDataUtils.getData(id2, true);
        rowData += StringDataUtils.getData(name2, false);
        rowData += StringDataUtils.getData(sum2, false);
        rowData += StringDataUtils.getData(hege, false);
        return rowData;
    }

    public void setData(String rowData) {
        String[] arr = StringDataUtils.split(rowData);

        id2 = StringDataUtils.getInt(arr, 0);
        name2 = StringDataUtils.getStr(arr, 1);
        sum2 = StringDataUtils.getBigDecimal(arr, 2);
        hege = StringDataUtils.getStr(arr, 3);
    }

}

  

变量定义

package cobol.src;

import cobol.filevar.Inputfile1;
import cobol.filevar.Output1;

/**
 * a 工具转换生成类
 * a 程序用变量定义
 * @author lihaibo
 *
 */
public class YourProgramNameVar{
    // 文件对象变量
    public Inputfile1 inputfile1 = new Inputfile1("E:\source\cobol\testdata\src\testfile\input.txt");
    public Output1 output1 = new Output1("E:\source\cobol\testdata\src\testfile\out.txt");
    // work内部对象变量
    public int aa = 0;
    public int eofmod = 0;
    public String fs = "";
    public int testred = 0;
    // Param参数对象变量

}

  

执行代码

package cobol.src;

import java.math.BigDecimal;
import cobol.filevar.Inputfile1;
import cobol.filevar.Output1;
import comm.FileOpenEnum;

/**
 * a 工具转换生成类
 * a 程序转换后代码
 * @author lihaibo
 *
 */
public class YourProgramNameSrc{
    // 程序使用的变量对象
    public YourProgramNameVar yourProgramNameVar = new YourProgramNameVar();

    // 外部接口方法
    public void mainExec(){
        System.out.println("start exec ");
        mainProcedure();
    }


    private int mainProcedure(){
        yourProgramNameVar.aa = 0;
        System.out.println(String.valueOf(yourProgramNameVar.aa) + "b");
        System.out.println("Hello world");
        if(creatfiledata() == 1){
            return 1;
        }
        sumchegnji();
        return 1;
    }

    private int creatfiledata(){
        yourProgramNameVar.inputfile1.open(FileOpenEnum.OUTPUT);
        yourProgramNameVar.inputfile1.id1 = 1;
        yourProgramNameVar.inputfile1.id1 = 1 + 1;
        yourProgramNameVar.inputfile1.name1 = "张三";
        yourProgramNameVar.inputfile1.kemu1 = 1;
        yourProgramNameVar.inputfile1.chengji1 = new BigDecimal(80.5);
        yourProgramNameVar.inputfile1.writeLine(yourProgramNameVar.inputfile1.getData());
        System.out.println("TEST1");
        yourProgramNameVar.inputfile1.id1 = 1;
        yourProgramNameVar.inputfile1.name1 = "张三";
        yourProgramNameVar.inputfile1.kemu1 = 2;
        yourProgramNameVar.inputfile1.chengji1 = new BigDecimal(90);
        System.out.println("TEST21" + ".");
        if(   yourProgramNameVar.inputfile1.chengji1.compareTo(new BigDecimal(0)) <= 0){
            System.out.println("ERROR" + " END");
            return 1;
        }
        yourProgramNameVar.inputfile1.writeLine(yourProgramNameVar.inputfile1.getData());
        System.out.println("WRITE OK");
        yourProgramNameVar.inputfile1.close();
        return 0;
    }

    private void sumchegnji(){
        yourProgramNameVar.output1.id2 = yourProgramNameVar.inputfile1.id1;
        yourProgramNameVar.output1.name2 = yourProgramNameVar.inputfile1.name1;
        yourProgramNameVar.output1.sum2 = new BigDecimal(0);
        yourProgramNameVar.output1.hege = " ";
        yourProgramNameVar.eofmod = 0;
        yourProgramNameVar.fs = "00";
        yourProgramNameVar.inputfile1.open(FileOpenEnum.INPUT);
        while(!(   yourProgramNameVar.fs.compareTo("00") != 0)) {
            yourProgramNameVar.inputfile1.readLine();
            yourProgramNameVar.fs = yourProgramNameVar.inputfile1.statu1;
            System.out.println(yourProgramNameVar.fs);
            if(   yourProgramNameVar.fs.compareTo("00") == 0){
                if(   yourProgramNameVar.inputfile1.id1 == 1){
                    yourProgramNameVar.output1.sum2 = yourProgramNameVar.output1.sum2.add(yourProgramNameVar.inputfile1.chengji1);
                }
            }
        }
        yourProgramNameVar.inputfile1.close();
        System.out.println(String.valueOf(yourProgramNameVar.output1.sum2));
        outdata();
    }

    private void outdata(){
        yourProgramNameVar.output1.open(FileOpenEnum.OUTPUT);
        if(   yourProgramNameVar.output1.sum2.compareTo(new BigDecimal(120)) >= 0){
            yourProgramNameVar.output1.hege = "A";
        }else{
            yourProgramNameVar.output1.hege = "D";
        }
        yourProgramNameVar.output1.writeLine(yourProgramNameVar.output1.getData());
        yourProgramNameVar.output1.close();
        outdataText2();
    }

    private void outdataText2(){
    }

}

  

 

内容来源于网络如有侵权请私信删除

文章来源: 博客园

原文链接: https://www.cnblogs.com/shenvsxian/p/16403568.html

你还没有登录,请先登录注册
  • 还没有人评论,欢迎说说您的想法!