summaryrefslogtreecommitdiff
path: root/structures/task25.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'structures/task25.cpp')
-rw-r--r--structures/task25.cpp51
1 files changed, 51 insertions, 0 deletions
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;
+}