Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- #include <string>
- const int I_SIZE = 1024;
- const int M_SIZE = 4;
- const int HASH_SIZE = 1 << 15;
- const int HASH_PRIME = 257;
- const int MODULO = 65536;
- const int MEM_SIZE = 26;
- using namespace std;
- template< class T > class vector
- {
- int size;
- int alloc_size;
- T* data;
- public:
- int get_size()
- {
- return size;
- }
- T& operator[](int n)
- {
- return data[n];
- }
- vector()
- {
- data = new T[I_SIZE];
- alloc_size = I_SIZE;
- size = 0;
- }
- T peek()
- {
- return data[size - 1];
- }
- T pull()
- {
- int result = data[--size];
- return result;
- }
- void pop()
- {
- size--;
- if (alloc_size > M_SIZE && size <= alloc_size / 4)
- {
- shrink();
- }
- }
- void push(T a)
- {
- if (size >= alloc_size) grow();
- data[size++] = a;
- }
- bool is_empty()
- {
- return (size == 0);
- }
- ~vector()
- {
- delete[] data;
- }
- private:
- void grow()
- {
- T* new_data = new T[alloc_size * 2];
- for (int i = 0; i < size; i++)
- {
- new_data[i] = data[i];
- }
- delete[] data;
- data = new_data;
- alloc_size *= 2;
- }
- void shrink()
- {
- T* new_data = new T[alloc_size / 2];
- for (int i = 0; i < size; i++)
- {
- new_data[i] = data[i];
- }
- delete[] data;
- data = new_data;
- alloc_size /= 2;
- }
- };
- template< class T > class queue
- {
- vector< T > stack_push, stack_pop;
- public:
- T pop()
- {
- if (stack_pop.is_empty()) while (!stack_push.is_empty())
- {
- stack_pop.push(stack_push.pull());
- }
- return stack_pop.pull();
- }
- void push(T x)
- {
- stack_push.push(x);
- }
- int size()
- {
- return stack_pop.get_size() + stack_push.get_size();
- }
- void dump(std::ostream& out)
- {
- bool started = false;
- for (int i = stack_pop.get_size() - 1; i >= 0; i--)
- {
- if (started) out << ' ';
- started = true;
- out << stack_pop[i];
- }
- for (int i = 0; i < stack_push.get_size(); i++)
- {
- if (started) out << ' ';
- started = true;
- out << stack_push[i];
- }
- out << '\n';
- }
- };
- int hash(string s)
- {
- int result = 0;
- for (int i = 0; i < s.length(); i++)
- {
- result = (result * HASH_PRIME + s[i]) % HASH_SIZE;
- }
- return result;
- }
- int main()
- {
- ifstream in("quack.in");
- ofstream out("quack.out");
- vector< string > source;
- vector< int > jump;
- queue< int > q;
- int label[HASH_SIZE];
- for (int i = 0; i < HASH_SIZE; i++)
- {
- label[i] = -1;
- }
- int mem[MEM_SIZE];
- for (int i = 0; i < MEM_SIZE; i++)
- {
- mem[i] = 0;
- }
- string line;
- while (in >> line)
- {
- source.push(line);
- if (line[0] == ':')
- {
- label[hash(line.substr(1, line.length() - 1))] = source.get_size() - 1;
- }
- }
- for (int i = 0; i < source.get_size(); i++)
- {
- switch (source[i][0])
- {
- case 'J':
- jump[i] = label[hash(source[i].substr(1, source[i].length() - 1))];
- break;
- case 'Z':
- jump[i] = label[hash(source[i].substr(2, source[i].length() - 2))];
- break;
- case 'E':
- case 'G':
- jump[i] = label[hash(source[i].substr(3, source[i].length() - 3))];
- break;
- case 'Q':
- jump[i] = -1;
- break;
- default:
- if (i == source.get_size() - 1) jump[i] = -1;
- else jump[i] = i + 1;
- }
- }
- int comm_ptr = 0, x, y, r;
- while (comm_ptr != -1)
- {
- switch (source[comm_ptr][0])
- {
- case ':':
- break;
- case '+':
- q.push((q.pop() + q.pop()) % MODULO);
- break;
- case '-':
- q.push((MODULO + q.pop() - q.pop()) % MODULO);
- break;
- case '*':
- q.push((q.pop() * q.pop()) % MODULO);
- break;
- case '%':
- x = q.pop();
- y = q.pop();
- if (y == 0) q.push(0);
- else q.push(x % y);
- break;
- case '/':
- x = q.pop();
- y = q.pop();
- if (y == 0) q.push(0);
- else q.push(x / y);
- break;
- case '>':
- r = q.pop();
- mem[source[comm_ptr][1] - 'a'] = r;
- break;
- case '<':
- q.push(mem[source[comm_ptr][1] - 'a']);
- break;
- case 'P':
- if (source[comm_ptr].length() > 1)
- {
- out << mem[source[comm_ptr][1] - 'a'] << '\n';
- }
- else
- {
- out << q.pop() << '\n';
- }
- break;
- case 'C':
- if (source[comm_ptr].length() > 1)
- {
- out << (char)(mem[source[comm_ptr][1] - 'a'] % 256);
- }
- else
- {
- out << (char)(q.pop() % 256);
- }
- break;
- case 'J':
- comm_ptr = jump[comm_ptr];
- continue;
- case 'Z':
- if (mem[source[comm_ptr][1] - 'a'] == 0)
- {
- comm_ptr = jump[comm_ptr];
- continue;
- }
- break;
- case 'E':
- if (mem[source[comm_ptr][1] - 'a'] == mem[source[comm_ptr][2] - 'a'])
- {
- comm_ptr = jump[comm_ptr];
- continue;
- }
- break;
- case 'G':
- if (mem[source[comm_ptr][1] - 'a'] > mem[source[comm_ptr][2] - 'a'])
- {
- comm_ptr = jump[comm_ptr];
- continue;
- }
- break;
- case 'Q':
- comm_ptr = -1;
- break;
- default:
- sscanf(source[comm_ptr].c_str(), "%d", &x);
- q.push(x % MODULO);
- break;
- }
- if (comm_ptr != source.get_size() - 1 && comm_ptr >= 0) comm_ptr++;
- else comm_ptr = -1;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement