summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew <saintruler@gmail.com>2021-02-10 23:56:03 +0400
committerAndrew <saintruler@gmail.com>2021-02-10 23:56:03 +0400
commite0857d37806f8da62d92f3462da14d6bd8aa8479 (patch)
treec2ab59f6d361855f0c13bd8227ad261f4de9cb37
parenta7a0646a34a1430568ec5cb22534f24c886d127b (diff)
Добавил решение 25 задачи
-rw-r--r--structures/Makefile6
-rw-r--r--structures/task25.cpp51
2 files changed, 57 insertions, 0 deletions
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 <iostream>
+#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;
+}