Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

( Please Help Urgently ) Can anyone show me a different way of writing this code in C++, Thanks #include #include using namespace std; const

( Please Help Urgently ) Can anyone show me a different way of writing this code in C++, Thanks

#include  #include  using namespace std; const int SIZE = 100; const int MAX_WORD = 9999, MIN_WORD = -9999; const long SENTITEL = -99999; enum Commands { READ = 10,WRITE , LOAD = 20, STORE, ADD = 30, SUBTRACT , DIVIDE , MULTIPLY, BRANCH = 40, BRANCHNEG, BRANCHZERO, HALT }; void load(int * const); void execute(int * const, int * const, int * const, int * const, int * const, int * const); void dump(const int *const, int, int, int, int, int); bool validWord(int); int main() { int memory[SIZE] = { 0 }; int accumulator = 0, instuctionCounter = 0, instructionRegister = 0, opCode = 0, operand = 0; load(memory); execute(memory, &accumulator,&instuctionCounter,&instructionRegister,&opCode,&operand); dump(memory, accumulator, instuctionCounter, instructionRegister, opCode, operand); system("pause"); //cin.get(); return 0; } void load(int *const loadMemory) { long instruction; int i = 0; cout << "*** Welcome to Simpletron *** " << "*** Please Enter your Program one instrution *** " << "*** (or Data word) at a time. I will type the *** " << "*** location number and question mark (?). *** " << "*** You then type the word for that location. *** " << "*** Type the senitel -99999 to stop entering *** " << "*** your Program *** " << "00 ? "; cin >> instruction; while (instruction != SENTITEL) { if (!validWord(instruction)) cout << "Number Out of range. Please enter a valid Number "; else loadMemory[i++] = instruction; //function setfill sets the padding character for unused filled width cout << setw(2) << setfill('0') << i << " ? "; cin >> instruction; } } bool validWord(int word) { return word >= MIN_WORD && word <= MAX_WORD; } void execute(int * const memory, int * const acPtr, int * const icPtr, int * const irPtr, int * const opCodePtr, int * const opPtr) { bool fatal = false; int temp; const char *messages[] = { "Accumulatoroverflow ***", "Attempt to devide by Zero ***", "Invalid op Code Detected ***"}, *termString = " *** Simpletron execution abnormally Terminated ***", *fatalString = " *** FATAL ERROR:"; cout << " ****START SIMPLETRON EXECUTION*********** "; do { *irPtr = memory[*icPtr]; *opCodePtr = *irPtr / 100; *opPtr = *irPtr % 100; switch (*opCodePtr) { case READ: cout << "Enter an integer: "; cin >> temp; while (!validWord(temp)) { cout << "Number out of Range.Please Enter again: "; cin >> temp; } memory[*opPtr] = temp; ++(*icPtr); break; case WRITE: cout << "Content of " << setw(2) << setfill('0') << *opPtr << ": " << memory[*opPtr] << ' '; ++(*icPtr); break; case LOAD: *acPtr = memory[*opPtr]; ++(*icPtr); break; case STORE: memory[*opPtr] = *acPtr; ++(*icPtr); break; case ADD: temp = *acPtr + memory[*opPtr]; if (!validWord(temp)) { cout << fatalString << messages[0] << termString << ' '; fatal = true; } else { *acPtr = temp; ++(*icPtr); } break; case SUBTRACT: temp = *acPtr - memory[*opPtr]; if (!validWord(temp)) { cout << fatalString << messages[0] << termString << ' '; fatal = true; } else { *acPtr = temp; ++(*icPtr); } break; case DIVIDE: if (memory[*opPtr] == 0) { cout << fatalString << messages[1] << termString << ' '; fatal = true; } else { *acPtr = temp; ++(*icPtr); } break; case MULTIPLY: temp = *acPtr * memory[*opPtr]; if (!validWord(temp)) { cout << fatalString << messages[0] << termString << ' '; fatal = true; } else { *acPtr = temp; ++(*icPtr); } break; case BRANCH: *icPtr = *opPtr; break; case BRANCHNEG: *acPtr < 0 ? *icPtr = *opPtr : ++(*icPtr); break; case BRANCHZERO: *acPtr == 0 ? *icPtr = *opPtr : ++(*icPtr); break; case HALT: cout << "***************SIMPLETRON EXECUTION TERMINATED************** "; break; default: cout << fatalString << messages[2] << termString << ' '; fatal = true; break; } } while (*opCodePtr != HALT && !fatal); cout << " *********** END SIMPLETRON EXECTION**************** "; } void dump(const int * const memory, int accumulator, int instructionCounter, int instructionRegister, int operationalCode, int operand) { void output(const char *const, int, int, bool);//prototype cout << " REGISTERS: "; output("accumulator", 5, accumulator, true); output("instructionCounter", 2, instructionCounter, false); output("instructionRegister", 5, instructionRegister, true); output("operationalCode", 2, operationalCode, false); output("operand", 2, operand, false); cout << " MEMORY : "; int i = 0; cout << setfill(' ') << setw(3)<<' '; //print header for (; i <= 9; ++i) { cout << setw(5) << i << ' '; } for (i = 0; i < SIZE; ++i) { if (i % 10 == 0) { cout << ' ' << setw(2) << i << ' '; } cout << setiosflags(ios::internal | ios::showpos) << setw(5) << setfill('0') << memory[i] << ' ' << resetiosflags(ios::internal | ios::showpos); } cout << endl; } void output(const char * const sPtr, int width, int value, bool sign) { cout << setfill(' ') << setiosflags(ios::left) << setw(20) << sPtr << ' '; //is a +/- Sign Needed? if (sign) cout << setiosflags(ios::showpos | ios::internal); //setup for displaying accumulator value etc cout << resetiosflags(ios::left) << setfill('0'); //determine the field width and dispay value if (width == 5) cout << setw(width) << value << ' '; else //width is 2 cout << setfill(' ') << setw(3) << ' ' << setw(width) << setfill('0') << value << ' '; //disable sign if it was set if (sign) cout << resetiosflags(ios::showpos | ios::internal); }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle 11G SQL

Authors: Joan Casteel

2nd Edition

1133947360, 978-1133947363

More Books

Students also viewed these Databases questions