中缀转后缀表达式并计算

中缀转后缀表达式并计算的知识点

可以参考: 点我访问
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1237
此题我的代码必须用C++交才能过,估计涉及到精度问题!

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
#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include <sstream>
#include <vector>
#include <algorithm>
#include <stack>
#include <queue>
#include <cstdlib>
#include <iomanip>
using namespace std;
stack<string> signStack;
queue<string> lastQueue;
struct node
{
string ansString;
double ansNum;
};
// 计算运算结果
double getCalAns(string signString, double a, double b)
{
double nowNum;
if(signString == "+"){
nowNum = a + b;
}
else if(signString == "-"){
nowNum = a - b;
}
else if(signString == "*"){
nowNum = a * b;
}
else if(signString == "/"){
nowNum = a / b;
}

return nowNum;
}
//得到加减乘除的优先级
int getSignLevel(string signString)
{
if(signString == "+" || signString == "-"){
return 1;
}
else if(signString == "*" || signString == "/"){
return 2;
}

return 0;
}
//比较两个符号的等级用stack弹出符号
bool cmpSign(string pushString, string stackSign)
{
if(getSignLevel(pushString) > getSignLevel(stackSign)){
return true;
}
return false;
}
void stack_push(string pushString)
{
if(pushString == "+" || pushString == "-" || pushString == "*" || pushString == "/"){
//当signStack不为空时
while(signStack.size() && !cmpSign(pushString, signStack.top())){
lastQueue.push(signStack.top());
signStack.pop();
}
//最后把当前符号放入
signStack.push(pushString);
}
else if(pushString == "("){
signStack.push(pushString);
}
else if(pushString == ")"){
while(signStack.top() != "("){
lastQueue.push(signStack.top());
signStack.pop();
}
//弹出(
signStack.pop();
}
}
// 中缀转后缀并计算结果
node getMediumTOPost(string mediumStrng)
{
int len = mediumStrng.length();
string numString = "";
for(int i = 0; i < len; i++){
//为空字符
if(mediumStrng[i] == '*' || mediumStrng[i] == '/' || mediumStrng[i] == '+' || mediumStrng[i] == '-'){
//把数字压入
if(numString.size()){
lastQueue.push(numString);
numString = "";
}
stack_push(mediumStrng.substr(i, 1));
}
else if(mediumStrng[i] == '(' || mediumStrng[i] == ')'){
//把数字压入
if(numString.size()){
lastQueue.push(numString);
numString = "";
}
stack_push(mediumStrng.substr(i, 1));
}
else if(mediumStrng[i] != ' '){
numString += mediumStrng[i];
}

if(i == len - 1){
//把数字压入
if(numString.size()){
lastQueue.push(numString);
numString = "";
}
}
}
string ansString = "";
while(signStack.size()){
lastQueue.push(signStack.top());
signStack.pop();
}
queue<string> getNumQueue = lastQueue;
stack<double> tmpStack;
//double lastNum = 0;
while(lastQueue.size()){
ansString += (lastQueue.front() + " ");
lastQueue.pop();
}
//将字符串转换为double
while(getNumQueue.size()){
string stmp = getNumQueue.front();
if(stmp == "+" || stmp == "-" || stmp == "*" || stmp == "/"){
double a, b;
b = tmpStack.top();
tmpStack.pop();
a = tmpStack.top();
tmpStack.pop();
a = getCalAns(stmp, a, b);
tmpStack.push(a);
}
else{
tmpStack.push(atof(stmp.c_str()));
}
getNumQueue.pop();
}
//node ansNode;
//ansNode.ansNum = tmpStack.top();
//ansNode.ansString = ansString.substr(0, ansString.length() - 1);
return node{ansString.substr(0, ansString.length() - 1), tmpStack.top()};
}
int main()
{
ios::sync_with_stdio(false);
string cins;
while(getline(cin, cins) && cins != "0"){
//栈清空
while(signStack.size()) signStack.pop();
while(lastQueue.size()) lastQueue.pop();
node ansNode = getMediumTOPost(cins);
//cout << ansNode.ansString << endl;
cout << fixed << setprecision(2) << (ansNode.ansNum) << endl;
}
return 0;
}

本文标题:中缀转后缀表达式并计算

文章作者:HKer_YM

发布时间:2019年07月10日 - 11:45:29

最后更新:2020年03月01日 - 12:04:49

原始链接:https://blog.dreams-wj.top/2019/07/10/%E4%B8%AD%E7%BC%80%E8%BD%AC%E5%90%8E%E7%BC%80%E8%A1%A8%E8%BE%BE%E5%BC%8F%E5%B9%B6%E8%AE%A1%E7%AE%97/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。