逆波蘭運算式計算(尾碼運算式)

來源:互聯網
上載者:User

標籤:

#include<iostream>#include<cstdlib>#include<cctype>using namespace std;template<class T>class stack{private:    T* base;    T* top;    int stackSize;public:    stack(int a=100):stackSize(a)    {        base=top=new T[stackSize];    }    ~stack()    {        delete [] base;        base=top=NULL;        cout<<"stack析構\n";    }    bool is_empty()const;    bool is_full()const;    bool pop(T &a);    bool push(T a);    int len()const;};template<class T>bool stack<T>::is_empty()const{    if(base==top)        return true;    else        return false;}template<class T>bool stack<T>::is_full()const{    if(top-base==stackSize)        return true;    else        return false;}template<class  T>bool stack<T>::push(T a){    if(!is_full())    {        *(top++)=a;        return true;    }    else        return false;;    }template<class T>bool stack<T>::pop(T& a){    if(!is_empty())    {        a=*(--top);        return true;    }    else        return false;}template<class T>int stack<T>::len()const{    cout<<(int*)base<<endl;    cout<<(int*)top<<endl;    return top-base;}int main(){    char c;    stack<double>s;        char bluff[10];    int i=0;        printf("輸入逆波蘭運算式,元素之間用空格分開,並以#結束\n");    scanf("%c",&c);    while(c!=‘#‘)    {        while(isdigit(c) || c==‘.‘)//處理的數是double型的        {            bluff[i++]=c;// 用一塊緩衝區儲存char型資料後轉換                        if(i>=10)            {                printf("\n輸入的數過大\n");                return 1;            }            scanf("%c",&c);            if(‘ ‘==c) // 處理數字後的空格,符號後的空格不管            {                bluff[i]=‘\0‘;                i=0;                s.push(atof(bluff));                break;            }        }        if(‘+‘==c || ‘-‘==c || ‘*‘==c || ‘/‘==c)        {            double d,e;            s.pop(d);            s.pop(e);            switch(c)            {            case ‘+‘:                s.push(d+e);                break;            case ‘-‘:                s.push(e-d);// 後出的減去前出的                break;            case ‘*‘:                s.push(e*d);                break;            case ‘/‘:                if(d !=0)                    s.push(e/d);                else                {                    printf("\n除數不能為0\n");                    return 1;                }                break;            default:                printf("\n符號不能識別\n");                return 1;            }        }        scanf("%c",&c);    }    double ans;    s.pop(ans);    printf("\n計算結果是%f\n",ans);        return 0;}

 

逆波蘭運算式計算(尾碼運算式)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.