Advertisement
Guest User

t

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