逆波蘭計算器

一個簡單的逆波蘭計算器,只支持加減乘除。

#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();
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章