NYOJ-35 表达式求值

    xiaoxiao2021-04-16  35

    时间限制: 3000 ms  |  内存限制: 65535 KB 难度: 4 描述 ACM队的mdd想做一个计算器,但是,他要做的不仅仅是一计算一个A+B的计算器,他想实现随便输入一个表达式都能求出它的值的计算器,现在请你帮助他来实现这个计算器吧。 比如输入:“1+2/4=”,程序就输出1.50(结果保留两位小数) 输入 第一行输入一个整数n,共有n组测试数据(n<10)。 每组测试数据只有一行,是一个长度不超过1000的字符串,表示这个运算式,每个运算式都是以“=”结束。这个表达式里只包含+-*/与小括号这几种符号。其中小括号可以嵌套使用。数据保证输入的操作数中不会出现负数。 数据保证除数不会为0 输出 每组都输出该组运算式的运算结果,输出结果保留两位小数。 样例输入 2 1.000+2/4= ((1+2)*5+1)/4= 样例输出 1.50 4.00 /* #include<stack> stack<int> s; s.empty() 如果栈为空返回true,否则返回false s.size() 返回栈中元素的个数 s.pop() 删除栈顶元素但不返回其值 s.top() 返回栈顶的元素,但不删除该元素 s.push() 在栈顶压入新元素 我前面定义的是int,所以入栈和出栈都是int.*/

    #include <stdio.h>#include <stack>#include <math.h>#include <string.h>#include <ctype.h>int main(){    int n;    scanf("%d",&n);    while(n--){        char s[1005];        scanf("%s",s+1);        int len=strlen(s+1);        std::stack<char>osta;        std::stack<double>dsta;        osta.push('(');s[len]=')';        for(int i=1;i<=len;i++){            if(s[i]=='(')                osta.push('(');            else if(isdigit(s[i])){                double d=0.0;                int dot=0;                while(isdigit(s[i])||s[i]=='.'){                    if(isdigit(s[i]))                        d = d*10+(s[i]-'0');                    else                        dot=i;                    i++;                        }                i--;                if(dot!=0)                    dsta.push(d/pow(10,i-dot));                else                    dsta.push(d);                }            else if(s[i]=='+'||s[i]=='-'){                while(osta.top()!='('){//必须用while不能用if                     double a=dsta.top(); dsta.pop();                    double b=dsta.top(); dsta.pop();                    switch(osta.top()){                        case '+':dsta.push(b+a);break;                        case '-':dsta.push(b-a);break;                        case '*':dsta.push(b*a);break;                        case '/':dsta.push(b/a*1.0);break;                    }                    osta.pop();                }                osta.push(s[i]);            }            else if(s[i]=='*'||s[i]=='/'){                if(osta.top()!='(' && osta.top()!='+' && osta.top()!='-'){                    double a=dsta.top(); dsta.pop();                    double b=dsta.top(); dsta.pop();                    switch(osta.top()){                        case '*':dsta.push(b*a);break;                        case '/':dsta.push(b/a*1.0);break;                    }                    osta.pop();                }                osta.push(s[i]);            }            else if(s[i]==')'){                while(osta.top()!='('){                    double a=dsta.top(); dsta.pop();                    double b=dsta.top(); dsta.pop();                    switch(osta.top()){                        case '+':dsta.push(b+a);break;                        case '-':dsta.push(b-a);break;                        case '*':dsta.push(b*a);break;                        case '/':dsta.push(b/a*1.0);break;                    }                    osta.pop();                }                osta.pop();            }        }        printf("%.2lf\n",dsta.top());        dsta.pop();//一定记得要回收     }    return 0;}

    转载请注明原文地址: https://ju.6miu.com/read-672396.html

    最新回复(0)