HDU 1237 简单计算器

来源:互联网 发布:淘宝安全证书过期 编辑:程序博客网 时间:2024/06/17 02:56
 /*简单计算器
Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 2   Accepted Submission(s) : 2
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
读入一个只包含 +, -, *, / 的非负整数计算表达式,计算该表达式的值。
Input
测试输入包含若干测试用例,每个测试用例占一行,每行不超过200个字符,整数和运算符之间用一个空格分隔。没有非法表达式。当一行中只有0时输入结束,相应的结果不要输出。
Output
对每个测试用例输出1行,即该表达式的值,精确到小数点后2位。
Sample Input

1 + 2
4 + 2 * 5 - 7 / 11
0

Sample Output

3.00
13.36
*/
#include <stack>  
#include <stdio.h>  
#include <string.h>  
using namespace std;  
bool check(char,char);  
void solve();  
char str[205],ch;  
double tmp,a,b;  
int i,len;  
stack<double> num;  
stack<char> cha;  
void solve()  
    {  
        len=strlen(str);  
        for(i=0;i<len;i++)  
        {  
            if(str[i]==' ')  
            {  
                continue;  
            }  
            if(str[i]>='0' && str[i]<='9') //数进栈
            {  
                tmp=0;  
                while(str[i]>='0' && str[i]<='9')  
                {  
                    tmp*=10;  
                    tmp+=str[i]-'0';  
                    i++;  
                }  
                num.push(tmp);  
            }  
            if(str[i]=='+' || str[i]=='-' || str[i]=='*' || str[i]=='/') //操作符进栈
            {  
                if(cha.empty())  
                {  
                    cha.push(str[i]);  
                }
                else{  
                    ch=cha.top();  
                    while(check(ch,str[i]))  
                    {  
                        a=num.top();  
                        num.pop();  
                        b=num.top();  
                        num.pop();  
                        cha.pop();  
                       switch(ch)  
                        {  
                            case '+':  
                                num.push(a+b);  
                                break;  
                            case '-':  
                                num.push(b-a);  
                                break;  
                            case '*':  
                                num.push(a*b);  
                                break;  
                            case '/':  
                               num.push(b/a);  
                                break;  
                        }  
      
                        if(cha.empty())  
                        {  
                            break;  
                        }
                        else
                        {  
                            ch=cha.top();  
                        }  
                    }  
                    cha.push(str[i]);  
                }  
            }  
        }  
        while(!cha.empty()) //此时操作栈里的操作符已经按优先级排好了
        {  
            a=num.top();  
            num.pop();  
            b=num.top();  
            num.pop();  
      
            ch=cha.top();  
            cha.pop();  
      
            switch(ch)  
            {  
                case '+':  
                    num.push(a+b);  
                    break;  
                case '-':  
                    num.push(b-a);  
                    break;  
                case '*':  
                    num.push(a*b);  
                    break;  
                case '/':  
                    num.push(b/a);  
                    break;  
            }  
        }  
      
        printf("%.2f\n",num.top());  
        num.pop();  
    }  
bool check(char ch1,char ch2)  
    {  
        if(ch1=='*'||ch1=='/')  
        {  
            return true;  
        }  
        if(ch2=='+'||ch2=='-')  
        {  
            return true;  
        }  
        return false;  
    }  
int main()  
    {  
        while(gets(str)!=NULL)  
        {  
            if(!strcmp(str,"0"))  
            {  
                break;  
            }  
            solve();  
        }  
        return 0;  
    } 
0 0
原创粉丝点击