Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Data Structure in C++ A simple “register machine”, Output I need: --------------

ID: 3798042 • Letter: D

Question

Data Structure in C++

A simple “register machine”,

Output I need:

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

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<iostream>
#include<sstream>
#include<string>
#include<map>
#include<algorithm>
using std::string;
using std::vector;

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

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

void print(string reg){
std::cout << rmap[reg] << std::endl;
}

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] ) < (rmap[r2])){
rmap[result] = -1;
}
}
};

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<string> 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 << "No command with that name exists" << endl;
}
}
}

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)

Explanation / Answer

Only single error that you didn't include the vector file #include <vector> due to which vector was not getting declared

Modified code: