-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTask9.cpp
204 lines (191 loc) · 4.38 KB
/
Task9.cpp
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#include <string>
#include<sstream>
#include<exception>
#include <iostream>
using namespace std;
class Polish_Converter {
private:
enum Op_sym {
PLUS = '+',
MINUS = '-',
MULTIP = '*',
DIVID = '/',
POW = '^',
L_BRACE = '(',
R_BRACE = ')',
SPACE_SYM = ' '
};
string input_string;
string output_string;
string op_stack;
protected:
char last_sym;
public:
Polish_Converter() = default;
Polish_Converter(string in) : input_string(in){}
Polish_Converter(const Polish_Converter&) = default;
~Polish_Converter() { input_string.clear(); output_string.clear(); op_stack.clear(); }
string Get_Input_String()const;
string Get_Output_String()const;
void Set_Input_String(string);
void Convert_To_Polish();
friend ostream& operator<<(ostream&, const Polish_Converter&);
friend istream& operator>>(istream&, Polish_Converter&);
};
/*
*@[brief]: friend overloaded output operator;
*@[in]: ostream object for output and const reference to class object
*@[out]: ostream reference for chain output
*/
ostream& operator<<(ostream& out, const Polish_Converter& obj) {
out << obj.output_string;
return out;
}
/*
*@[brief]: friend overloaded input operator;
*@[in]: istream object for input and reference to class object
*@[out]: istream reference for chain input
*/
istream & operator>>(istream & in, Polish_Converter & obj)
{
try {
getline(in, obj.input_string);
return in;
}
catch (exception& ex) {
cout << "Whoaa we got exception here, ty another input but restart program first\n Error: " << ex.what();
exit(true);
}
}
/*
*@[brief]: Function convert string expr to RPN form
*/
void Polish_Converter::Convert_To_Polish()
{
op_stack.clear();
output_string.clear();
last_sym = 0;
for (auto x : input_string) {
if (isspace(x)) {
continue;
}
if (isalpha(x)||isdigit(x)) {
output_string.push_back(x);
}
switch (x) {
case PLUS:
case MINUS:
output_string.push_back(SPACE_SYM);
if (op_stack.empty()) {
op_stack.push_back(x);
}
else {
last_sym = op_stack.back();
switch (last_sym) {
case PLUS:
case MINUS:
case MULTIP:
case DIVID:
case POW:
output_string.push_back(op_stack.back());
op_stack.pop_back();
op_stack.push_back(x);
output_string.push_back(SPACE_SYM);
break;
case L_BRACE:
op_stack.push_back(x);
break;
}
}
break;
case MULTIP:
case DIVID:
output_string.push_back(SPACE_SYM);
if (op_stack.empty()) {
op_stack.push_back(x);
}
else {
last_sym = op_stack.back();
switch (last_sym) {
case MULTIP:
case DIVID:
case POW:
output_string.push_back(op_stack.back());
op_stack.pop_back();
op_stack.push_back(x);
output_string.push_back(SPACE_SYM);
break;
case L_BRACE:
op_stack.push_back(x);
break;
}
}
break;
case POW:
output_string.push_back(SPACE_SYM);
case L_BRACE:
op_stack.push_back(x);
break;
case R_BRACE:
int tmp_count = 0;
output_string.push_back(SPACE_SYM);
string::iterator b = op_stack.end()-1;
for (; *b != L_BRACE; --b) {
output_string.push_back(*b);
output_string.push_back(SPACE_SYM);
tmp_count++;
}
tmp_count++;
op_stack.erase(op_stack.end() - tmp_count, op_stack.end());
break;
}
}
if (!op_stack.empty()) {
string::iterator b = op_stack.end() - 1;
for (; b != op_stack.begin(); --b) {
output_string.push_back(*b);
}
output_string.push_back(*b);
output_string.push_back(SPACE_SYM);
op_stack.clear();
}
}
/*
*@[brief]: Helper function that returns input string
*@[out]: copy of private member that holds user expr
*/
string Polish_Converter::Get_Input_String() const
{
return input_string;
}
/*
*@[brief]: Helper function that returns converted expr
*@[out]: copy of private member that holds RPN expr
*/
string Polish_Converter::Get_Output_String() const
{
return output_string;
}
/*
*@[brief]: Helper function that sets input string;
*@[in]: user input string;
*/
void Polish_Converter::Set_Input_String(string in)
{
input_string = in;
}
void main() {
Polish_Converter obj;
obj.Set_Input_String("((a + b)/(3*2/4^3))");
obj.Convert_To_Polish();
cout << obj << endl;
Polish_Converter * p_obj = new Polish_Converter("1 2 + 2");
p_obj->Convert_To_Polish();
cout << *p_obj;
delete p_obj;
cout << "\n\n\nEnter Expression: ";
cin >> obj;
//obj.Set_Input_String("((a + b) / (3 * 2 / 4 ^ 3))");
obj.Convert_To_Polish();
cout << "\n\n" << obj << "\n";
}