五彩缤纷啊,帮忙改一下吧,谢谢!!!
查看原帖
五彩缤纷啊,帮忙改一下吧,谢谢!!!
1220218
yangxiyi楼主2023/12/27 19:32
#include<bits/stdc++.h>
using namespace std;
int main(){
	stack<int> s;
	string a;
	cin>>a;
	int ans=0,x=0,y=0;
	for(int i=0;i<a.size()-1;i++)
	{
		if(a[i]=='.')
		{
			s.push(a[i-1]-48);
		}
		
		if(a[i]=='+')
		{
			x=s.top();
			s.pop();
			y=s.top();
			s.pop();
			ans=y+x;
			s.push(ans);
			ans=0;
		}
		
		if(a[i]=='-')
		{
			x=s.top();
			s.pop();
			y=s.top();
			s.pop();
			ans=y-x;
			s.push(ans);
			ans=0;
		}
		
		if(a[i]=='*')
		{
			x=s.top();
			s.pop();
			y=s.top();
			s.pop();
			ans=y*x;
			s.push(ans);
			ans=0;
		}
		
		if(a[i]=='/')
		{
			x=s.top();
			s.pop();
			y=s.top();
			s.pop();
			ans=y/x;
			s.push(ans);
			ans=0;
		}
	}
	cout<<s.top()<<endl;
	return 0;
}
2023/12/27 19:32
加载中...