Advertisement
Guest User

Untitled

a guest
Jul 28th, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.47 KB | None | 0 0
  1. #include <fstream>
  2. #include <string>
  3.  
  4. const int I_SIZE = 1024;
  5. const int M_SIZE = 4;
  6. const int HASH_SIZE = 1 << 15;
  7. const int HASH_PRIME = 257;
  8. const int MODULO = 65536;
  9. const int MEM_SIZE = 26;
  10.  
  11. using namespace std;
  12.  
  13. template< class T > class vector
  14. {
  15.    int size;
  16.    int alloc_size;
  17.    T* data;
  18.  
  19. public:
  20.  
  21.    int get_size()
  22.    {
  23.       return size;
  24.    }
  25.  
  26.    T& operator[](int n)
  27.    {
  28.       return data[n];
  29.    }  
  30.    
  31.    vector()
  32.    {
  33.       data = new T[I_SIZE];
  34.       alloc_size = I_SIZE;
  35.       size = 0;
  36.    }
  37.    
  38.    T peek()
  39.    {
  40.       return data[size - 1];
  41.    }
  42.    
  43.    T pull()
  44.    {
  45.       int result = data[--size];
  46.       return result;
  47.    }
  48.    
  49.    void pop()
  50.    {
  51.       size--;
  52.       if (alloc_size > M_SIZE && size <= alloc_size / 4)
  53.       {
  54.          shrink();
  55.       }
  56.    }
  57.    
  58.    void push(T a)
  59.    {
  60.       if (size >= alloc_size) grow();
  61.       data[size++] = a;
  62.    }
  63.    
  64.    bool is_empty()
  65.    {
  66.       return (size == 0);
  67.    }
  68.    
  69.    ~vector()
  70.    {
  71.       delete[] data;
  72.    }
  73.    
  74. private:
  75.    void grow()
  76.    {
  77.       T* new_data = new T[alloc_size * 2];
  78.       for (int i = 0; i < size; i++)
  79.       {
  80.          new_data[i] = data[i];
  81.       }
  82.       delete[] data;
  83.       data = new_data;
  84.       alloc_size *= 2;
  85.    }
  86.    
  87.    void shrink()
  88.    {
  89.       T* new_data = new T[alloc_size / 2];
  90.       for (int i = 0; i < size; i++)
  91.       {
  92.          new_data[i] = data[i];
  93.       }
  94.       delete[] data;
  95.       data = new_data;
  96.       alloc_size /= 2;
  97.    }
  98. };
  99.  
  100. template< class T > class queue
  101. {
  102.    vector< T > stack_push, stack_pop;
  103. public:
  104.    T pop()
  105.    {
  106.       if (stack_pop.is_empty()) while (!stack_push.is_empty())
  107.       {
  108.          stack_pop.push(stack_push.pull());
  109.       }
  110.       return stack_pop.pull();
  111.    }
  112.    
  113.    void push(T x)
  114.    {
  115.       stack_push.push(x);
  116.    }
  117.    
  118.    int size()
  119.    {
  120.       return stack_pop.get_size() + stack_push.get_size();
  121.    }
  122.    
  123.    void dump(std::ostream& out)
  124.    {
  125.       bool started = false;
  126.       for (int i = stack_pop.get_size() - 1; i >= 0; i--)
  127.       {
  128.          if (started) out << ' ';
  129.          started = true;
  130.          out << stack_pop[i];
  131.       }
  132.       for (int i = 0; i < stack_push.get_size(); i++)
  133.       {
  134.          if (started) out << ' ';
  135.          started = true;
  136.          out << stack_push[i];
  137.       }
  138.       out << '\n';
  139.    }
  140. };
  141.  
  142. int hash(string s)
  143. {
  144.    int result = 0;
  145.    for (int i = 0; i < s.length(); i++)
  146.    {
  147.       result = (result * HASH_PRIME + s[i]) % HASH_SIZE;
  148.    }
  149.    return result;
  150. }
  151.  
  152. int main()
  153. {
  154.    ifstream in("quack.in");
  155.    ofstream out("quack.out");
  156.    
  157.    vector< string > source;
  158.    vector< int > jump;
  159.    queue< int > q;
  160.    
  161.    int label[HASH_SIZE];
  162.    for (int i = 0; i < HASH_SIZE; i++)
  163.    {
  164.       label[i] = -1;
  165.    }
  166.    
  167.    int mem[MEM_SIZE];
  168.    for (int i = 0; i < MEM_SIZE; i++)
  169.    {
  170.       mem[i] = 0;
  171.    }
  172.    
  173.    string line;
  174.    while (in >> line)
  175.    {
  176.       source.push(line);
  177.       if (line[0] == ':')
  178.       {
  179.          label[hash(line.substr(1, line.length() - 1))] = source.get_size() - 1;
  180.       }
  181.    }
  182.    
  183.    for (int i = 0; i < source.get_size(); i++)
  184.    {
  185.       switch (source[i][0])
  186.       {
  187.          case 'J':
  188.             jump[i] = label[hash(source[i].substr(1, source[i].length() - 1))];
  189.             break;
  190.          
  191.          case 'Z':
  192.             jump[i] = label[hash(source[i].substr(2, source[i].length() - 2))];
  193.             break;
  194.          
  195.          case 'E':
  196.          case 'G':
  197.             jump[i] = label[hash(source[i].substr(3, source[i].length() - 3))];
  198.             break;
  199.          
  200.          case 'Q':
  201.             jump[i] = -1;
  202.             break;
  203.          
  204.          default:
  205.             if (i == source.get_size() - 1) jump[i] = -1;
  206.             else jump[i] = i + 1;
  207.       }
  208.    }
  209.    
  210.    int comm_ptr = 0, x, y, r;
  211.    while (comm_ptr != -1)
  212.    {
  213.       switch (source[comm_ptr][0])
  214.       {
  215.          case ':':
  216.             break;
  217.          
  218.          case '+':
  219.             q.push((q.pop() + q.pop()) % MODULO);
  220.             break;
  221.          
  222.          case '-':
  223.             q.push((MODULO + q.pop() - q.pop()) % MODULO);
  224.             break;
  225.          
  226.          case '*':
  227.             q.push((q.pop() * q.pop()) % MODULO);
  228.             break;
  229.          
  230.          case '%':
  231.             x = q.pop();
  232.             y = q.pop();
  233.            
  234.             if (y == 0) q.push(0);
  235.             else q.push(x % y);
  236.             break;
  237.          
  238.          case '/':
  239.             x = q.pop();
  240.             y = q.pop();
  241.            
  242.             if (y == 0) q.push(0);
  243.             else q.push(x / y);
  244.             break;
  245.          
  246.          case '>':
  247.             r = q.pop();
  248.             mem[source[comm_ptr][1] - 'a'] = r;
  249.             break;
  250.          
  251.          case '<':
  252.             q.push(mem[source[comm_ptr][1] - 'a']);
  253.             break;
  254.          
  255.          case 'P':
  256.             if (source[comm_ptr].length() > 1)
  257.             {
  258.                out << mem[source[comm_ptr][1] - 'a'] << '\n';
  259.             }
  260.             else
  261.             {
  262.                out << q.pop() << '\n';
  263.             }
  264.             break;
  265.          
  266.          case 'C':
  267.             if (source[comm_ptr].length() > 1)
  268.             {
  269.                out << (char)(mem[source[comm_ptr][1] - 'a'] % 256);
  270.             }
  271.             else
  272.             {
  273.                out << (char)(q.pop() % 256);
  274.             }
  275.             break;
  276.          
  277.          case 'J':
  278.             comm_ptr = jump[comm_ptr];
  279.             continue;
  280.          
  281.          case 'Z':
  282.             if (mem[source[comm_ptr][1] - 'a'] == 0)
  283.             {
  284.                comm_ptr = jump[comm_ptr];
  285.                continue;
  286.             }
  287.             break;
  288.          
  289.          case 'E':
  290.             if (mem[source[comm_ptr][1] - 'a'] == mem[source[comm_ptr][2] - 'a'])
  291.             {
  292.                comm_ptr = jump[comm_ptr];
  293.                continue;
  294.             }
  295.             break;
  296.          
  297.          case 'G':
  298.             if (mem[source[comm_ptr][1] - 'a'] > mem[source[comm_ptr][2] - 'a'])
  299.             {
  300.                comm_ptr = jump[comm_ptr];
  301.                continue;
  302.             }
  303.             break;
  304.          
  305.          case 'Q':
  306.             comm_ptr = -1;
  307.             break;
  308.          
  309.          default:
  310.             sscanf(source[comm_ptr].c_str(), "%d", &x);
  311.             q.push(x % MODULO);
  312.             break;
  313.       }
  314.       if (comm_ptr != source.get_size() - 1 && comm_ptr >= 0) comm_ptr++;
  315.       else comm_ptr = -1;
  316.    }
  317.    
  318.    return 0;
  319. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement