Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <vector>
- #include <queue>
- #include <map>
- using namespace std;
- typedef unsigned short int uint16;
- struct Queue
- {
- queue<uint16> q;
- uint16 get()
- {
- uint16 result = q.front();
- q.pop();
- return result;
- }
- void put(uint16 value)
- {
- q.push((uint16) (value % 65536));
- }
- };
- int main()
- {
- vector<string> source;
- string line;
- while (getline(cin, line))
- {
- source.push_back(line);
- }
- Queue queue;
- map<string, int> labels;
- vector<uint16> registers(26);
- for (int i = 0; i < source.size(); i++)
- {
- if (source[i][0] == ':')
- {
- labels[source[i].substr(1)] = i;
- }
- }
- uint16 x, y;
- for (int i = 0; i < source.size(); i++)
- {
- string command = source[i];
- switch (command[0])
- {
- case '+':
- x = queue.get();
- y = queue.get();
- queue.put(x + y);
- break;
- case '-':
- x = queue.get();
- y = queue.get();
- queue.put(x - y);
- break;
- case '*':
- x = queue.get();
- y = queue.get();
- queue.put(x * y);
- break;
- case '/':
- x = queue.get();
- y = queue.get();
- queue.put(y != 0 ? x / y : 0);
- break;
- case '%':
- x = queue.get();
- y = queue.get();
- queue.put(y != 0 ? x % y : 0);
- break;
- case '>':
- registers[command[1] - 'a'] = queue.get();
- break;
- case '<':
- queue.put(registers[command[1] - 'a']);
- break;
- case 'P':
- if (command.length() == 1)
- cout << queue.get() << endl;
- else
- cout << registers[command[1] - 'a'] << endl;
- break;
- case 'C':
- if (command.length() == 1)
- cout << (unsigned char) queue.get();
- else
- cout << (unsigned char) registers[command[1] - 'a'];
- break;
- case ':':
- break;
- case 'J':
- i = labels[command.substr(1)];
- break;
- case 'Z':
- if (registers[command[1] - 'a'] == 0)
- {
- i = labels[command.substr(2)];
- }
- break;
- case 'E':
- if (registers[command[1] - 'a'] == registers[command[2] - 'a'])
- {
- i = labels[command.substr(3)];
- }
- break;
- case 'G':
- if (registers[command[1] - 'a'] > registers[command[2] - 'a'])
- {
- i = labels[command.substr(3)];
- }
- break;
- case 'Q':
- i = source.size();
- break;
- default:
- queue.put(atoi(command.c_str()));
- break;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement