pat 表达式转换
好多細(xì)節(jié)多找點數(shù)據(jù)輸入輸出看看自己哪里有不足就行了吧
#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());
}
總結(jié)
- 上一篇: 山中访友作者是谁啊?
- 下一篇: 结构题直接定义赋值语句