Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
500 views
in Technique[技术] by (71.8m points)

algorithm - Automatic code generation that produces thousands of lines

For bureaucracy reasons, I need to submit the source code each month to the umbrella company I work with. They require to submit a lot of code monthly, but even if my customer allowed code sharing, I do not type that much code monthly because the product is in the maintenance phase. So the goal is to implement a source code generator that would produce thousands of lines of source code per month.

What I have tried: Hanoi towers. I implemented a code generator for writing the Hanoi tower moving program in a brute-force way. Below it is (in C++):

#include <fstream>
#include <iostream>
#include <exception>
#include <string>
using namespace std;

namespace {
  constexpr int gcLinesPerPage = 55;
  int gnRings, gLinesPerMonth;
  string gCodeMonk;
  int64_t gCurLineNo;
  int gCurMonth;
  ofstream gOfs;
}

void PrintUsageAndExit(int argc, char* argv[]) {
  cout << "Usage: " << argv[0] << " <nRings> <zlotysPerMonth> <CodeMonk>" << endl;
  exit(1);
}

void MoveRing(const int iSrc, const int iDest) {
  if (gCurLineNo > gLinesPerMonth) {
    gOfs << "  return 0;" << endl;
    gOfs << "}" << endl;
    gOfs.close();
    gCurMonth++;
    gCurLineNo = 0;
  }
  if (gCurLineNo == 0) {
    string fileName = "src";
    fileName += to_string(gCurMonth);
    fileName += ".cpp";
    gOfs.open(fileName);
    gOfs << "#include <iostream>" << endl;
    gOfs << "using namespace std;" << endl;
    gOfs << endl;
    gOfs << "int main() {" << endl;
  }
  gOfs << "  cout << "" << gCodeMonk << " moves the top ring from peg "
    << int(iSrc) << " to peg " << int(iDest) << "" << endl;" << endl;
  gCurLineNo++;
}

void Hanoi(const int nRings, const int8_t iSrc, const int8_t iSpare, const int8_t iDest) {
  if (nRings <= 0) {
    return;
  }
  if (nRings == 1) {
    MoveRing(iSrc, iDest);
    return;
  }
  Hanoi(nRings - 1, iSrc, iDest, iSpare);
  MoveRing(iSrc, iDest);
  Hanoi(nRings - 1, iSpare, iSrc, iDest);
}

int main(int argc, char* argv[]) {
  if (argc < 4) {
    PrintUsageAndExit(argc, argv);
  }
  try {
    gnRings = stoi(argv[1]);
    if (gnRings <= 0) {
      cout << "nRings must be a positive integer." << endl;
      return 1;
    }

    const int plnPerMonth = stoi(argv[2]);
    if (plnPerMonth <= 0) {
      cout << "zlotysPerMonth must be a positive integer." << endl;
      return 1;
    }
    gLinesPerMonth = int(ceil(plnPerMonth / 1000.0f)) * gcLinesPerPage;
    
    gCodeMonk = argv[3];

    gCurLineNo = 0;
    gCurMonth = 1;

    Hanoi(gnRings, 1, 2, 3);
  }
  catch (const exception& ex) {
    cout << "FATAL: " << ex.what() << endl;
  }
  return 0;
}

It generates programs as in the attached screenshot (cropped): enter image description here

However, now the umbrella company tells me they are tired of Hanoi towers. Can you suggest what else can be generated relatively simply and that produces a diverse codebase? You can see that the Hanoi tower program has mostly the same statement repeated in a different order.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...