From e0857d37806f8da62d92f3462da14d6bd8aa8479 Mon Sep 17 00:00:00 2001 From: Andrew Date: Wed, 10 Feb 2021 23:56:03 +0400 Subject: =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20=D1=80?= =?UTF-8?q?=D0=B5=D1=88=D0=B5=D0=BD=D0=B8=D0=B5=2025=20=D0=B7=D0=B0=D0=B4?= =?UTF-8?q?=D0=B0=D1=87=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- structures/Makefile | 6 ++++++ structures/task25.cpp | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 structures/task25.cpp (limited to 'structures') diff --git a/structures/Makefile b/structures/Makefile index 7dcb9d1..ae8cca3 100644 --- a/structures/Makefile +++ b/structures/Makefile @@ -21,5 +21,11 @@ test18: task18 printf "8\n1 1 2 2 3 4 5 5" | ./task.out printf "11\n1 1 1 2 2 1 1 3 4 5 5" | ./task.out +task25: + $(COMPILE) -o task.out task25.cpp +test25: task25 + printf "5\n123 45 1 + *" | ./task.out + printf "13\n4 5 + 8 * 4 2 - * 2 + 5 +" | ./task.out + clean: rm task.out diff --git a/structures/task25.cpp b/structures/task25.cpp new file mode 100644 index 0000000..fde664c --- /dev/null +++ b/structures/task25.cpp @@ -0,0 +1,51 @@ +#include +#include "stack.h" + +using namespace std; + +int main() +{ + int n; + cout << "n = "; + cin >> n; + + stack *s = NULL; + char* buf = new char[32]; + for (int i = 0; i < n; i++) + { + cin >> buf; + if ('0' <= buf[0] && buf[0] <= '9') + { + int i = atoi(buf); + push(s, i); + } + else + { + switch (buf[0]) + { + case '+': { + int o1 = pop(s); + int o2 = pop(s); + push(s, o2 + o1); + } break; + + case '-': { + int o1 = pop(s); + int o2 = pop(s); + push(s, o2 - o1); + } break; + + case '*': { + int o1 = pop(s); + int o2 = pop(s); + push(s, o2 * o1); + } break; + } + } + } + + int result = pop(s); + cout << "Result = " << result << endl; + + return 0; +} -- cgit v1.2.3