Advertisement
snowywhitee

Untitled

Dec 6th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.04 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.             uint16_t z = (x + y);
  46.             quack.push( z );
  47.         } else if (commands[it][0] == '-')
  48.         {
  49.             uint16_t x = quack.front();
  50.             quack.pop();
  51.             uint16_t y = quack.front();
  52.             quack.pop();
  53.             uint16_t z = (x - y);
  54.             quack.push( z );
  55.         } else if (commands[it][0] == '*')
  56.         {
  57.             uint16_t x = quack.front();
  58.             quack.pop();
  59.             uint16_t y = quack.front();
  60.             quack.pop();
  61.             uint16_t z = (x * y);
  62.             quack.push( z );
  63.         } else if (commands[it][0] == '/')
  64.         {
  65.             uint16_t x = quack.front();
  66.             quack.pop();
  67.             uint16_t y = quack.front();
  68.             quack.pop();
  69.             if (y == 0)
  70.             {
  71.                 uint16_t z = 0;
  72.                 quack.push( z );
  73.             } else {
  74.                 uint16_t z = (x / y);
  75.                 quack.push( z );
  76.             }
  77.         } else if (commands[it][0] == '%')
  78.         {
  79.             uint16_t x = quack.front();
  80.             quack.pop();
  81.             uint16_t y = quack.front();
  82.             quack.pop();
  83.             if (y == 0)
  84.             {
  85.                 uint16_t z = 0;
  86.                 quack.push( z );
  87.             } else {
  88.                 uint16_t z = (x % y);
  89.                 quack.push( z );
  90.             }
  91.         } else if (commands[it][0] == 'J')
  92.         {
  93.             string label = commands[it].substr(1, commands[it].length());
  94.             for (int i = 0; i < labels.size(); i++)
  95.             {
  96.                 if (labels[i].second == label)
  97.                 {
  98.                     it = labels[i].first;
  99.                 }
  100.             }
  101.         } else if (commands[it][0] == '>')
  102.         {
  103.             uint16_t x = quack.front();
  104.             quack.pop();
  105.             registers[commands[it][1] - 'a'] = x ;
  106.             //NOT ALL
  107.         } else if (commands[it][0] == '<')
  108.         {
  109.             quack.push(registers[commands[it][1] - 'a']);
  110.         } else if (commands[it][0] == 'P')
  111.         {
  112.             if (commands[it].length() == 1)
  113.             {
  114.                 fout << quack.front() << endl;
  115.                 quack.pop();
  116.             } else {
  117.                 fout << registers[commands[it][1] - 'a'] << endl;
  118.             }
  119.         } else if (commands[it][0] == 'Z')
  120.         {
  121.             if (registers[commands[it][1] - 'a'] == 0)
  122.             {
  123.                 string label = commands[it].substr(2, commands[it].length());
  124.                 for (int i = 0; i < labels.size(); i++)
  125.                 {
  126.                     if (labels[i].second == label)
  127.                     {
  128.                         it = labels[i].first;
  129.                     }
  130.                 }
  131.             }
  132.         } else if (commands[it][0] == 'E')
  133.         {
  134.             if (registers[commands[it][1] - 'a'] == registers[commands[it][2] - 'a'])
  135.             {
  136.                 string label = commands[it].substr(3, commands[it].length());
  137.                 for (int i = 0; i < labels.size(); i++)
  138.                 {
  139.                     if (labels[i].second == label)
  140.                     {
  141.                         it = labels[i].first;
  142.                     }
  143.                 }
  144.             }
  145.         } else if (commands[it][0] == 'G')
  146.         {
  147.             if (registers[commands[it][1] - 'a'] > registers[commands[it][2] - 'a'])
  148.             {
  149.                 string label = commands[it].substr(3, commands[it].length());
  150.                 for (int i = 0; i < labels.size(); i++)
  151.                 {
  152.                     if (labels[i].second == label)
  153.                     {
  154.                         it = labels[i].first;
  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.         } else if ( istringstream ( commands[it] ) >> num )
  172.         {
  173.             quack.push(num );
  174.         }
  175.         it++;
  176.     }
  177.    
  178.     return 0;
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement