Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <queue>
- #include <fstream>
- #include <vector>
- #include <sstream>
- using namespace std;
- int main()
- {
- queue<uint16_t> quack;
- ifstream fin("quack.in");
- ofstream fout("quack.out");
- vector < uint16_t > registers (26, 0);
- vector < pair < int , string > > labels;
- vector < string > commands;
- string command;
- int it = -1;
- while(fin >> command)
- {
- commands.push_back(command);
- it++;
- if (command[0] == ':')
- {
- pair < int, string > pair1;
- pair1.first = it;
- pair1.second = command.substr(1, command.length());
- labels.push_back(pair1);
- }
- }
- it = 0;
- while( it < commands.size())
- {
- uint16_t num;
- // cout << commands[it] << ", it: " << it << endl;
- if (commands[it][0] == '+')
- {
- uint16_t x = quack.front();
- quack.pop();
- uint16_t y = quack.front();
- quack.pop();
- uint16_t z = (x + y);
- quack.push( z );
- } else if (commands[it][0] == '-')
- {
- uint16_t x = quack.front();
- quack.pop();
- uint16_t y = quack.front();
- quack.pop();
- uint16_t z = (x - y);
- quack.push( z );
- } else if (commands[it][0] == '*')
- {
- uint16_t x = quack.front();
- quack.pop();
- uint16_t y = quack.front();
- quack.pop();
- uint16_t z = (x * y);
- quack.push( z );
- } else if (commands[it][0] == '/')
- {
- uint16_t x = quack.front();
- quack.pop();
- uint16_t y = quack.front();
- quack.pop();
- if (y == 0)
- {
- uint16_t z = 0;
- quack.push( z );
- } else {
- uint16_t z = (x / y);
- quack.push( z );
- }
- } else if (commands[it][0] == '%')
- {
- uint16_t x = quack.front();
- quack.pop();
- uint16_t y = quack.front();
- quack.pop();
- if (y == 0)
- {
- uint16_t z = 0;
- quack.push( z );
- } else {
- uint16_t z = (x % y);
- quack.push( z );
- }
- } else if (commands[it][0] == 'J')
- {
- string label = commands[it].substr(1, commands[it].length());
- for (int i = 0; i < labels.size(); i++)
- {
- if (labels[i].second == label)
- {
- it = labels[i].first;
- }
- }
- } else if (commands[it][0] == '>')
- {
- uint16_t x = quack.front();
- quack.pop();
- registers[commands[it][1] - 'a'] = x ;
- //NOT ALL
- } else if (commands[it][0] == '<')
- {
- quack.push(registers[commands[it][1] - 'a']);
- } else if (commands[it][0] == 'P')
- {
- if (commands[it].length() == 1)
- {
- fout << quack.front() << endl;
- quack.pop();
- } else {
- fout << registers[commands[it][1] - 'a'] << endl;
- }
- } else if (commands[it][0] == 'Z')
- {
- if (registers[commands[it][1] - 'a'] == 0)
- {
- string label = commands[it].substr(2, commands[it].length());
- for (int i = 0; i < labels.size(); i++)
- {
- if (labels[i].second == label)
- {
- it = labels[i].first;
- }
- }
- }
- } else if (commands[it][0] == 'E')
- {
- if (registers[commands[it][1] - 'a'] == registers[commands[it][2] - 'a'])
- {
- string label = commands[it].substr(3, commands[it].length());
- for (int i = 0; i < labels.size(); i++)
- {
- if (labels[i].second == label)
- {
- it = labels[i].first;
- }
- }
- }
- } else if (commands[it][0] == 'G')
- {
- if (registers[commands[it][1] - 'a'] > registers[commands[it][2] - 'a'])
- {
- string label = commands[it].substr(3, commands[it].length());
- for (int i = 0; i < labels.size(); i++)
- {
- if (labels[i].second == label)
- {
- it = labels[i].first;
- }
- }
- }
- } else if (commands[it][0] == 'C')
- {
- if (commands[it].length() == 0)
- {
- uint16_t x = quack.front() % 256;
- quack.pop();
- fout << static_cast<char>(x);
- } else {
- fout << static_cast<char>(registers[commands[it][1] - 'a']);
- }
- } else if (commands[it][0] == 'Q')
- {
- break;
- } else if ( istringstream ( commands[it] ) >> num )
- {
- quack.push(num );
- }
- it++;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement