-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path2_stack_poland.cpp
More file actions
113 lines (103 loc) · 2.2 KB
/
2_stack_poland.cpp
File metadata and controls
113 lines (103 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <iostream>
#include <cstdlib>
#include <string.h>
using namespace std;
struct list
{
double data;
list *next;
};
list *getmem(int data)
{
list *newmem = new list;
newmem->data = data;
newmem->next = NULL;
return (newmem);
}
list *push_one(list *stack, double number)
{
list *newmem = getmem(number);
newmem->next = stack;
return (newmem);
}
void operation_plus(list *stack)
{
cout << "operation plus" << endl;
stack->data = stack->data + stack->next->data;
list *temp = stack->next;
stack->next = stack->next->next;
delete temp;
}
void operation_minus(list *stack)
{
cout << "operation minus" << endl;
stack->data = stack->data - stack->next->data;
list *temp = stack->next;
stack->next = stack->next->next;
delete temp;
}
void operation_pow(list *stack)
{
cout << "operation pow" << endl;
stack->data = stack->data * stack->next->data;
list *temp = stack->next;
stack->next = stack->next->next;
delete temp;
}
void operation_division(list *stack)
{
cout << "operation div" << endl;
stack->data = stack->data / stack->next->data;
list *temp = stack->next;
stack->next = stack->next->next;
delete temp;
}
void show_stack(list *stack)
{
list *cur = stack;
int i = 0;
cout << "your stack: \n";
while (cur != NULL)
{
cout << i++ << " " << cur->data << endl;
cur = cur->next;
}
}
int main()
{
char buffer[100];
char cur = 'A';
list *stack = NULL;
cout << "enter expression" << endl;
cin.getline(buffer, 100);
for (int i = 0; i < strlen(buffer); i++)
if (buffer[i] == cur + 1)
cur++;
double *mas = new double[(int)(cur - 'A' + 1)];
char c = 'A';
while (c <= cur)
{
cout << "enter number for " << c << endl;
char buffer[20];
cin.getline(buffer, 20);
mas[(int)(c - 'A')] = atof(buffer);
c = c + 1;
}
for (int i = 0; i < strlen(buffer); i++)
{
if (buffer[i] >= 'A' && buffer[i] <= 'Z')
stack = push_one(stack, mas[(int)(buffer[i] - 'A')]);
else if (buffer[i] == '+')
operation_plus(stack);
else if (buffer[i] == '-')
operation_minus(stack);
else if (buffer[i] == '*')
operation_pow(stack);
else if (buffer[i] == '/')
operation_division(stack);
show_stack(stack);
}
cout << "rezult is " << stack->data << endl;
delete stack;
delete[] mas;
}