1019.簡單計算機

來源:互聯網
上載者:User

標籤:sim   pac   line   ==   計算表達   bool   簡單   3.3   ane   

題目描述:
    讀入一個只包含 +, -, *, / 的非負整數計算運算式,計算該運算式的值。
輸入:
    測試輸入包含若干測試案例,每個測試案例佔一行,每行不超過200個字元,整數和運算子之間用一個空格分隔。沒有非法運算式。當一行中只有0時輸入結束,相應的結果不要輸出。
輸出:
    對每個測試案例輸出1行,即該運算式的值,精確到小數點後2位。
範例輸入:
1 + 24 + 2 * 5 - 7 / 110
範例輸出:
3.0013.36

 Solution1:

#include<stdio.h> #define Max 200int main(){    double stack[Max];    int top,i,n;    double d,num,count;     char symbol,space;    while(scanf("%d ",&n)!=EOF&&n){        top=0;        stack[++top]=1.0*n;        while(scanf("%c%lf%c",&symbol,&num,&space)!=EOF){            if(symbol ==‘+‘){                stack[++top]=num;            }else if(symbol==‘-‘){                stack[++top]=-1*num;            }else if(symbol==‘*‘){                stack[top]=stack[top]*num;            }else if(symbol==‘/‘){                stack[top]=stack[top]/num;            }            if(space!=‘ ‘){                    break;            }        }         count=0;        for(i=1;i<=top;i++)            count+=stack[i];        printf("%.2lf\n",count);              }    return 0;}

 

 

Solution2:

 

#include<stack>#include<stdio.h>using namespace std;char str[220];int mat[][5]={        1,0,0,0,0,        1,0,0,0,0,        1,0,0,0,0,        1,1,1,0,0,        1,1,1,0,0};stack<int> op;stack<double> in;void getop(bool &reto,int &retn,int &i){    if(i==0 && op.empty()==true){        reto=true;        retn=0;        return;    }    if(str[i]==0){        reto=true;        retn=0;        return;    }    if(str[i]>=‘0‘ && str[i]<=‘9‘){        reto=false;    }    else{        reto=true;        if(str[i]==‘+‘){            retn=1;        }        else if(str[i]==‘-‘){            retn=2;        }        else if(str[i]==‘*‘){            retn=3;        }        else if(str[i]==‘/‘){            retn=4;        }        i+=2;        return;    }    retn=0;    for(;str[i]!=‘ ‘ && str[i]!=0;i++){        retn*=10;        retn+=str[i]-‘0‘;    }    if(str[i]==‘ ‘) i++;    return;}int main(){    while(gets(str)){        if(str[0]==‘0‘ && str[1]==‘0‘) break;        bool retop;        int retnum;        int idx=0;        while(!op.empty()) op.pop();        while(!in.empty()) in.pop();        while(true){            getop(retop,retnum,idx);            if(retop==false){                in.push((double)retnum);            }            else{                double temp;                if(op.empty()==true || mat[retnum][op.top()]==1){                    op.push(retnum);                }                else{                    while(mat[retnum][op.top()]==0){                        int ret=op.top();                        op.pop();                        double b=in.top();                        in.pop();                        double a=in.top();                        in.pop();                        if(ret==1) temp=a+b;                        else if(ret==2) temp=a-b;                        else if(ret==3) temp=a*b;                        else temp=a/b;                        in.push(temp);                    }                    op.push(retnum);                }            }            if(op.size()==2 && op.top()==0) break;        }        printf("%.2f\n",in.top());    }    return 0;}

 

1019.簡單計算機

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.