好多细节多找点数据输入输出看看自己哪里有不足就行了吧
#include<bits/stdc++.h>
using namespace std;
string str;
int flag=1;
int f=1;
bool op(int i,char c)
{
if(str[i-1]>'9'||str[i-1]<'0'&&(str[i-1]!=')'))
return 0;
if(c=='+'||c=='-'||c=='*'||c=='/')
return true;
return false;
}
bool out(int i,char c)
{
if(c>='0'&&c<='9')
return 1;
if(c=='.')
return 1;
if(c=='-')
{
if(i==0)
return 1;
else if(str[i-1]>='0'&&str[i-1]<='9'||str[i-1]==')')
return 0;
return 1;
}
return 0;
}
map<char,int > m;
void solve(int l,int r)
{
stack<char> s;
for(int i=l;i<=r;i++)
{
if(out(i,str[i]))
{
if(flag==1||f)
{
flag=0;
f=1;
cout<<str[i];
}
else
{
f=1;
cout<<' '<<str[i];
}
}
else if(op(i,str[i]))
{
f=0;
if(!s.empty())
{
while(m[str[i]]<=m[s.top()])
{
cout<<' '<<s.top();
f=0;
s.pop();
if(s.empty())
break;
}
}
s.push(str[i]);
}
else if(str[i]=='(')
{
stack<char> q;
int j=i+1;
q.push('(');
while(!q.empty())
{
if(str[j]==')')
q.pop();
else if(str[j]=='(')
q.push('(');
j++;
}
solve(i+1,j-2);
i=j-1;
}
}
while(!s.empty())
{
f=0;
cout<<' '<<s.top();
s.pop();
}
}
int main()
{
m['+']=1;
m['-']=1;
m['*']=2;
m['/']=2;
cin>>str;
solve(0,str.length());
}