//============================================================================ /
ID: 3600105 • Letter: #
Question
//============================================================================
// Name : shiftregister.cpp
// Author :
// Version :
// Copyright : Your copyright notice
// Description : shiftregister in C++, Ansi-style
//============================================================================
#include <iostream>
#include <cstdlib>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
class RegisterOp
{
private:
vector<char> reg;
public:
int size;
RegisterOp(int si);
int insert_register(string s1);
int right_shift_op(int num_rs, char vvalue);
int left_shift_op(int num_ls, char vvalue);
int getregistersizeValue();
void setregistersizeValue(int val);
int print_regis();
};
RegisterOp::RegisterOp(int s1)
{
size = s1;
}
int RegisterOp::insert_register(string s1)
{
for (int i = 0; i < size; i++)
{
reg.push_back(s1[i]);
}
reverse(reg.begin(), reg.end());
return 0;
}
int RegisterOp::right_shift_op(int num_rs, char vvalue)
{
reg.erase(reg.begin(), reg.begin()+num_rs);
for (int i = 0; i < num_rs; i++)
{
reg.push_back(vvalue);
}
return 0;
}
int RegisterOp::left_shift_op(int num_ls, char vvalue)
{
reg.erase(reg.end()-num_ls, reg.end());
for (int i = 0; i < num_ls; i++)
{
reg.insert(reg.begin(), vvalue);
}
return 0;
}
in the above class extend the shift register class to support the plus (‘+’) and minus (‘-‘) operator. The semantic of those operators would be that of binary addition and subtraction. You most modify your command line argument parser to support passing two registers as follows: (ARGUMENTS MUST BE SUPPORTED IN ANY ORDER.)
by default your register must inject 0 values unless told otherwise via command line arguments.
Here are some of the parameters to support as your command line arguments:
-i “010101”: Initial values stored in your register1. . This option must accept a positive or negative integer and covert it to binary.
-s 6: Specifies the number of bits in your shift register1.
-r 2: shift the register1 right by two positions.
-l 3: shift left register1 by 3 positions.
-I “010101”: Initial values stored in your register2. This option must accept a positive or negative integer and covert it to binary.
-S 6: Specifies the number of bits in your shift register2.
-R 2: shift the register2 right by two positions.
-L 3: shift left register2 by 3 positions.
-o +/-: operator to be used among the registers if any
-v 1: value to inject in vacated bits if other than default.
-d: if this option is given the resulting registers are followed by their representation in Decimal as follows: 0111(7)
-h: as above but in hex:1111(0xF)
-p: prints the value of bits in your register after performing all the operations. Bits must be printed as a non-spaced string of 0 or 1s and terminated by a new line. One line for register1, one line for register2 and the last line contains the result in binary. Implement the << operator in your register class so you can output using cout << register
Explanation / Answer
#include <iostream>
#include <cstdlib>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
class RegisterOp
{
private:
vector<char> reg;
public:
int size;
RegisterOp(int si);
int insert_register(string s1);
int right_shift_op(int num_rs, char vvalue);
int left_shift_op(int num_ls, char vvalue);
int getregistersizeValue();
void setregistersizeValue(int val);
int print_regis();
};
RegisterOp::RegisterOp(int s1)
{
size = s1;
}
int RegisterOp::insert_register(string s1)
{
for (int i = 0; i < size; i++)
{
reg.push_back(s1[i]);
}
reverse(reg.begin(), reg.end());
return 0;
}
int RegisterOp::right_shift_op(int num_rs, char vvalue)
{
reg.erase(reg.begin(), reg.begin()+num_rs);
for (int i = 0; i < num_rs; i++)
{
reg.push_back(vvalue);
}
return 0;
}
int RegisterOp::left_shift_op(int num_ls, char vvalue)
{
reg.erase(reg.end()-num_ls, reg.end());
for (int i = 0; i < num_ls; i++)
{
reg.insert(reg.begin(), vvalue);
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.