简单的用栈模拟一下就可以了,但要注意这道题要输入的是多位数,另外不用考虑太多输入的数字均为正整数。
#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:不用考虑负数的问题
*/