表达式求值训练 F 表达式求值

表达式求值训练




F 表达式求值


内存限制:64MB  时间限制:3s  Special Judge: No

题目描述:

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<iostream>
#include<cstdio>
#include<stack>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
int compare(char i)
{
    if(i=='-'||i=='+')return 1;
    if(i=='/'||i=='*')return 2;
    if(i=='(')return 0;
    return -1;
}
int main()
{
    char str[10000];
    int t,len;
    double n1=0,n2=0,n3=0,n4=0;
    cin>>t;
    while(t--)
    {
        cin>>str;
        len=strlen(str);
        stack<char>s;
        stack<double>j;
        j.push(-99999999);
        s.push('#');
        for(int i=0;i<len-1;i++){
            n1=0;n2=0;n3=0;n4=1;
            while(isdigit(str[i])||str[i]=='.')
            {
                if(str[i]=='.'){n3=1;i++;}
                if(n3==0)n1=n1*10+(str[i]-'0');
                else {
                    n1=n1+(str[i]-'0')/pow(10,n4);
                    n4++;
                }
                n2=1;
                if(isdigit(str[i+1])||str[i+1]=='.')i++;
                else break;
            }
            if(n2==1){
                j.push(n1);
            }
            else if(str[i]=='(')s.push(str[i]);
            else if(str[i]==')'){
                while(s.top()!='('){
                    if(j.top()==-99999999){break;}
                    n1=j.top();j.pop();
                    if(j.top()==-99999999){j.push(n1);break;}
                    n2=j.top();j.pop();
                    switch(s.top()){
                        case '-':j.push(n2-n1);break;
                        case '+':j.push(n1+n2);break;
                        case '/':j.push(n2/n1);break;
                        case '*':j.push(n1*n2);break;
                        default:break;
                    }
                    s.pop();
                }
                s.pop();
            }
            else {
                while(compare(s.top())>=compare(str[i])){
                    if(j.top()==-99999999){break;}
                    n1=j.top();j.pop();
                    if(j.top()==-99999999){j.push(n1);break;}
                    n2=j.top();j.pop();
                    int n3=0;
                    switch(s.top()){
                        case '-':j.push(n2-n1);break;
                        case '+':j.push(n1+n2);break;
                        case '/':j.push(n2/n1);break;
                        case '*':j.push(n1*n2);break;
                        default:break;
                    }
                    s.pop();
                }
                s.push(str[i]);
            }
        }
        while(s.top()!='#')
        {
            if(j.top()==-99999999){break;}
            n1=j.top();j.pop();
            if(j.top()==-99999999){j.push(n1);break;}
            n2=j.top();j.pop();
            int n3=0;
            switch(s.top()){
                case '-':j.push(n2-n1);break;
                case '+':j.push(n1+n2);break;
                case '/':j.push(n2/n1);break;
                case '*':j.push(n1*n2);break;
                default:break;
            }
            s.pop();
        }
        printf("%.2f\n",j.top());
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_40788630/article/details/80424261