Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Data Structure in C++ A simple register machine, Output I need: ------------------------------------------------------------------------------------------------------------------------ 1. Note that your version will allow the use of any string as

Data Structure in C++

A simple register machine,

Output I need:

image text in transcribed

------------------------------------------------------------------------------------------------------------------------

1. Note that your version will allow the use of any string as a register name, and will create new registers as required (or just crash if you request the value of a nonexistent register).

2. All the arithmetic/comparison commands should be able to take either a number or a register name as their first two arguments. e.g., the following are all valid commands:

add 1 2 a add a 2 b add 2 b c add b c d

3. Please add the required comments before classes and methods (as described in the coding standard).

Please fix the above problems.

This is my code below, but I need to fix the above problems(1, 2, 3).

-------------------------------------------------------------------------------------------------------------------------------------------

#include #include #include #include #include using std::string; using std::vector;

// std::vector regval[4]; // 0:a, 1:b , 2:c, 3:d

class command { public: std::map rmap; void store(int val, string reg){ rmap[reg] = val; }

void print(string reg){ std::cout

void add(string r1, string r2, string result){ rmap[result] = rmap[r1] + rmap[r2]; }

void sub(string r1, string r2, string result){ rmap[result] = rmap[r1] - rmap[r2]; }

void mul(string r1, string r2, string result){ rmap[result] = (rmap[r1]) * (rmap[r2]); }

void cmp(string r1, string r2, string result){ if((rmap[r1] ) == (rmap[r2])){ rmap[result] = 0; } if((rmap[r1] ) > (rmap[r2])){ rmap[result] = 1; } if((rmap[r1] )

int main(){ using std::cin; using std::cout; using std::endl; string line; command execute = command(); while(true){ cout "; getline(cin,line); string word; // Have a buffer string std::stringstream ss(line); // Insert the string into a stream vector words; // Create vector to hold our words while (ss >> word) words.push_back(word);

if(words[0] == "store"){ int val = atoi(words[1].c_str()); // Change string into$ execute.store(val, words[2]); }

else if(words[0] == "print"){ execute.print(words[1]); }

else if(words[0] == "add"){ execute.add(words[1], words[2], words[3]); }

else if(words[0] == "sub"){ execute.sub(words[1], words[2], words[3]); }

else if(words[0] == "mul"){ execute.mul(words[1], words[2], words[3]); }

else if(words[0] == "cmp"){ execute.cmp(words[1], words[2], words[3]); }

else if(words[0] == "stop"){ return 0; }

else{ cout store 1 a store 2 b print a print b add a b c print c comp a b d No command with that name exists cmp a b d print d stop Command Description store x r Store x into register r(a. b. c or d) print. r Print the value in register r add x y d Addxand yand store the result in d (xand ymay be either, but d must be a register) sub x y d Subtract y from Xand store the result in d mul x y d Multiply x and yand store the result in d Compare xand vand store into d cmp x y d b O if they are equal, -1 if a b. or 1 if a Note that the arithmetic add. sub mul) and comparison (cmp) commands can take either values or register names as their first two parameters. E.g., the following are all valid commands: add 1 2 a add a 2 b add 1 b c add b c d You may assume that command names and arguments are always separated by at least one space character. You should bear in mind that we might want to add new commands later on. so think about how to make your program easily extensible in that way (e.g., a huge if-else chain is not very extensible)

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

More Books

Students also viewed these Databases questions

Question

How do Data Types perform data validation?

Answered: 1 week ago

Question

How does Referential Integrity work?

Answered: 1 week ago