Advertisement
Guest User

main.cpp

a guest
Dec 7th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.77 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <Windows.h>
  4.  
  5. using namespace std;
  6.  
  7. short x = 1, y = 1;
  8. short x_max = 10, x_min = 1, y_max = 10, y_min = 1;
  9. string code;
  10.  
  11. void read_code()
  12. {
  13. string new_task;
  14. int line_index = 1;
  15. cout << "Введите код программы:\n";
  16. do
  17. {
  18. cout << line_index << " ";
  19. getline(cin, new_task);
  20. if (new_task == "ВЛЕВО") code += 'l';
  21. else if (new_task == "ВПРАВО") code += 'r';
  22. else if (new_task == "ВВЕРХ") code += 'u';
  23. else if (new_task == "ВНИЗ") code += 'd';
  24. else if (new_task.length() == 11 && new_task.substr(0, 10) == "ПОВТОРИТЬ " && new_task[10] >= '1' && new_task[10] <= '9') code += new_task[10];
  25. else if (new_task == "КЦ") code += 'e';
  26. else if (new_task == "");
  27. else
  28. {
  29. cout << "Повторите ввод\n";
  30. line_index--;
  31. }
  32.  
  33. line_index++;
  34. } while (new_task != "");
  35. }
  36. bool simple_move(int i)
  37. {
  38. if (code[i] == 'u')
  39. {
  40. y++;
  41. if (y > y_max)
  42. {
  43. cout << "Команда " << i + 1 << ": Y>" << y_max << '\n';
  44. return true;
  45. }
  46. else return false;
  47. }
  48. else if (code[i] == 'd')
  49. {
  50. y--;
  51. if (y < y_min)
  52. {
  53. cout << "Команда " << i + 1 << ": Y<" << y_min << '\n';
  54. return true;
  55. }
  56. else return false;
  57. }
  58. else if (code[i] == 'r')
  59. {
  60. x++;
  61. if (x > x_max)
  62. {
  63. cout << "Команда " << i + 1 << ": X>" << x_max << '\n';
  64. return true;
  65. }
  66. else return false;
  67. }
  68. else if (code[i] == 'l')
  69. {
  70. x--;
  71. if (x < x_min)
  72. {
  73. cout << "Команда " << i + 1 << ": Y<" << x_min << '\n';
  74. return true;
  75. }
  76. else return false;
  77. }
  78. }
  79. bool cycle_move(int &i)
  80. {
  81. if (code[i] == 'e')
  82. {
  83. cout << "Команда " << i + 1 << ": Встречено закрытие неоткрытого цикла.\n";
  84. return true;
  85. }
  86. else
  87. {
  88. short n = code[i] - '0';
  89. int position = i + 1;
  90. bool is_out = false;
  91.  
  92. do
  93. {
  94. n--;
  95. i = position;
  96. while (code[i] != 'e' && i < code.length() && !is_out)
  97. {
  98. if (code[i] == 'u' || code[i] == 'd' || code[i] == 'l' || code[i] == 'r') is_out = simple_move(i);
  99. else is_out = cycle_move(i);
  100. i++;
  101. }
  102. if (i == code.length())
  103. {
  104. cout << "Команда " << i + 1 << ": Цикл не был закрыт.\n";
  105. return true;
  106. }
  107. } while (n > 0 && !is_out);
  108. }
  109. }
  110. void run_code()
  111. {
  112. bool is_out = false;
  113. for (int i = 0; i < code.length() && !is_out; i++)
  114. {
  115. if (code[i] == 'u' || code[i] == 'd' || code[i] == 'l' || code[i] == 'r') is_out = simple_move(i);
  116. else is_out = cycle_move(i);
  117. }
  118. if(!is_out) cout << "Координаты исполнителя: (" << x << ',' << y << ").\n";
  119. }
  120.  
  121. void main()
  122. {
  123. SetConsoleCP(1251);
  124. SetConsoleOutputCP(1251);
  125.  
  126. read_code();
  127. run_code();
  128.  
  129. system("pause");
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement