-
Notifications
You must be signed in to change notification settings - Fork 0
/
AcWing3302.cpp
92 lines (92 loc) · 2.03 KB
/
AcWing3302.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
#include <iostream>
#include <stack>
#include <string>
using namespace std;
stack<int> num;
stack<char> op;
int getPrio(char c)
{
if(c == '+' || c == '-')
{
return 1;
}
else if(c == '*' || c == '/')
{
return 2;
}
else
{
return 0;
}
}
//计算的逻辑函数
void compute()
{
int k;
//注意:num1是第二个操作数,num2是第一个操作数
int num1 = num.top();
num.pop();
int num2 = num.top();
num.pop();
char c = op.top();
op.pop();
if(c == '+') k = num2 + num1;
else if(c == '-') k = num2 - num1;
else if(c == '*') k = num1 * num2;
else if (c == '/')
{
k = num2 / num1;
}
num.push(k);
}
int main()
{
string str;
cin >> str;
for(int i = 0;i != str.length(); i++)
{
if (isdigit(str[i]))
{
//注意这里k必须初始化为0,因为你后面对k操作时要用到k的初始值
//当然第一次操作时k是默认初始化为0,而后面几次时由于你没有显示
//初始化那k就变成了上次的值
int j = i, k = 0;
while (j < str.length() && isdigit(str[j]))
{
k = k * 10 + str[j] - '0';
j ++;
}
i = j - 1;
num.push(k);
}
else if (str[i] == '(')
{
op.push(str[i]);
}
else if (str[i] == ')')
{
while(op.top() != '(')
{
compute();
}
//记得将'('出栈
op.pop();
}
else
{
//如果栈顶运算符优先级比输入元素低,那就直接入栈,while循环都不会执行
//否则先计算,再入栈
while(op.size() && getPrio(str[i]) <= getPrio(op.top()))
{
compute();
}
op.push(str[i]);
}
}
while (op.size())
{
compute();
}
cout << num.top() << endl;
return 0;
}