-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSource.cpp
More file actions
180 lines (169 loc) · 3.64 KB
/
Copy pathSource.cpp
File metadata and controls
180 lines (169 loc) · 3.64 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include <iostream>
#include <string>
struct node
{
std::string value;
node* next{};
};
bool is_empty(node* top)
{
return top == nullptr;
}
node* make_stack(std::string b)
{
node* current{new node};
current->value = std::move(b);
current->next = nullptr;
return current;
}
node* add_element_top(node* top, std::string num)
{
node* current{new node};
current->value = std::move(num);
current->next = top;
return current;
}
node* del_top(node* front)
{
node* temp{front->next};
delete front;
return (temp);
}
void del_all(node* top)
{
node* temp{top};
node* current;
while (temp->next != nullptr)
{
temp = temp->next;
if (temp->next == nullptr)
{
current = temp;
delete current;
return;
}
current = temp->next;
temp->next = current->next;
delete current;
}
}
void output_list(node* top)
{
for (node* current{top}; current != nullptr; current = current->next)
{
std::cout << current->value << " ";
}
std::cout << std::endl;
}
int getPriority(char c) {
if (c == '-' || c == '+') return 1;
if (c == '*' || c == '/') return 2;
return 0;
}
std::string t_postfix(std::string str, struct node* Stack) {
std::string x;
std::string out_str;
while (str.length() > 0) {
x = "";
if (str[0] == '(') {
Stack=add_element_top(Stack, "(");
str.erase(0, 1);
}
else if (str[0] == ')') {
while (Stack->value != "(") {
out_str+=Stack->value + " ";
Stack=del_top(Stack);
}
Stack=del_top(Stack);
str.erase(0, 1);
}
else if (!isdigit(str[0]) && str[0] != ',') {
x = str[0];
while (Stack!=nullptr && getPriority(Stack->value[0]) >= getPriority(x[0])) {
out_str+=Stack->value + " ";
Stack=del_top(Stack);
}
Stack=add_element_top(Stack, x);
}
else {
for (char& c : str)
{
if (!isdigit(c) && c != ',') break;
x = x + c;
}
out_str+=x + " ";
}
str.erase(0, x.length());
}
while (Stack!=nullptr) {
out_str+=Stack->value + " ";
Stack=del_top(Stack);
}
return out_str;
}
node* result (node* top, std::string out_str)
{
for (int i = 0; i < out_str.length(); i++)
{
if (out_str[i] == '*' || out_str[i] == '/' || out_str[i] == '+' || out_str[i] == '-')
{
double a{atof(top->value.c_str())};
top = del_top(top);
double b{atof(top->value.c_str())};
top = del_top(top);
switch (out_str[i])
{
case '+': top = add_element_top(top, std::to_string(b + a));
break;
case '-': top = add_element_top(top, std::to_string(b - a));
break;
case '*': top = add_element_top(top, std::to_string(b * a));
break;
case '/': top = add_element_top(top, std::to_string(b / a));
break;
default: break;
}
}
else if (out_str[i] != ' ')
{
std::string s;
int a{i};
while (a < out_str.length() && out_str[a] != ' ')
{
s += out_str[a];
a++;
}
if (is_empty(top))
{
top = make_stack(s);
}
else
{
top = add_element_top(top, s);
}
for (int j = i; j < a; j++)
{
out_str[j] = ' ';
}
}
}
return top;
}
int main()
{
std::string in_str;
node* top = nullptr;
std::cout << "Input ";
std::cin >> in_str;
if (in_str[0] == '-')
{
in_str = '0' + in_str;
}
for (int i = 1; i < in_str.length(); ++i)
if (in_str[i - 1] == '(' && (in_str[i] == '-' || in_str[i] == '+'))
in_str.insert(i, "0");
std::string out_str = t_postfix(in_str, top);
std::cout << std::endl << out_str;
top=result(top,out_str);
std::cout << std::endl <<top->value;
}