Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <Windows.h>
- using namespace std;
- short x = 1, y = 1;
- short x_max = 10, x_min = 1, y_max = 10, y_min = 1;
- string code;
- void read_code()
- {
- string new_task;
- int line_index = 1;
- cout << "Введите код программы:\n";
- do
- {
- cout << line_index << " ";
- getline(cin, new_task);
- if (new_task == "ВЛЕВО") code += 'l';
- else if (new_task == "ВПРАВО") code += 'r';
- else if (new_task == "ВВЕРХ") code += 'u';
- else if (new_task == "ВНИЗ") code += 'd';
- else if (new_task.length() == 11 && new_task.substr(0, 10) == "ПОВТОРИТЬ " && new_task[10] >= '1' && new_task[10] <= '9') code += new_task[10];
- else if (new_task == "КЦ") code += 'e';
- else if (new_task == "");
- else
- {
- cout << "Повторите ввод\n";
- line_index--;
- }
- line_index++;
- } while (new_task != "");
- }
- bool simple_move(int i)
- {
- if (code[i] == 'u')
- {
- y++;
- if (y > y_max)
- {
- cout << "Команда " << i + 1 << ": Y>" << y_max << '\n';
- return true;
- }
- else return false;
- }
- else if (code[i] == 'd')
- {
- y--;
- if (y < y_min)
- {
- cout << "Команда " << i + 1 << ": Y<" << y_min << '\n';
- return true;
- }
- else return false;
- }
- else if (code[i] == 'r')
- {
- x++;
- if (x > x_max)
- {
- cout << "Команда " << i + 1 << ": X>" << x_max << '\n';
- return true;
- }
- else return false;
- }
- else if (code[i] == 'l')
- {
- x--;
- if (x < x_min)
- {
- cout << "Команда " << i + 1 << ": Y<" << x_min << '\n';
- return true;
- }
- else return false;
- }
- }
- bool cycle_move(int &i)
- {
- if (code[i] == 'e')
- {
- cout << "Команда " << i + 1 << ": Встречено закрытие неоткрытого цикла.\n";
- return true;
- }
- else
- {
- short n = code[i] - '0';
- int position = i + 1;
- bool is_out = false;
- do
- {
- n--;
- i = position;
- while (code[i] != 'e' && i < code.length() && !is_out)
- {
- if (code[i] == 'u' || code[i] == 'd' || code[i] == 'l' || code[i] == 'r') is_out = simple_move(i);
- else is_out = cycle_move(i);
- i++;
- }
- if (i == code.length())
- {
- cout << "Команда " << i + 1 << ": Цикл не был закрыт.\n";
- return true;
- }
- } while (n > 0 && !is_out);
- }
- }
- void run_code()
- {
- bool is_out = false;
- for (int i = 0; i < code.length() && !is_out; i++)
- {
- if (code[i] == 'u' || code[i] == 'd' || code[i] == 'l' || code[i] == 'r') is_out = simple_move(i);
- else is_out = cycle_move(i);
- }
- if(!is_out) cout << "Координаты исполнителя: (" << x << ',' << y << ").\n";
- }
- void main()
- {
- SetConsoleCP(1251);
- SetConsoleOutputCP(1251);
- read_code();
- run_code();
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement