Advertisement
snowywhitee

Untitled

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