简单来说,就是弄两个栈,判断执行:
上代码:
#include<iostream>
#include<stack>
#include<cstring>
using namespace std;
stack<char>s1,s2;
char now;
int main(){
string c;
cin>>c;
for(int i=0;i<c.length();i++)
{
if(c[i]>='0'&&c[i]<='9')
{
s2.push(c[i]);
if(!(c[i+1]>='0'&&c[i+1]<='9'))
s2.push('.');
}
else if(c[i]=='+'||c[i]=='-'){
while(!s1.empty()&&s1.top()!='(')
{
s2.push(s1.top());
s1.pop();
}
s1.push(c[i]);
}
else if(c[i]=='*'||c[i]=='/'){
while(!s1.empty()&&(s1.top()=='*'||s1.top()=='/'))
{
s2.push(s1.top());
s1.pop();
}
s1.push(c[i]);
}
else if(c[i]=='('){
s1.push(c[i]);
}
else if(c[i]==')'){
while(s1.top()!='(')
{
s2.push(s1.top());
s1.pop();
}
s1.pop();
}
}
while(!s1.empty())
{
s2.push(s1.top());
s1.pop();
}
while(!s2.empty())
{
s1.push(s2.top());
s2.pop();
}
while(!s1.empty())
{
cout<<s1.top();
s1.pop();
}
return 0;
}
因为csp中常考的就是中、后缀的计算,所以先写这些,后续再更