
Кристина
5 год назад
Напишите программу, которая моделирует работу стека целых чисел, управляемого текстовыми командами. В начале работы стек пуст. Затем последовательно выполняются команды, записанные в файле input.txt . Для управления стеком используются две команды:'+<число>' – втолкнуть число на вершину стека'-' – удалить число с вершины стекаТребуется определить состояние стека после окончания выполнения всех команд.Входные данныеВходные строки в файле input.txt содержат команды управления стеком. Последняя строка файла пустая.Выходные данныеПрограмма должна вывести в одной строке через пробел все числа, оказавшиеся в стеке после выполнения всех команд. Слева должно быть дно стека, справа – вершина. Если стек пуст, нужно вывести слово 'EMPTY'. Если во время выполнения команд произошла ошибка, нужно вывести слово 'ERROR'.Примерывходные данные+12+23-+34выходные данные12 34входные данные+12+23--выходные данныеEMPTY
ОТВЕТЫ

Velika
Oct 24, 2020
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186#include<fstream>#include <iostream>#include<vector>#include <stack>#include<string>#include<algorithm>using namespace std;stack<int>st;int error = 0;void operation(string command){if (command == "DROP"){if (st.size() > 0){st.pop(); return;}else{error = 1; return;}}if (command == "SWAP"){if (st.size() >= 2){int a = st.top();st.pop();int b = st.top();st.pop();st.push(a);st.push(b);return;}else{error = 1; return;}}if (command == "DUP"){if (st.size() > 0){int a = st.top();st.push(a);return;}else{error = 1;return;}}if (command == "OVER"){if (st.size() >= 2){int a = st.top();st.pop();int b = st.top();st.pop();st.push(b);st.push(a);st.push(b);return;}else{error = 1; return;}}if (command == "+"){if (st.size() >= 2){int a = st.top();st.pop();int b = st.top();st.pop();st.push(a + b);}else{error = 1;return;}}if (command == "-"){if (st.size() >= 2){int a = st.top();st.pop();int b = st.top();st.pop();st.push(b - a); return;}else{error = 1;return;}}if (command == "*"){if (st.size() >= 2){int a = st.top();st.pop();int b = st.top();st.pop();st.push(b*a); return;}else{error = 1;return;}}if (command == "/"){if (st.size() >= 2){int a = st.top();st.pop();int b = st.top();st.pop();st.push(b/a); return;}else{error = 1;return;}}}bool isnumber(string x){for (int i = 0; i < x.length(); i++){if (x[i] > '9' || x[i] < '0'){return false;}}return true;}int main(){vector<int>v;ifstream in;ofstream out;in.open("input.txt");out.open("output.txt");string command;while (in >> command&&error != 1){if (isnumber(command) == true){st.push(atoi(command.c_str())); continue;}operation(command);}if (error == 1){out << "ERROR"; return 0;}if (st.size() == 0){out << "EMPTY"; return 0;}while (st.size() != 0){v.push_back(st.top());st.pop();}reverse(v.begin(), v.end());for (int i = 0; i < v.size(); i++){out << v[i] << " ";}in.close();out.close();}
876
Смежные вопросы: