pat 表达式转换

好多细节多找点数据输入输出看看自己哪里有不足就行了吧

#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());
} 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值