Advertisement
Guest User

Untitled

a guest
Dec 7th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.32 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. ifstream fin("quack.in");
  5. ofstream fout("quack.out");
  6. vector <string> commands;
  7.  
  8. class Quack {
  9. private:
  10.     map <char, uint16_t> registers;
  11.     queue <uint16_t> values;
  12.     map <string, int> labels;
  13. public:
  14.     void quackStart() {
  15.         int i=0;
  16.         uint16_t tmpint;
  17.         string tmp;
  18.  
  19.         for(int j = 0; j < commands.size(); j++) {
  20.             char a = commands[j][0];
  21.             if(a == ':') {
  22.                 tmp = commands[j];
  23.                 tmp.erase(0, 1);
  24.                 labels[tmp] = j;
  25.                 continue;
  26.             }
  27.         }
  28.  
  29.         while(i < commands.size()) {
  30.             switch (commands[i][0]) {
  31.                 case ':': {
  32.                     i++;
  33.                     continue;
  34.                 }
  35.                 case '+' : {
  36.                     tmpint = values.front();
  37.                     values.pop();
  38.                     tmpint += values.front();
  39.                     values.pop();
  40.                     values.push(tmpint % 65536);
  41.                     i++;
  42.                     continue;
  43.                 }
  44.  
  45.                 case '-': {
  46.                     tmpint = values.front();
  47.                     values.pop();
  48.                     tmpint -= values.front();
  49.                     values.pop();
  50.                     values.push(tmpint % 65536);
  51.                     i++;
  52.                     continue;
  53.                 }
  54.  
  55.                 case '*': {
  56.                     tmpint = values.front();
  57.                     values.pop();
  58.                     if(values.front()) tmpint *= values.front();
  59.                     else tmpint = 0;
  60.                     values.pop();
  61.                     values.push(tmpint % 65536);
  62.                     i++;
  63.                     continue;
  64.                 }
  65.  
  66.                 case '/': {
  67.                     tmpint = values.front();
  68.                     values.pop();
  69.                     if(values.front()) tmpint = tmpint / values.front();
  70.                     else tmpint = 0;
  71.                     values.pop();
  72.                     values.push(tmpint % 65536);
  73.                     i++;
  74.                     continue;
  75.                 }
  76.  
  77.                 case '%': {
  78.                     tmpint = values.front();
  79.                     values.pop();
  80.                     if(values.front()) tmpint %= values.front();
  81.                     else tmpint = 0;
  82.                     values.pop();
  83.                     values.push(tmpint % 65536);
  84.                     i++;
  85.                     continue;
  86.                 }
  87.  
  88.                 case '>': {
  89.                     registers[commands[i][1] -'a'] = values.front();
  90.                     values.pop();
  91.                     i++;
  92.                     continue;
  93.                 }
  94.  
  95.                 case '<': {
  96.                     values.push(registers[commands[i][1] -'a']);
  97.                     i++;
  98.                     continue;
  99.                 }
  100.  
  101.                 case 'P': {
  102.                     if(commands[i].length() == 1) {
  103.                         fout << values.front() << endl;
  104.                         values.pop();
  105.                     }
  106.                     else fout << registers[commands[i][1] -'a'] << endl;
  107.                     i++;
  108.                     continue;
  109.                 }
  110.  
  111.                 case 'Q': {
  112.                     return;
  113.                 }
  114.  
  115.                 case 'C': {
  116.                     if(commands[i].length() == 1) {
  117.                         fout<<(char)(values.front() % 256);
  118.                         values.pop();
  119.                     }
  120.                     else fout<<(char)((registers[commands[i][1] - 'a'] % 256));
  121.                     i++;
  122.                     continue;
  123.                 }
  124.  
  125.                 case 'J': {
  126.                     tmp = commands[i];
  127.                     tmp.erase(0, 1);
  128.                     i = labels[tmp];
  129.                     continue;
  130.                 }
  131.                 case 'Z': {
  132.                     if (!registers[commands[i][1] -'a']) {
  133.                         tmp = commands[i];
  134.                         tmp.erase(0, 2);
  135.                         i = labels[tmp];
  136.                         continue;
  137.                     }
  138.                     i++;
  139.                     continue;
  140.                 }
  141.                 case 'E': {
  142.                     if(registers[commands[i][1] -'a'] == registers[commands[i][2] -'a']) {
  143.                         tmp = commands[i];
  144.                         tmp.erase(0, 3);
  145.                         i = labels[tmp];
  146.                         continue;
  147.                     }
  148.                     i++;
  149.                     continue;
  150.                 }
  151.                 case 'G': {
  152.                     if(registers[commands[i][1] -'a'] > registers[commands[i][2] -'a']) {
  153.                         tmp = commands[i];
  154.                         tmp.erase(0, 3);
  155.                         i = labels[tmp];
  156.                         continue;
  157.                     }
  158.                     i++;
  159.                     continue;
  160.                 }
  161.  
  162.                 default:
  163.                     values.push(stoi(commands[i]));
  164.                     i++;
  165.             }
  166.         }
  167.     }
  168. };
  169.  
  170. int main() {
  171.     string command;
  172.     vector <string> cmd;
  173.      
  174.     while(fin>>command) {
  175.         commands.push_back(command);
  176.     }
  177.     Quack quack;
  178.     quack.quackStart();
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement