d016. 後序運算法
| From: [140.117.182.115] |
發表日期
:
2008-12-07 10:37
#include<iostream>
using namespace std;
class stack
{
public:
stack():top(-1){}
void push(int x){b[++top]=x;}
int pop(){return b[top--];}
private:
int top;
int b[100];
};
int main()
{
string s;
while(getline(cin,s)){
stack stk;
int i=0,tp;
while(s[i]!='\0')
{
if(isdigit(s[i]))
stk.push(s[i]-'0');
else
{
switch(s[i])
{
case'+':
stk.push(stk.pop()+stk.pop());break;
case'*':
stk.push(stk.pop()*stk.pop());break;
case'-':
tp=stk.pop();
stk.push(stk.pop()-tp);break;
case'/':
tp=stk.pop();
if(tp!=0)
stk.push(stk.pop()/tp);break;
case'%':
tp=stk.pop();
stk.push(stk.pop()%tp);break;
}
}
i++;
}
cout<<stk.pop()<<endl;
}
}