I really need help with this.. please help.. Data Structures in C++ The original
ID: 3818917 • Letter: I
Question
I really need help with this.. please help..
Data Structures in C++
The original problem:
In this assignment you’re going to build a simple “register machine”, a kind of minimal computer that only supports storing a fixed number of values (i.e., no randomly-accessible “main memory”).
Your machine will consist of an input loop that reads lines of input from the user and then executes them, stopping when the user quits (by executing the stop command). Each line of input consists of a command followed by 1 to 3 arguments. Arguments can be either:
Immediate integer values (positive or negative)
Register names a through d (I.e., this machine only supports four registers)
For some commands, a register name may be required in certain argument positions. Here’s a sample transcript from the official solution implementation (lines starting with > are user input; everything else is output printed by the program):
> store 1 a
> store 2 b
> print a
1
> print b
2
> add a b c
> print c
3
> comp a b d
No command with that name exists
> cmp a b d
> print d
-1
> 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
Add x and y and store the result in d (x and y may be either, but d must be a register)
sub x y d
Subtract y from x and store the result in d
mul x y d
Multiply x and y and store the result in d
cmp x y d
Compare x and y and store into d
0 if they are equal, -1 if a < b, or 1 if a > b
Note that the arithmetic (add, sub, mul) and comparison (cmp) commands can take either immediates 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
Similarly, store’s first argument can be either an immediate or a register:
store a b
store 1 c
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).
The code doesnt accept anything and reads everything including the correct input to come out to error. How can I fix this? For example when i type in store 1 a, it reads it out as an error, when it is supposed to take this. Also my professor wants me to use functions so not everything is the main function. Is there a way to fix this?? thank you.
The code:
#include<iostream>
#include<map>
#include<algorithm>
#include<string.h>
using namespace std;
int main()
{
string cmd;
cin >> cmd;
int pp=0,c=0;
for(pp=0;cmd[pp]!='';pp++)
{
if(cmd[pp]==' ')c++;
if(c>0)
{
}
}
map<string,int> commands;
commands.insert(make_pair("stop",0));
commands.insert(make_pair("store",1));
commands.insert(make_pair("print",2));
commands.insert(make_pair("add",3));
commands.insert(make_pair("sub",4));
commands.insert(make_pair("mul",5));
commands.insert(make_pair("cmp",6));
int commandType;
std::map<string,int>::iterator itr;
itr = commands.find(cmd);
if(itr!= commands.end())
commandType = itr->second;
else
cout << "this commands not exist" << endl;
//cout <<"command Type is: " <<commandType << endl;
map<char,int> reg;
reg.insert(make_pair('a',-999));
reg.insert(make_pair('b',-999));
reg.insert(make_pair('c',-999));
reg.insert(make_pair('d',-999));
while(commandType != 0)
{
std::map<char,int>::iterator it;
switch(commandType)
{
case 1:
{
int value;
char regist;
cin >> value >> regist;
//modified
if(value!='a'||value!='b'||value!='c'||value!='d')
{
cout<<"Error:invalid command entered";
return 0;
}
it = reg.find(regist);
it->second = value;
break;
}
case 2:
{
char r;
cin >> r;
//modified
if(r!='a'||r!='b'||r!='c'||r!='d')
{
cout<<"Command format worng!!!";
return 0;
}
it = reg.find(r);
cout << it -> second << endl;
break;
}
case 3:
{
char x,y;
char r;
cin >> x >> y >> r;
//modified
if(x!='a'||x!='b'||x!='c'||x!='d')
{
cout<<"Command format worng!!!";
return 0;
}
if(y!='a'||y!='b'||y!='c'||y!='d')
{
cout<<"Command format worng!!!";
return 0;
}
it = reg.find(r);
it->second = (reg.find(x)->second + reg.find(y)->second);
break;
}
case 4:
{
char x,y;
char r;
cin >> x >> y >> r;
//modified
if(x!='a'||x!='b'||x!='c'||x!='d')
{
cout<<"Command format worng!!!";
return 0;
}
if(y!='a'||y!='b'||y!='c'||y!='d')
{
cout<<"Command format worng!!!";
return 0;
}
it = reg.find(r);
it->second = (reg.find(y)->second - reg.find(x)->second);;
break;
}
case 5:
{
char x,y;
char r;
cin >> x >> y >> r;
//modified
if(x!='a'||x!='b'||x!='c'||x!='d')
{
cout<<"Command format worng!!!";
return 0;
}
if(y!='a'||y!='b'||y!='c'||y!='d')
{
cout<<"Command format worng!!!";
return 0;
}
it = reg.find(r);
it->second = (reg.find(x)->second * reg.find(y)->second);
break;
}
case 6:
{
char x,y;
char r;
cin >> x >> y >> r;
//modified
if(x!='a'||x!='b'||x!='c'||x!='d')
{
cout<<"Command format worng!!!";
return 0;
}
if(y!='a'||y!='b'||y!='c'||y!='d')
{
cout<<"Command format worng!!!";
return 0;
}
it = reg.find(r);
int val1 = reg.find(x)->second;
int val2 = reg.find(y)->second;
if(val1 == val2)
it->second = 0;
else if(val1 < val2)
it->second = -1;
else
it->second = 1;
break;
}
default:
{
{
break;
}
}
cin >> cmd;
std::map<string,int>::iterator itr;
itr = commands.find(cmd);
if(itr!= commands.end())
commandType = itr->second;
else
{
char x,y,z;
cin >> x >> y>> z ;
cout << "No command with that name exist" << endl;
commandType = -1;
}
}
return 0;
}}
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
Add x and y and store the result in d (x and y may be either, but d must be a register)
sub x y d
Subtract y from x and store the result in d
mul x y d
Multiply x and y and store the result in d
cmp x y d
Compare x and y and store into d
0 if they are equal, -1 if a < b, or 1 if a > b
Explanation / Answer
#include<iostream>
#include<map>
#include<algorithm>
#include<string.h>
using namespace std;
int main()
{
string cmd;
cin >> cmd;
int pp=0,c=0;
for(pp=0;cmd[pp]!='';pp++)
{
if(cmd[pp]==' ')c++;
if(c>0)
{
}
}
map<string,int> commands;
commands.insert(make_pair("stop",0));
commands.insert(make_pair("store",1));
commands.insert(make_pair("print",2));
commands.insert(make_pair("add",3));
commands.insert(make_pair("sub",4));
commands.insert(make_pair("mul",5));
commands.insert(make_pair("cmp",6));
int commandType = 0;
std::map<string,int>::iterator itr;
itr = commands.find(cmd);
if(itr!= commands.end())
commandType = itr->second;
else
cout << "this commands not exist" << endl;
map<char,int> reg;
reg.insert(make_pair('a',-999));
reg.insert(make_pair('b',-999));
reg.insert(make_pair('c',-999));
reg.insert(make_pair('d',-999));
while(commandType != 0)
{
std::map<char,int>::iterator it;
cout <<"command Type is: " <<commandType << endl;
switch(commandType)
{
case 1:
{
int value;
char regist;
cin >> value >> regist;
//modified
//cout << "value :"<<value <<" regist:"<< regist << endl;
if(value=='a'||value=='b'||value=='c'||value=='d')
{
cout<<"Error:invalid command entered";
return 0;
}
it = reg.find(regist);
it->second = value;
break;
}
case 2:
{
char r;
cin >> r;
//modified
//cout << "r :"<<r <<endl;
if(r!='a'&&r!='b'&&r!='c'&&r!='d')
{
cout<<"Command format worng!!!";
return 0;
}
it = reg.find(r);
cout << it -> second << endl;
break;
}
case 3:
{
char x,y;
char r;
cin >> x >> y >> r;
//modified
if(x!='a'&&x!='b'&&x!='c'&&x!='d')
{
cout<<"Command format worng!!!";
return 0;
}
if(y!='a'&&y!='b'&&y!='c'&&y!='d')
{
cout<<"Command format worng!!!";
return 0;
}
it = reg.find(r);
it->second = (reg.find(x)->second + reg.find(y)->second);
break;
}
case 4:
{
char x,y;
char r;
cin >> x >> y >> r;
//modified
if(x!='a'&&x!='b'&&x!='c'&&x!='d')
{
cout<<"Command format worng!!!";
return 0;
}
if(y!='a'&&y!='b'&&y!='c'&&y!='d')
{
cout<<"Command format worng!!!";
return 0;
}
it = reg.find(r);
it->second = (reg.find(y)->second - reg.find(x)->second);;
break;
}
case 5:
{
char x,y;
char r;
cin >> x >> y >> r;
//modified
if(x!='a'&&x!='b'&&x!='c'&&x!='d')
{
cout<<"Command format worng!!!";
return 0;
}
if(y!='a'&&y!='b'&&y!='c'&&y!='d')
{
cout<<"Command format worng!!!";
return 0;
}
it = reg.find(r);
it->second = (reg.find(x)->second * reg.find(y)->second);
break;
}
case 6:
{
char x,y;
char r;
cin >> x >> y >> r;
//modified
if(x!='a'&&x!='b'&&x!='c'&&x!='d')
{
cout<<"Command format worng!!!";
return 0;
}
if(y!='a'&&y!='b'&&y!='c'&&y!='d')
{
cout<<"Command format worng!!!";
return 0;
}
it = reg.find(r);
int val1 = reg.find(x)->second;
int val2 = reg.find(y)->second;
if(val1 == val2)
it->second = 0;
else if(val1 < val2)
it->second = -1;
else
it->second = 1;
break;
}
default:
{
break;
}
}
cin >> cmd;
std::map<string,int>::iterator itr;
itr = commands.find(cmd);
if(itr!= commands.end())
commandType = itr->second;
else
{
char x,y,z;
cin >> x >> y>> z ;
cout << "No command with that name exist" << endl;
commandType = -1;
}
}
return 0;
}