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;
}
}
}
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:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.