forked from haoel/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BasicCalculator.cpp
313 lines (277 loc) · 8.36 KB
/
BasicCalculator.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
// Source : https://leetcode.com/problems/basic-calculator/
// Author : Hao Chen
// Date : 2015-06-12
/**********************************************************************************
*
* Implement a basic calculator to evaluate a simple expression string.
*
* The expression string may contain open ( and closing parentheses ), the plus + or minus sign -,
* non-negative integers and empty spaces .
*
* You may assume that the given expression is always valid.
*
* Some examples:
*
* "1 + 1" = 2
* " 2-1 + 2 " = 3
* "(1+(4+5+2)-3)+(6+8)" = 23
*
* Note: Do not use the eval built-in library function.
*
*
**********************************************************************************/
#include <stdlib.h>
#include <ctype.h>
#include <iostream>
#include <string>
#include <stack>
#include <vector>
using namespace std;
/*
* Sorry, I cannot help using Design Pattern ;-)
* (https://en.wikipedia.org/wiki/Interpreter_pattern)
*
* Two Stack is cheap, design pattern is powerful! ;-)
*
* But WTF, Memory Limit Exceeded!! Shit!!
*/
class Expression
{
public:
virtual int interpret() = 0;
virtual ~Expression() {};
};
class Number: public Expression
{
public:
Number(string num) { this->number = atoi(num.c_str()); }
~Number() { }
int interpret() { return number; }
private:
int number;
};
class Plus : public Expression
{
public:
Plus(Expression* left, Expression* right) :leftOperand(left), rightOperand(right) { }
~Plus() { delete leftOperand; delete rightOperand; }
int interpret() { return leftOperand->interpret() + rightOperand->interpret(); }
private:
Expression* leftOperand;
Expression* rightOperand;
};
class Minus : public Expression
{
public:
Minus(Expression* left, Expression* right) :leftOperand(left), rightOperand(right) { }
~Minus() { delete leftOperand; delete rightOperand; }
int interpret() { return leftOperand->interpret() - rightOperand->interpret(); }
private:
Expression* leftOperand;
Expression* rightOperand;
};
class Multiply : public Expression
{
public:
Multiply(Expression* left, Expression* right) :leftOperand(left), rightOperand(right) { }
~Multiply() { delete leftOperand; delete rightOperand; }
int interpret() { return leftOperand->interpret() * rightOperand->interpret(); }
private:
Expression* leftOperand;
Expression* rightOperand;
};
class Divide : public Expression
{
public:
Divide(Expression* left, Expression* right) :leftOperand(left), rightOperand(right) { }
~Divide() { delete leftOperand; delete rightOperand; }
int interpret() { return leftOperand->interpret() / rightOperand->interpret(); }
private:
Expression* leftOperand;
Expression* rightOperand;
};
bool isOperator(const string &c) {
return (c == "+" || c == "-" || c == "*" || c == "/" );
}
bool isOperator(const char &c) {
return (c == '+' || c == '-' || c == '*' || c == '/');
}
class Evaluator : public Expression
{
private:
Expression* syntaxTree;
public:
Evaluator(vector<string>& s)
{
vector<Expression*> stack;
for (unsigned int i=0; i < s.size(); i++) {
if (isOperator(s[i])) {
Expression* left = stack.back(); stack.pop_back();
Expression* right = stack.back(); stack.pop_back();
switch(s[i][0]) {
case '+' :
stack.push_back(new Plus(right, left)); break;
case '-' :
stack.push_back(new Minus(right, left)); break;
case '*' :
stack.push_back(new Multiply(right, left)); break;
case '/' :
stack.push_back(new Divide(right, left)); break;
}
}else{
stack.push_back(new Number(s[i]));
}
}
syntaxTree = stack.back();
}
~Evaluator() {
delete syntaxTree;
}
int interpret() {
return syntaxTree->interpret();
}
};
vector<string> Parse(string& s){
vector<string> exp;
for(int i=0; i<s.size(); ){
char c = s[i];
string token;
if (c == '+' || c == '-' || c == '*' || c == '/' || c == '(' || c == ')' ) {
exp.push_back(token+c);
i++;
continue;
}
if ( isdigit(c) ) {
while( isdigit(s[i]) ) {
token.push_back(s[i++]);
}
exp.push_back(token);
continue;
}
i++;
}
return exp;
}
int Priority(const string &c) {
if (c == "*" || c == "/") {
return 2;
} else if (c== "+" || c == "-") {
return 1;
} else {
return 0;
}
}
vector<string> Infix2RPN(vector<string>& infix) {
vector<string> rpn;
stack<string> s;
for(int i = 0; i < infix.size(); i++) {
if(isdigit(infix[i][0])) { //number
rpn.push_back(infix[i]);
} else if (infix[i] == "(") {
s.push(infix[i]);
} else if (infix[i] == ")") {
while(!s.empty() && s.top() != "(") {
rpn.push_back(s.top());
s.pop();
}
s.pop();
}else if(isOperator(infix[i]) ){
while(!s.empty() && Priority(s.top()) >= Priority(infix[i])) {
rpn.push_back(s.top());
s.pop();
}
s.push(infix[i]);
}
}
while(!s.empty()) {
rpn.push_back(s.top());
s.pop();
}
return rpn;
}
//Design Pattern for RPN - Memory Limit Exceeded!!
int calculate_RPN_design_pattern(string& s) {
vector<string> exp = Parse(s);
//for (int i=0; i<exp.size(); i++){
// cout << exp[i] << " ";
//}
//cout << endl;
exp = Infix2RPN(exp);
for (int i=0; i<exp.size(); i++){
cout << exp[i] << " ";
}
cout << endl;
Evaluator sentence(exp);
return sentence.interpret();
}
/*
* RPN - Reverse Polish Notation
* see: https://en.wikipedia.org/wiki/Reverse_Polish_notation
*/
//RPN evluation - Memory Limit Exceeded!!
int calculate_RPN_evluation(string& s) {
vector<string> exp = Parse(s);
exp = Infix2RPN(exp);
stack<int> ss;
for(int i=0; i<exp.size(); i++) {
if (isdigit(exp[i][0])) {
ss.push(atoi(exp[i].c_str()));
}
if (exp[i]=="+" || exp[i]=="-") {
int rhs = ss.top(); ss.pop();
int lhs = ss.top(); ss.pop();
if (exp[i]=="-") rhs = -rhs;
ss.push(lhs + rhs);
}
}
return ss.top();
}
//Two stack solution - quick & dirty solution
inline void calculate_two_stacks(stack<int>& num_stack, stack<char>& op_stack) {
int lhs = num_stack.top(); num_stack.pop();
int rhs = num_stack.top(); num_stack.pop();
char op = op_stack.top(); op_stack.pop();
if (op=='-') rhs = -rhs;
num_stack.push(lhs + rhs);
}
int calculate_two_stacks(string& s) {
stack<int> num_stack; //put the number
stack<char> op_stack; //put the operations
for(int i = s.size() - 1; i >= 0; i--){
if(s[i] == ')' || s[i] == '+' || s[i] == '-') {
op_stack.push(s[i]);
} else if(isdigit(s[i])){
string num;
num += s[i];
while(isdigit(s[i-1])){
num.insert(num.begin(), s[i-1]);
i--;
}
num_stack.push(atoi(num.c_str()));
} else if(s[i] == '('){
while(op_stack.top() != ')') {
calculate_two_stacks(num_stack, op_stack);
}
op_stack.pop();
}
}
while(!op_stack.empty()){
calculate_two_stacks(num_stack, op_stack);
}
return num_stack.top();
}
int calculate(string s) {
return calculate_two_stacks(s);
return calculate_RPN_evluation(s);
return calculate_RPN_design_pattern(s);
}
int main(int argc, char** argv) {
string s = " 15-(1+3)+(2+1) ";
if (argc >1){
s = argv[1];
}
cout << s << " = " << calculate(s) << endl;
cout << "---------------" << endl;
s = "(2+4)-(6+(1+5))";
cout << s << " = " << calculate(s) << endl;
}