-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculation.cpp
112 lines (107 loc) · 2.6 KB
/
calculation.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
#include <iostream>
#include <stack>
#include <string>
#include <math.h>
#include <map>
using namespace std;
bool isop(char x)
{
char ops[] = {'+','-','*','/','(',')'};
for(int i=0; i<6; i++)
{
if(x == ops[i])
{
return true;
}
}
return false;
}
int jisuan(int num1, int num2, char op)
{
switch(op)
{
case '+':
return num1+num2;
case '-':
return num1-num2;
case '*':
return num1*num2;
case '/':
return num1/num2;
}
}
int main()
{
map<char, int> priority;
priority['+'] = 1;
priority['-'] = 1;
priority['*'] = 2;
priority['/'] = 2;
priority['('] = 3;
priority[')'] = 3;
string expression = "2+4*3+(5*2-3)*6";
string afexp = "";
stack<char> opstack;
for(int i=0; i<expression.length();i++)
{
if (isop(expression[i])){
if(opstack.empty() || expression[i] == '(')
{
opstack.push(expression[i]);
}
else if(expression[i] == ')')
{
while(!opstack.empty() && opstack.top()!='(')
{
afexp.push_back(opstack.top());
//cout << opstack.top();
opstack.pop();
}
opstack.pop();
}
else{
while(!opstack.empty() && priority[opstack.top()] >= priority[ expression[i] ] && opstack.top()!='(')
{
afexp.push_back(opstack.top());
//cout << opstack.top();
opstack.pop();
}
opstack.push(expression[i]);
}
}
else{
afexp.push_back(expression[i]);
//cout << expression[i];
}
}
while(!opstack.empty())
{
afexp.push_back(opstack.top());
//cout << opstack.top();
opstack.pop();
}
cout << "原式:"<<expression<<endl;
cout << "postfix:"<< afexp;
cout <<endl;
stack<int> numstack;
for(int i=0; i<afexp.length();i++)
{
if(isop(afexp[i]))
{
int num1 = numstack.top();
numstack.pop();
int num2 = numstack.top();
numstack.pop();
int num3 = jisuan(num2,num1,afexp[i]);
//cout << num3 <<endl;
numstack.push(num3);
}
else{
//cout << afexp[i] <<"\t"<< (int)afexp[i] -48<<endl;
numstack.push((int)afexp[i]-48);
}
}
cout <<endl;
cout <<"result:"<< numstack.top();
return 0;
}