-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
217 lines (198 loc) · 4.95 KB
/
main.cpp
File metadata and controls
217 lines (198 loc) · 4.95 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
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
#include <iostream>
#include <string>
#include <vector>
#include <stack>
#include <map>
#include <cstdlib>
#include <cmath>
#include <stdexcept>
using namespace std;
namespace op{
enum opIndex{lparen, rparen, assign, u_plus, u_minus, plus, minus, divide, times, mod, eos, operand};
}
enum Associativity{left2right, right2left};
enum operandType{identifier, pure_number, syntax_error};
//In-stack precedence
const size_t isp[]={0, 19, 3, 16, 16, 13, 13, 14, 14, 14, 0};
//Incomming precedence
const size_t icp[]={20, 19, 3, 16, 16, 13, 13, 14, 14, 14, 0};
const Associativity asso[]={left2right, left2right, right2left, right2left, left2right, left2right, left2right, left2right, left2right, left2right, left2right};
map<string, double> variable;
string getToken(const string &str, size_t *p);
op::opIndex tokenType(const string &str);
string trim(const string &str);
vector<string> Infix2Postfix(const string &str);
double calcPostfixExpr(const vector<string> &str);
operandType checkOpType(const string &);
double getOpValue(const string &);
int main(int argc, char *argv[]){
size_t sz=0;
string expr;
while(1){
cout<<">";
cin>>expr;
vector<string> result=Infix2Postfix(expr);
/*for(auto &s: result){
cout<<s;
}*/
cout<<"="<<calcPostfixExpr(result)<<endl;
}
return 0;
}
vector<string> Infix2Postfix(const string &str){
vector<string> result;
stack<string> opstack;
size_t p=0;
string token=getToken(str, &p);
//p refer to "next reading position", so when p==str.size(), token=last token before eos.
while(p<=str.size()){
const op::opIndex opi=tokenType(token);
//cout<<"??????"<<opi;
if(opi==op::operand){
result.push_back(token);
}else if(opi==op::rparen){
//unstack until lparen
for(;!opstack.empty()&&opstack.top()!="(";opstack.pop()){
result.push_back(opstack.top());
}
//unstack '('
opstack.pop();
}else{
for(;!opstack.empty()&&(isp[tokenType(opstack.top())]>icp[opi]||(isp[tokenType(opstack.top())]==icp[opi]&&asso[tokenType(opstack.top())]==left2right));opstack.pop())
result.push_back(opstack.top());
opstack.push(token);
}
token=getToken(str, &p);
}
//Unstack remaining operator...
for(;!opstack.empty(); opstack.pop())
result.push_back(opstack.top());
return result;
}
double calcPostfixExpr(const vector<string> &pExprToken){
stack<string> pstack;
for(auto &token: pExprToken){
op::opIndex opi=tokenType(token);
if(opi==op::operand)
pstack.push(token);
else{
double op2_val, op1_val;
string op2=pstack.top();
op2_val=getOpValue(op2);
pstack.pop();
if(opi==op::u_plus||opi==op::u_minus){
op2_val=getOpValue(op2);
pstack.push(to_string(opi==op::u_plus?op2_val:-op2_val));
continue;
}
string op1=pstack.top();
pstack.pop();
op1_val=getOpValue(op1);
switch(opi){
case op::assign:
variable[op1]=op2_val;
pstack.push(to_string(op2_val));
break;
case op::plus:
pstack.push(to_string(op1_val+op2_val)); break;
case op::minus:
pstack.push(to_string(op1_val-op2_val)); break;
case op::times:
pstack.push(to_string(op1_val*op2_val)); break;
case op::divide:
pstack.push(to_string(op1_val/op2_val)); break;
case op::mod:
pstack.push(to_string(fmod(op1_val,op2_val))); break;
}
}
}
if(checkOpType(pstack.top())==pure_number)
return stod(pstack.top());
else
return variable[pstack.top()];
}
string getToken(const string &str, size_t *p){
string token;
size_t sz=0;
if(*p>=str.size())
return (*p)++,"";
try{
stod(str.substr(*p), &sz);
}
catch(...){
sz=0;
}
if(str[*p]=='+'||str[*p]=='-'){
if(*p==0||str[*p-1]=='(')
return string(1, str[(*p)++])+"u";
else
return string(1, str[(*p)++]);
}
if(sz>0){
token=str.substr(*p, sz);
*p+=sz;
}else{
token=str[(*p)++];
}
return trim(token);
}
op::opIndex tokenType(const string &str){
switch(str[0]){
case '+':
return (str.size()==1)?(op::plus):(op::u_plus);
case '-':
return (str.size()==1)?(op::minus):(op::u_minus);
case '*':
return op::times;
case '/':
return op::divide;
case '(':
return op::lparen;
case ')':
return op::rparen;
case '%':
return op::mod;
case '=':
return op::assign;
default:
return op::operand;
}
return op::operand;
}
string trim(const string &str){
const size_t start=str.find_first_not_of(' ');
const size_t count=str.find_last_not_of(' ')-start+1;
//count =end-start+1
return str.substr(start, count);
}
operandType checkOpType(const string &token){
size_t sz;
try{
stod(token, &sz);
}catch(std::invalid_argument &e){
sz=0;
}
if(sz==0)
return identifier;
if(sz>0&&sz!=token.size())
return syntax_error;
else
return pure_number;
}
double getOpValue(const string &token){
//cout<<"GetOpValue="<<token<<endl;
switch(checkOpType(token)){
case pure_number:
return stod(token);
case identifier:
{
auto it=variable.find(token);
if(it==variable.cend()){
variable[token]=0.0;
return 0.0;
}else
return it->second;
}
case syntax_error:;
}
}