Question

-Data Structure in C++ (Review for C++) -using vector and class In this assignment you’re going...

-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).

Homework Answers

Answer #1

//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...

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
1. Every class is compiled into a separate bytecode file that has the same name as...
1. Every class is compiled into a separate bytecode file that has the same name as the classand ends with the .class extension. True or False? 2. The variable x starts with the value 0.     The variable y starts with the value 5.     Add 1 to x.     Add 1 to y.     Add x and y, and store the result in y.     Display the value in y on the screen. A. 8 B. 9 C. 7...
For this assignment you need to write a parallel program in C++ using OpenMP for vector...
For this assignment you need to write a parallel program in C++ using OpenMP for vector addition. Assume A, B, C are three vectors of equal length. The program will add the corresponding elements of vectors A and B and will store the sum in the corresponding elements in vector C (in other words C[i] = A[i] + B[i]). Every thread should execute approximately equal number of loop iterations. The only OpenMP directive you are allowed to use is: #pragma...
For this assignment, you need to write a parallel program in C++ using OpenMP for vector...
For this assignment, you need to write a parallel program in C++ using OpenMP for vector addition. Assume A, B, C are three vectors of equal length. The program will add the corresponding elements of vectors A and B and will store the sum in the corresponding elements in vector C (in other words C[i] = A[i] + B[i]). Every thread should execute an approximately equal number of loop iterations. The only OpenMP directive you are allowed to use is:...
For this assignment, you need to write a parallel program in C++ using OpenMP for vector...
For this assignment, you need to write a parallel program in C++ using OpenMP for vector addition. Assume A, B, C are three vectors of equal length. The program will add the corresponding elements of vectors A and B and will store the sum in the corresponding elements in vector C (in other words C[i] = A[i] + B[i]). Every thread should execute an approximately equal number of loop iterations. The only OpenMP directive you are allowed to use is:...
(UNIX/LInux) Please download the file from course documents to do the following exercises after going through...
(UNIX/LInux) Please download the file from course documents to do the following exercises after going through the awk handout: 1. Print all the phone numbers. 2. Print Dan's phone number. 3. Print Susan's name and phone number. 4. Print all last names beginning with D. 5. Print all first names beginning with either a C or E. 6. Print all first names containing only four characters. 7. Print all first names of all those in the 916 area code. 8....
2. Consider the data set has four variables which are Y, X1, X2 and X3. Construct...
2. Consider the data set has four variables which are Y, X1, X2 and X3. Construct a multiple regression model using Y as response variable and other X variables as explanatory variables. (a) Write mathematics formulas (including the assumptions) and give R commands to obtain linear regression models for Y Xi, i =1, 2 and 3. (b) Write several lines of R commands to obtain correlations between Xi and Xj , i 6= j and i, j = 1, 2,...
You can complete this assignment individually or as a group of two people. In this assignment...
You can complete this assignment individually or as a group of two people. In this assignment you will create a ​​Sorted Singly-Linked List​ that performs basic list operations using C++. This linked list should not allow duplicate elements. Elements of the list should be of type ‘ItemType’. ‘ItemType’ class should have a private integer variable with the name ‘value’. Elements in the linked list should be sorted in the ascending order according to this ‘value’ variable. You should create a...
High-level computer languages are created to be understood by humans. As a result, the keywords and...
High-level computer languages are created to be understood by humans. As a result, the keywords and the commands of these languages are easy to understand. Machine languages are harder to understand and operate. For this assignment, you should assume that the memory cells at addresses F0 to F9 are in the machine described here: Op-Code Operand Description 1 RXY LOAD the register R with the bit pattern found in the memory cell whose address is XY 2 RXY LOAD the...
I did already posted this question before, I did get the answer but i am not...
I did already posted this question before, I did get the answer but i am not satisfied with the answer i did the code as a solution not the description as my solution, so i am reposting this question again. Please send me the code as my solution not the description In this project, build a simple Unix shell. The shell is the heart of the command-line interface, and thus is central to the Unix/C programming environment. Mastering use of...
In c++ create a class to maintain a GradeBook. The class should allow information on up...
In c++ create a class to maintain a GradeBook. The class should allow information on up to 3 students to be stored. The information for each student should be encapsulated in a Student class and should include the student's last name and up to 5 grades for the student. Note that less than 5 grades may sometimes be stored. Your GradeBook class should at least support operations to add a student record to the end of the book (i.e., the...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT