Data Structure in C Using vectors and class In this assignme
Data Structure in C++
Using vectors 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):
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
| 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 | 
Solution
#include<iostream>
 #include<string>
 //presenly we have found commands, declare MAX as 4
 #define MAX 6
 using namespace std;
int getCommandIndex(string command[],string str);
 int getRegister(char a);
 void aasign_register(char c, int value);
 int get_value(char a);
 //declare registers
 int a, b, c, d;
 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\" };
   
    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] != \'\ \' && 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 = getCommandIndex(commands,command[0]);
        switch (ret)
        {
            case 0:
                    //strore command
            if (command[2][0] == \'a\')
                a = stoi(command[1]);
            if (command[2][0] == \'b\')
                b = stoi(command[1]);
            if (command[2][0] == \'c\')
                c = stoi(command[1]);
            if (command[2][0] == \'d\')
                d = stoi(command[1]);
                break;
            case 1:
            //print command
            if (command[1][0] == \'a\')
                printf(\"%d\ \", a);
            if (command[1][0] == \'b\')
                printf(\"%d\ \", b);
            if (command[1][0] == \'c\')
                printf(\"%d\ \", c);
            if (command[1][0] == \'d\')
                printf(\"%d\ \", d);
                break;
            case 2:
                    //cmp command
                int r1, r2, r3;
                r1 = getRegister(command[1][0]);
                r2 = getRegister(command[2][0]);
            r3 = getRegister(command[3][0]);
                if (get_value(r1) == get_value(r2))
                    aasign_register(r3, 0);
                if (get_value(r1) < get_value(r2))
                    aasign_register(r3, -1);
                else
                    aasign_register(r3, 1);
                   
                break;
            case 3:
                    //add command
                int num1, num2,r;
                /*num1 = stoi(command[1]);
                num2 = stoi(command[2]);
               r = getRegister(command[3][0]);
                aasign_register(r, (num1 + num2));*/
                r1 = getRegister(command[1][0]);
           r2 = getRegister(command[2][0]);
                r3 = getRegister(command[3][0]);
                aasign_register(r3, get_value(r1) + get_value(r2));
               
                break;
            case 4:
                    //multiply
                int n1, n2, reg1;
                /*n1 = stoi(command[1]);
                n2 = stoi(command[2]);*/
                reg1 = getRegister(command[3][0]);
                r1 = getRegister(command[1][0]);
                r2 = getRegister(command[2][0]);
                r3 = getRegister(command[3][0]);
                aasign_register(r3, get_value(r1) * get_value(r2));
                break;
            default :
                cout << \"No command found\" << endl;
           }
           
       
    } while (input.compare(\"stop\")!=0);
 }
int getCommandIndex(string command[], string str)
 {
    //get index of the command in commands array of string
    for (int i = 0; i < MAX; i++)
    {
        if (str.compare(command[i]) == 0)
        {
            return i;
        }
    }
   return -1;
 }
 int getRegister(char a)
 {
    switch (a)
    {
        case \'a\':
            return 97;
            break;
        case \'b\':
                return 98;
       case \'c\':
                return 99;
                break;
        case \'d\':
                return 100;
        default:
            return -1;
    }
 }
 void 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 get_value(char ch)
 {
    if (ch == \'a\')
        return a;
    if (ch == \'b\')
        return b;
    if (ch == \'c\')
        return c;
    if (ch == \'d\')
        return c;
 }
----------------------------------------------------------------------------------------
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
 mul a c d
 Enter command to process commands 2. stop to exit
 print d
 3
 Enter command to process commands 2. stop to exit
 stop
 Exiting the application...




