-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculator.cpp
201 lines (192 loc) · 4.74 KB
/
calculator.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
#include "calculator.h"
#include "util.h"
#include "exception.h"
#include <string>
#include <vector>
#include <regex>
using namespace std;
Calculator::Calculator()
{
level_map["~"] = 5;
level_map["*"] = level_map["/"] = 4;
level_map["+"] = level_map["-"] = 3;
level_map["<<"] = level_map[">>"] = level_map[">>>"] = 2;
level_map["&"] = level_map["|"] = level_map["^"] = 1;
level_map["#"] = 0;
}
Calculator::Calculator(string str)
{
level_map["~"] = 5;
level_map["*"] = level_map["/"] = 4;
level_map["+"] = level_map["-"] = 3;
level_map["<<"] = level_map[">>"] = level_map[">>>"] = 2;
level_map["&"] = level_map["|"] = level_map["^"] = 1;
level_map["#"] = 0;
decode(str);
}
void Calculator::decode(string str)
{
equation = str;
vector<string> opstack;
opstack.push_back("#");
string num;
string op;
str = trim(str);
str = replace (str, "\t", "");
str = replace (str, " ", "");
str = replace (str, "<<", "z");
str = replace (str, ">>>", "s");
str = replace (str, ">>", "y");
str += "#";
string otmp;
mix mtmp;
int i = 0;
while (true)
{
if (str[i] <= '9' && str[i] >= '0' || str[i] <= 'F' && str[i] >= 'A' ||
str[i] <= 'f' && str[i] >= 'a' || str[i] == 'x' || str[i] == 'X' ||
((str[i] == '-' || str[i] == '+') && (i == 0 || str[i - 1] == '(')))
{
while (str[i] <= '9' && str[i] >= '0' || str[i] <= 'F' && str[i] >= 'A' ||
str[i] <= 'f' && str[i] >= 'a' || str[i] == 'x' || str[i] == 'X' ||
((str[i] == '-' || str[i] == '+') && (i == 0 || str[i - 1] == '(')))
num += str[i++];
mix mtmp(toInt(num));
mixqueue.push_back(mtmp);
num = "";
continue;
}
op = str[i++];
if (op == "")
throw Exception("Invalid equation", equation);
regex re("[+\\-\\*/%~^&\\|\\(\\)zys#]");
if (!regex_match(op, re))
throw Exception("Invalid operation in the equation:" + op, equation);
if (op == ")")
{
while (opstack.back() != "(")
{
if (opstack.back() == "#")
throw Exception("Unmatched parentheses in equation", str);
mix mtmp(opstack.back());
mixqueue.push_back(mtmp);
opstack.pop_back();
}
opstack.pop_back();
}
else if (opstack.back() == "(" || op == "(" )
opstack.push_back(op);
else if(level_map[op] <= level_map[opstack.back()] && opstack.back() != "#")
{
do
{
mix mtmp(opstack.back());
mixqueue.push_back(mtmp);
opstack.pop_back();
}while (level_map[op] <= level_map[opstack.back()] &&
opstack.back() != "#" && opstack.back() != "(");
if (op == "#" && opstack.back() == "#")
break;
opstack.push_back(op);
}
else if (op == "#" && opstack.back() == "#")
break;
else
opstack.push_back(op);
}
}
int Calculator::exec()
{
vector<int> stack;
while (!mixqueue.empty())
{
if (mixqueue.front().isnumber)
{
stack.push_back(mixqueue.front().number);
mixqueue.pop_front();
}
else
{
char op = mixqueue.front().operation[0];
mixqueue.pop_front();
int op1,op2;
if (stack.empty())
throw Exception("Invalid Equation", equation);
op2 = stack.back();
stack.pop_back();
if (stack.empty())
throw Exception("Invalid Equation", equation);
op1 = stack.back();
stack.pop_back();
switch(op)
{
case '+':stack.push_back(op1 + op2);break;
case '-':stack.push_back(op1 - op2);break;
case '*':stack.push_back(op1 * op2);break;
case '/':stack.push_back(op1 / op2);break;
case '%':stack.push_back(op1 % op2);break;
case '~':stack.push_back(op1);stack.push_back(~op2);break;
case 'z':stack.push_back(op1 << op2);break;
case 'y':stack.push_back((unsigned)op1 >> op2);break;
case 's':stack.push_back(op1 >> op2);break;
case '&':stack.push_back(op1 & op2);break;
case '|':stack.push_back(op1 | op2);break;
case '^':stack.push_back(op1 ^ op2);break;
default:break;
}
/*
cout << op << ":";
for (int i = 0; i != stack.size(); ++i)
cout << stack[i] << " ";
cout << endl;
*/
}
}
return stack[0];
}
int Calculator::toInt(string str)
{
string org_str = str;
int scale = 10;
int sign = 1;
str = trim(str);
if (!isnumber(str))
throw Exception("Invalid number format:" + org_str, "");
if (str[0] == '-')
{
sign = -1;
str = str.substr(1);
}
else if (str[0] == '+')
str = str.substr(1);
if (str.substr(0,2) == "0x")
{
str = str.substr(2);
scale = 16;
}
else if(str.substr(0,1) == "0")
{
str = str.substr(1);
scale = 8;
}
else
;
int number = 0;
for (int i = 0; i != str.size(); ++i)
{
number *= scale;
int tmp = 0;
if (str[i] >= 'A' && str[i] <= 'F')
tmp = str[i] - 'A' + 10;
else if (str[i] >= 'a' && str[i] <= 'z')
tmp = str[i] - 'a' + 10;
else if (str[i] >= '0' && str[i] <= '9')
tmp = str[i] - '0';
number += tmp;
}
return number * sign;
}
bool Calculator::isequation(string str)
{
return has(str, "[+\\-\\*/%~&\\|^<>\\(\\)]", false);
}