标签:ring int str i++ space main tac out 理解
#include <iostream>
#include <stack>
#include <string>
using namespace std;
int main()
{
stack<int> st;//初始化栈
string s;
cin>>s;
int x,y;
for(int i=0;i<s.size();i++)
{
if(s[i]==‘+‘)
{
x=st.top();//返回头部值
st.pop();//弹出
y=st.top();
st.pop();
st.push(x+y);
}
else if(s[i]==‘-‘)
{
x=st.top();
st.pop();
y=st.top();
st.pop();
st.push(y-x);
}
else if(s[i]==‘*‘)
{
x=st.top();
st.pop();
y=st.top();
st.pop();
st.push(x*y);
}
else if(s[i]==‘/‘)
{
x=st.top();
st.pop();
y=st.top();
st.pop();
st.push(y/x);
}
else
{
st.push(s[i]-‘0‘);//字符转实数压入
}
}
cout<<st.top()<<endl;
}
标签:ring int str i++ space main tac out 理解
原文地址:http://www.cnblogs.com/masterchd/p/6683101.html