洛谷P1449——后缀表达式【栈】

洛谷P1449——后缀表达式【栈】

简单的用栈模拟一下就可以了,但要注意这道题要输入的是多位数,另外不用考虑太多输入的数字均为正整数。

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
stack<ll> st;

bool Check(char x){
	if(x != '+' && x != '-' && x != '*' && x != '/')
		return true;
    return false;
}

int main(int argc, char const *argv[])
{
	char ch;
	ll cnt = 0;
	while(~scanf("%c",&ch) && ch != '@'){
		if(ch == '+'){
			ll a = st.top();st.pop();
			ll b = st.top();st.pop();
			st.push(b + a);
		}
		else if(ch == '-'){
			ll a = st.top();st.pop();
			ll b = st.top();st.pop();
			st.push(b - a);
		}
		else if(ch == '*'){
			ll a = st.top();st.pop();
			ll b = st.top();st.pop();
			st.push(b * a);
		}
		else if(ch == '/'){
			ll a = st.top();st.pop();
			ll b = st.top();st.pop();
			st.push(b / a);
		}
		else if(ch == '.'){
			st.push(cnt);
			cnt = 0;
		}
		else{
			cnt = ch - '0' + cnt * 10;
		}

	}
	printf("%lld\n",st.top());
	return 0;
}
/*
本题坑点:输入的数为多位数,输入的数为负数,负数的符号如何处理//ps:不用考虑负数的问题
*/

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值