Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- ifstream fin("quack.in");
- ofstream fout("quack.out");
- vector <string> commands;
- class Quack {
- private:
- map <char, uint16_t> registers;
- queue <uint16_t> values;
- map <string, int> labels;
- public:
- void quackStart() {
- int i=0;
- uint16_t tmpint;
- string tmp;
- for(int j = 0; j < commands.size(); j++) {
- char a = commands[j][0];
- if(a == ':') {
- tmp = commands[j];
- tmp.erase(0, 1);
- labels[tmp] = j;
- continue;
- }
- }
- while(i < commands.size()) {
- switch (commands[i][0]) {
- case ':': {
- i++;
- continue;
- }
- case '+' : {
- tmpint = values.front();
- values.pop();
- tmpint += values.front();
- values.pop();
- values.push(tmpint % 65536);
- i++;
- continue;
- }
- case '-': {
- tmpint = values.front();
- values.pop();
- tmpint -= values.front();
- values.pop();
- values.push(tmpint % 65536);
- i++;
- continue;
- }
- case '*': {
- tmpint = values.front();
- values.pop();
- if(values.front()) tmpint *= values.front();
- else tmpint = 0;
- values.pop();
- values.push(tmpint % 65536);
- i++;
- continue;
- }
- case '/': {
- tmpint = values.front();
- values.pop();
- if(values.front()) tmpint = tmpint / values.front();
- else tmpint = 0;
- values.pop();
- values.push(tmpint % 65536);
- i++;
- continue;
- }
- case '%': {
- tmpint = values.front();
- values.pop();
- if(values.front()) tmpint %= values.front();
- else tmpint = 0;
- values.pop();
- values.push(tmpint % 65536);
- i++;
- continue;
- }
- case '>': {
- registers[commands[i][1] -'a'] = values.front();
- values.pop();
- i++;
- continue;
- }
- case '<': {
- values.push(registers[commands[i][1] -'a']);
- i++;
- continue;
- }
- case 'P': {
- if(commands[i].length() == 1) {
- fout << values.front() << endl;
- values.pop();
- }
- else fout << registers[commands[i][1] -'a'] << endl;
- i++;
- continue;
- }
- case 'Q': {
- return;
- }
- case 'C': {
- if(commands[i].length() == 1) {
- fout<<(char)(values.front() % 256);
- values.pop();
- }
- else fout<<(char)((registers[commands[i][1] - 'a'] % 256));
- i++;
- continue;
- }
- case 'J': {
- tmp = commands[i];
- tmp.erase(0, 1);
- i = labels[tmp];
- continue;
- }
- case 'Z': {
- if (!registers[commands[i][1] -'a']) {
- tmp = commands[i];
- tmp.erase(0, 2);
- i = labels[tmp];
- continue;
- }
- i++;
- continue;
- }
- case 'E': {
- if(registers[commands[i][1] -'a'] == registers[commands[i][2] -'a']) {
- tmp = commands[i];
- tmp.erase(0, 3);
- i = labels[tmp];
- continue;
- }
- i++;
- continue;
- }
- case 'G': {
- if(registers[commands[i][1] -'a'] > registers[commands[i][2] -'a']) {
- tmp = commands[i];
- tmp.erase(0, 3);
- i = labels[tmp];
- continue;
- }
- i++;
- continue;
- }
- default:
- values.push(stoi(commands[i]));
- i++;
- }
- }
- }
- };
- int main() {
- string command;
- vector <string> cmd;
- while(fin>>command) {
- commands.push_back(command);
- }
- Quack quack;
- quack.quackStart();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement