Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<vector>
- #include <math.h>
- using namespace std;
- char defV = '0';
- class Register {
- public:
- vector <char> reg;
- void setInitial(string s) {
- if (s[0] != '1') {
- for (int i = 1; i < s.length(); ++i)
- {
- reg.push_back(s[i]);
- }
- }
- else {
- for (int i = 1; i < s.length(); ++i) {
- if (s[i] == '1')
- {
- reg.push_back('0');
- }
- else {
- reg.push_back('1');
- }
- }
- }
- }
- void shiftLeft(int n) {
- if (reg.size() <= n)
- {
- for (int i = 0; i<reg.size(); i++)
- {
- reg[i] = defV;
- }
- }
- else {
- while(n--) {
- for (int i = 0; i < reg.size()-1; i++)
- {
- reg[i] = reg[i+1];
- }
- reg[reg.size()-1] = defV;
- }
- }
- }
- void shiftRight(int n) {
- if (reg.size() <= n)
- {
- for (int i = 0; i<reg.size(); i++)
- {
- reg[i] = defV;
- }
- }
- else {
- while(n--) {
- for (int i = reg.size()-1; i > 0; i--)
- {
- reg[i] = reg[i-1];
- }
- reg[0] = defV;
- }
- }
- }
- void setLength (int l) {
- if (reg.size() > 0) {
- reg.insert(reg.begin(),l-reg.size(),'0');
- }
- else {
- for (int i = 0; i < l; ++i)
- {
- reg.push_back('0');
- }
- }
- }
- void printBits () {
- for (int i = 0; i < reg.size(); ++i)
- {
- cout << reg[i];
- }
- cout << endl;
- }
- void printDecimal() {
- for (int i = 0; i < reg.size(); ++i)
- {
- cout << reg[i];
- }
- int decVal = 0;
- int j = 0;
- for (int i = reg.size()-1; i>=0; i--) {
- int num = reg[i] - '0';
- decVal += num*pow(2.0,j);
- j++;
- }
- cout << '(' << decVal << ')' << endl;
- }
- void printHexa() {
- for (int i = 0; i < reg.size(); ++i)
- {
- cout << reg[i];
- }
- unsigned int hexval = 0, i = 1, rem;
- for (int j = reg.size()-1; j>=0; j--) {
- rem = reg[j] - '0';
- hexval = hexval + rem * i;
- i *= 2;
- }
- printf("(%X)\n",hexval);
- }
- };
- int main(int argc, char const *argv[])
- {
- Register r1;
- Register r2;
- int count = argc - 1;
- string arguments[count];
- for (int i=1; i<argc; i++) {
- arguments[i-1] = argv[i];
- }
- int j = 0;
- bool isDefault = true;
- bool isDefault2 = true;
- while (j < count) {
- if (arguments[j] == "-i")
- {
- if (j+1 < count)
- {
- r1.setInitial(arguments[j+1]);
- isDefault = false;
- j+=2;
- }
- else {
- return -1;
- }
- }
- else if (arguments[j] == "-s")
- {
- if (j+1 < count && stoi(arguments[j+1]) >= r1.reg.size())
- {
- r1.setLength(stoi(arguments[j+1]));
- j+=2;
- }
- else {
- return -1;
- }
- }
- else if (arguments[j] == "-r")
- {
- if (j+1 < count)
- {
- r1.shiftRight(stoi(arguments[j+1]));
- j+=2;
- }
- else {
- return -1;
- }
- }
- else if (arguments[j] == "-l")
- {
- if (j+1 < count)
- {
- r1.shiftLeft(stoi(arguments[j+1]));
- j+=2;
- }
- else {
- return -1;
- }
- }
- else if (arguments[j] == "-v")
- {
- if (j+1 < count)
- {
- if(!arguments[j+1].compare("1"))
- {
- defV = '1';
- }
- else if(!arguments[j+1].compare("0"))
- {
- defV = '0';
- }
- j+=2;
- }
- else {
- return -1;
- }
- }
- else if (arguments[j] == "-p")
- {
- if (!isDefault && !isDefault2)
- {
- r1.printBits();
- r2.printBits();
- j++;
- }
- else {
- return -1;
- }
- }
- else if (arguments[j] == "-I")
- {
- if (j+1 < count)
- {
- r2.setInitial(arguments[j+1]);
- isDefault2 = false;
- j+=2;
- }
- else {
- return -1;
- }
- }
- else if (arguments[j] == "-S")
- {
- if (j+1 < count && stoi(arguments[j+1]) >= r2.reg.size())
- {
- r2.setLength(stoi(arguments[j+1]));
- j+=2;
- }
- else {
- return -1;
- }
- }
- else if (arguments[j] == "-R")
- {
- if (j+1 < count)
- {
- r2.shiftRight(stoi(arguments[j+1]));
- j+=2;
- }
- else {
- return -1;
- }
- }
- else if (arguments[j] == "-L")
- {
- if (j+1 < count)
- {
- r2.shiftLeft(stoi(arguments[j+1]));
- j+=2;
- }
- else {
- return -1;
- }
- }
- else if (arguments[j] == "-d") {
- if (!isDefault)
- {
- r1.printDecimal();
- }
- if (!isDefault2)
- {
- r2.printDecimal();
- }
- j++;
- }
- else if (arguments[j] == "-h") {
- if (!isDefault)
- {
- r1.printHexa();
- }
- if (!isDefault2)
- {
- r2.printHexa();
- }
- j++;
- }
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment