-Data Structure in C++ (Review for C++)
-using vector and class
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).
//registerMachine.cpp
#include<iostream>
#include<string>
#include<vector>
//presenly we have found commands, declare MAX as 6
#define MAX 6
using namespace std;
class register_machine
{
//declare registers
int a, b, c, d;
vector<string> commands;
public:
//initialize commands to impement while creating
object
register_machine(string command[],int );
int getCommandIndex(string str);
int getRegister(char a);
void aasign_register(char c, int value);
int get_value(char a);
void set_value(char ch,int value);
};
register_machine::register_machine(string command[],int
size)
{
a = 0, b = 0, c = 0, d = 0;
for (int i = 0; i < size; i++)
{
commands.push_back(command[i]);
}
}
int register_machine::getCommandIndex(string str)
{
//get index of the command in commands array of
string
for (int i = 0; i < MAX; i++)
{
if (str.compare(commands[i]) ==
0)
{
return i;
}
}
return -1;
}
int register_machine::getRegister(char ch)
{
switch (ch)
{
case 'a':
return 97;
break;
case 'b':
return 98;
case 'c':
return 99;
break;
case 'd':
return 100;
default:
return -1;
}
}
void register_machine::aasign_register(char ch, int value)
{
if (ch == 'a')
a = value;
if (ch == 'b')
b = value;
if (ch == 'c')
c = value;
if (ch == 'd')
d = value;
}
int register_machine::get_value(char ch)
{
if (ch == 'a')
return a;
if (ch == 'b')
return b;
if (ch == 'c')
return c;
if (ch == 'd')
return d;
}
void register_machine::set_value(char ch,int value)
{
if (ch == 'a')
a = value;
if (ch == 'b')
b = value;
if (ch == 'c')
c = value;
if (ch == 'd')
d = value;
}
int main()
{
//declare command set, u can add other commands when
you are extending the command set here
string commands[MAX] = { "store", "print", "cmp",
"add", "mul", "store" };
//initialize the object
register_machine machine(commands,MAX);
int choice,ret;
string input;
//loop for user inpute and exit when user press
0
do
{
cout << "Enter command to
process commands 2. stop to exit" << endl;
string command[5];
getline(cin ,input);
//process command entered by
user
int j = 0, k = 0,
entered_loop;
if (input.compare("stop") ==
0)
{
cout <<
"Exiting the application..." << endl;
break;
}
while( j < input.length())
{
entered_loop =
0;
while
(!isspace(input[j]) && input[j] != '\n' && j <
input.length())
{
//command[i][j++]=
input[i];
//cout << input[j]
<< endl;
command[k]+=input[j++];
entered_loop = 1;
}
if
(entered_loop)
k++;
j++;
}
ret =
machine.getCommandIndex(command[0]);
switch (ret)
{
case 0:
//strore command
if (command[2][0] == 'a')
machine.set_value('a',stoi(command[1]));
if (command[2][0] == 'b')
machine.set_value('b',
stoi(command[1]));
if (command[2][0] == 'c')
machine.set_value('c',
stoi(command[1]));
if (command[2][0] == 'd')
machine.set_value('d',
stoi(command[1]));
break;
case 1:
//print
command
if (command[1][0] == 'a')
printf("%d\n",
machine.get_value('a'));
if (command[1][0] == 'b')
printf("%d\n",
machine.get_value('b'));
if (command[1][0] == 'c')
printf("%d\n",
machine.get_value('c'));
if (command[1][0] == 'd')
printf("%d\n",
machine.get_value('d'));
break;
case 2:
//cmp command
int r1, r2, r3;
r1 = machine.getRegister(command[1][0]);
r2 = machine.getRegister(command[2][0]);
r3 = machine.getRegister(command[3][0]);
if (machine.get_value(r1) ==
machine.get_value(r2))
machine.aasign_register(r3,
0);
if (machine.get_value(r1) <
machine.get_value(r2))
machine.aasign_register(r3,
-1);
else
machine.aasign_register(r3,
1);
break;
case 3:
//add command
int num1, num2,r;
r1 = machine.getRegister(command[1][0]);
r2 = machine.getRegister(command[2][0]);
r3 = machine.getRegister(command[3][0]);
machine.aasign_register(r3,
machine.get_value(r1) + machine.get_value(r2));
break;
case 4:
//multiply
int n1, n2, reg1;
reg1 = machine.getRegister(command[3][0]);
r1 = machine.getRegister(command[1][0]);
r2 = machine.getRegister(command[2][0]);
r3 = machine.getRegister(command[3][0]);
machine.aasign_register(r3,
machine.get_value(r1) * machine.get_value(r2));
break;
default :
cout << "No command found" <<
endl;
}
} while (input.compare("stop")!=0);
}
-----------------------------------------------------------------------------------------------------------------------
Enter command to process commands 2. stop to exit
store 2 b
Enter command to process commands 2. stop to exit
print a
1
Enter command to process commands 2. stop to exit
print b
2
Enter command to process commands 2. stop to exit
add a b c
Enter command to process commands 2. stop to exit
print c
3
Enter command to process commands 2. stop to exit
comp a b d
No command found
Enter command to process commands 2. stop to exit
cmp a b d
Enter command to process commands 2. stop to exit
print d
-1
Enter command to process commands 2. stop to exit
stop
Exiting the application...
Get Answers For Free
Most questions answered within 1 hours.