一個簡單的逆波蘭計算器,只支持加減乘除。
#include<bits/stdc++.h>
using namespace std;
int main()
{
stack<int> n;
int t,i,t1,t2;
char a[10];
while(1)
{
while(cin>>a&&a[0]!='=')
{
if(a[0]=='+')
{
t1=n.top();
n.pop();
t2=n.top();
n.pop();
n.push(t1+t2);
continue;
}
if(a[0]=='-')
{
t1=n.top();
n.pop();
t2=n.top();
n.pop();
n.push(t2-t1);
continue;
}
if(a[0]=='*')
{
t1=n.top();
n.pop();
t2=n.top();
n.pop();
n.push(t1*t2);
continue;
}
if(a[0]=='/')
{
t1=n.top();
n.pop();
t2=n.top();
n.pop();
n.push(t2/t1);
continue;
}
t=0;
for(i=0;i<strlen(a);i++)
{
t*=10;
t+=a[i]-48;
}
n.push(t);
memset(a,0,sizeof(a));
}
cout<<n.top()<<endl;
while(!n.empty()) n.pop();
}
}