include sstream include iostream include vector include algorithm incl

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
#include <sstream>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cctype>
using namespace std;
string to_bin(int n)
{
string res;
n &= (1<<17)-1;
while (n)
{
res = (char)(((n&1)?1:0)+48) +res;
n = n>>1;
}
while (res.length()<18)
res = '0' + res;
return res;
}
class Processor
{
public:
int R[4];
void write(int reg, int val)
{
val = val & ((1<<18)-1);
reg = val;
}
void print()
{
for (int i=0; i<4; i++)
{
cout << 'R' << i << ": " <<to_bin(R[i]) << endl;
}
}
};
bool isnumeric(string str)
{
istringstream inpstream(str);
int testvar = 0;
if (inpstream >> testvar)
{
return true;
}
else
{
return false;
}
}
vector<string> split_by_spaces(string line)
{
istringstream iss(line);
vector<string> result;
do
{
string subs;
iss >> subs;
result.push_back(subs);
} while (iss);
result.erase(result.end());
return result;
}
int main()
{
Processor proc;
string line;
while (getline(cin, line))
{
vector<string> in = split_by_spaces(line);
cout << endl;
if (in[0] == "add")
{
if ((in.size() != 3) || (!isnumeric(in[1])) ||
(!isnumeric(in[2])) || (atoi(in[1].c_str())>4) ||
(atoi(in[1].c_str())<0))
{
cout << "Wrong command" << endl;
continue;
}
proc.R[atoi(in[1].c_str())] += atoi(in[2].c_str());
}
else if (in[0] == "put")
{
if ((in.size() != 3) || (!isnumeric(in[1])) ||
(!isnumeric(in[2])) || (atoi(in[1].c_str())>4) ||
(atoi(in[1].c_str())<0))
{
cout << "Wrong command" << endl;
continue;
}
proc.R[atoi(in[1].c_str())] = atoi(in[2].c_str());
}
else
{
cout << "Wrong command" << endl;
continue;
}
proc.print();
}
}