关于后缀表达式
  • 板块灌水区
  • 楼主Xin_Ting
  • 当前回复23
  • 已保存回复23
  • 发布时间2024/8/28 20:53
  • 上次更新2024/8/28 22:43:32
查看原帖
关于后缀表达式
1332851
Xin_Ting楼主2024/8/28 20:53
#include<bits/stdc++.h>
using namespace std;
string str;
stack<int> s;
int a,b;
int main(){
	cin>>str;
	for(int i=0;i<str.size();i++){
		if(str[i]=='@'){
			break;
		}else if(str[i]<='9'&&str[i]>='1'){
			s.push(str[i]);
//			cout<<str[i]<<" ";
		}else if(str[i]=='.'){
			continue;
		}else{
			if(str[i]=='+'){
				a=s.top();
				s.pop();
				b=s.top();
				s.pop();
//				cout<<"1"<<" "<<a+b<<endl;
				s.push(a+b);
			}else if(str[i]=='-'){
				a=s.top();
				s.pop();
				cout<<s.top();
				b=s.top();
				cout<<b<<" ";
				s.pop();
//				cout<<"2"<<" "<<b-a<<endl;
				s.push(b-a);
//				for(int i=0;i<s.size();i++) cout<<s[i]<<" ";
//				cout<<endl;
			}else if(str[i]=='*'){
				a=s.top();
				s.pop();
				b=s.top();
				s.pop();
				s.push(a*b);
//				cout<<3<<" "<<a<<" "<<b<<endl;
//				cout<<"3"<<" "<<a*b<<endl;
		
			}else if(str[i]=='/'){
				a=s.top();
				s.pop();
				b=s.top();
				s.pop();
				s.push(b/a);
//				cout<<"4"<<" "<<b/a<<endl;

			}
		}
	}
	cout<<s.top();
	return 0;
}

样例没过

输入样例: 3.5.2.-*7.+@

输出结果:208

正确结果:16

编号:p1149

2024/8/28 20:53
加载中...