Advertisement
Guest User

Untitled

a guest
Dec 5th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.34 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <queue>
  5. #include <map>
  6. using namespace std;
  7.  
  8. typedef unsigned short int uint16;
  9.  
  10. struct Queue
  11. {
  12. queue<uint16> q;
  13. uint16 get()
  14. {
  15. uint16 result = q.front();
  16. q.pop();
  17. return result;
  18. }
  19.  
  20. void put(uint16 value)
  21. {
  22. q.push((uint16) (value % 65536));
  23. }
  24. };
  25.  
  26.  
  27. int main()
  28. {
  29.  
  30.  
  31. vector<string> source;
  32.  
  33. string line;
  34. while (getline(cin, line))
  35. {
  36. source.push_back(line);
  37. }
  38.  
  39.  
  40. Queue queue;
  41. map<string, int> labels;
  42.  
  43.  
  44. vector<uint16> registers(26);
  45.  
  46. for (int i = 0; i < source.size(); i++)
  47. {
  48. if (source[i][0] == ':')
  49. {
  50. labels[source[i].substr(1)] = i;
  51. }
  52. }
  53.  
  54. uint16 x, y;
  55. for (int i = 0; i < source.size(); i++)
  56. {
  57. string command = source[i];
  58. switch (command[0])
  59. {
  60. case '+':
  61. x = queue.get();
  62. y = queue.get();
  63. queue.put(x + y);
  64. break;
  65. case '-':
  66. x = queue.get();
  67. y = queue.get();
  68. queue.put(x - y);
  69. break;
  70. case '*':
  71. x = queue.get();
  72. y = queue.get();
  73. queue.put(x * y);
  74. break;
  75. case '/':
  76. x = queue.get();
  77. y = queue.get();
  78. queue.put(y != 0 ? x / y : 0);
  79. break;
  80. case '%':
  81. x = queue.get();
  82. y = queue.get();
  83. queue.put(y != 0 ? x % y : 0);
  84. break;
  85. case '>':
  86. registers[command[1] - 'a'] = queue.get();
  87. break;
  88. case '<':
  89. queue.put(registers[command[1] - 'a']);
  90. break;
  91. case 'P':
  92. if (command.length() == 1)
  93. cout << queue.get() << endl;
  94. else
  95. cout << registers[command[1] - 'a'] << endl;
  96. break;
  97. case 'C':
  98. if (command.length() == 1)
  99. cout << (unsigned char) queue.get();
  100. else
  101. cout << (unsigned char) registers[command[1] - 'a'];
  102. break;
  103. case ':':
  104. break;
  105. case 'J':
  106. i = labels[command.substr(1)];
  107. break;
  108. case 'Z':
  109. if (registers[command[1] - 'a'] == 0)
  110. {
  111. i = labels[command.substr(2)];
  112. }
  113. break;
  114. case 'E':
  115. if (registers[command[1] - 'a'] == registers[command[2] - 'a'])
  116. {
  117. i = labels[command.substr(3)];
  118. }
  119. break;
  120. case 'G':
  121. if (registers[command[1] - 'a'] > registers[command[2] - 'a'])
  122. {
  123. i = labels[command.substr(3)];
  124. }
  125. break;
  126. case 'Q':
  127. i = source.size();
  128. break;
  129. default:
  130. queue.put(atoi(command.c_str()));
  131. break;
  132. }
  133. }
  134.  
  135. return 0;
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement