给定一个中缀表达式转成后缀表达式
#include<bits/stdc++.h>
using namespace std;
string s;
stack<char> q;
int judge(char ch)
{
if(ch == '(') return 6;
if(ch == '*' || ch == '/') return 3;
if(ch == '+' || ch == '-') return 1;
if(ch == ')') return 0;
}
int main()
{
cin >> s;
for(int i = 0;i < s.size();i++)
{
if(s[i] >= 'A' && s[i] <= 'Z')//遇见字符直接输出
{
cout<<s[i];
}
else//如果是运算符
{
if(q.empty() || judge(s[i]) > judge(q.top())) q.push(s[i]);//如果栈为空或该字符的优先级大于栈顶的优先级,直接入栈
else while(judge(s[i]) <= judge(q.top()))
{
if(s[i] == ')' && q.top() == '(') q.pop();
else {
cout<<q.top();
q.pop();
q.push(s[i]);
}
}
}
}
return 0;
}
真的不知道哪里错了...