This is by far the worse assignment I've ever done. It was the code I had to modify. I had to copy and paste it because the program I used was being evil at the time.
#include <cstdlib>
#include <cstdio>
#include <cassert>
#include <cstring>
using namespace std;
const int MAXLINES = 1000;
struct DataLine {
int location;
int value;
} dataLine[MAXLINES];
typedef enum CodeTag { INVALID=0, LABEL, CODE, CODEI, CODES, END } CodeTag;
const int OPLEN = 8+1;
const int STRARGLEN = 25+1;
struct CodeLine {
CodeTag tag;
char opCode[OPLEN];
int intArg;
char strArg[STRARGLEN];
} codeLine[MAXLINES];
const int LINETYPELEN = 8+1; // |.comment| + '\0'
const char COMMENTLINE[] = ".comment";
const char DATALINE[] = ".data";
const char LABELLINE[] = ".label";
const char CODELINE[] = ".code";
const char CODEILINE[] = ".codeI";
const char CODESLINE[] = ".codeS";
const char ENDLINE[] = ".end";
const int STREQ = 0;
int main() {
int nextData = 0;
int nextCode = 0;
for (int lineNum = 0;
MAXLINES > lineNum && !feof(stdin);
lineNum+=1) {
char lineType[LINETYPELEN];
lineType[0] = '\0';
fscanf(stdin, "%8s", lineType);
if (STREQ == strcmp(lineType, COMMENTLINE)) {
;
} else if (STREQ == strcmp(lineType, DATALINE)) {
assert(MAXLINES > nextData);
fscanf(stdin, "%d %d", &dataLine[nextData].location, &dataLine[nextData].value);
if (0 > dataLine[nextData].location) {
fprintf(stderr, "error: negative location %d\n", dataLine[nextData].location);
}
nextData += 1;
} else {
assert (MAXLINES > nextCode);
if (STREQ == strcmp(lineType, ENDLINE)) {
codeLine[nextCode].tag = END;
} else if (STREQ == strcmp(lineType, LABELLINE)) {
codeLine[nextCode].tag = LABEL;
fscanf(stdin, "%25s %d", codeLine[nextCode].strArg, &codeLine[nextCode].intArg);
} else if (STREQ == strncmp(lineType, CODELINE, strlen(CODELINE))) {
fscanf(stdin, "%8s", codeLine[nextCode].opCode);
if (STREQ == strcmp(lineType, CODEILINE)) {
codeLine[nextCode].tag = CODEI;
fscanf(stdin, "%d", &codeLine[nextCode].intArg);
} else if (STREQ == strcmp(lineType, CODESLINE)) {
codeLine[nextCode].tag = CODES;
fscanf(stdin, "%25s", codeLine[nextCode].strArg);
} else {
codeLine[nextCode].tag = CODE;
}
} else if (0 == strlen(lineType)) {
;
}
nextCode += 1;
}
char ignored[BUFSIZ];
fgets(ignored, BUFSIZ, stdin);
}
int maxDataLocation = -1;
fprintf(stdout, "The data are:\n");
for (int i=0; i<nextData; i+=1) {
fprintf(stdout, "\t.data %d %d\n", dataLine.location, dataLine.value);
if (dataLine.location > maxDataLocation) { maxDataLocation = dataLine.location; }
}
fprintf(stdout, "with maximum %d\n", maxDataLocation);
fprintf(stdout, "The code lines are\n");
for (int i=0; i<nextCode; i+=1) {
switch (codeLine.tag) {
case LABEL: fprintf(stdout, " .label %s %d\n", codeLine.strArg, codeLine.intArg); break;
case END: fprintf(stdout, " .end\n"); break;
case CODE: fprintf(stdout, " .code %s\n", codeLine.opCode); break;
case CODEI: fprintf(stdout, " .codeI %s %d\n", codeLine.opCode, codeLine.intArg); break;
case CODES: fprintf(stdout, " .codeS %s %s\n", codeLine.opCode, codeLine.strArg); break;
default: fprintf(stderr, " invalid code line type at %d\n", nextCode); ; break;
}
}
return EXIT_SUCCESS;
}
From what a friend tells me this is a code compiler for another language. o.O
I'm not going to enjoy modifying that.