解析表达式

内容源自:BUPT复试专题—解析表达式(2015)

题目描述

输入一个字符串形式的表达式,该表达式中包括整数,四则运算符(+、-、*、/),括号,三角函数(sin(x)、cos(x)、tan(x)),底数函数(lg(x)、ln(x)),计算该表达式的值

输入

输入一个字符串形式的表达式,保证中间及最终结果不超出double的范围

 

输出

表达式的值,保留6位小数

样例输入

3
3+5
((2-1)*5-1)*6
1+cos(0)

样例输出

3.000000
8.000000
24.000000
2.000000
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<stack>
using namespace std;
int main()
{
    string str;
    while(cin>>str)
    {
        stack<double>num;
        stack<char>chr;
        for(int i=0;i<str.size();i++)
        {
            double number=0;
            if(str[i]>='0' && str[i]<='9')
            {
                number=str[i]-'0';
                while(str[i+1]>='0'&&str[i+1]<='9')
                {
                    number=number*10+(str[i+1]-'0');
                    i++;
                }
                num.push(number);
            }
            else if(str[i]=='(')
                chr.push(str[i]);
            else if(str[i]=='+'||str[i]=='-')
                chr.push(str[i]);
            else if(str[i]=='*'||str[i]=='/')
            {
                char t=str[i];
                i++;
                number=str[i]-'0';
                while(str[i+1]>='0'&&str[i+1]<='9')
                {
                    number=number*10+(str[i+1]-'0');
                    i++;
                }
                num.push(number);
                double x=num.top();num.pop();
                double y=num.top();num.pop();
                double n=0;
                if(t=='*')
                    n=x*y;
                else n=x/y;
                num.push(n);
            }
            else if(str[i]==')')
            {
                char temp=chr.top();chr.pop();
                double x=num.top();num.pop();
                double y=num.top();num.pop();
                double n=0;
                if(temp=='+')
                    n=x+y;
                else if(temp=='-')
                    n=y-x;
                num.push(n);
                chr.pop();
                chr.pop();
            }
            else if(str[i]=='s'||str[i]=='c'||str[i]=='t')
            {
                char t=str[i];
                i=i+4;
                number=str[i]-'0';
                while(str[i+1]>='0'&&str[i+1]<='9')
                {
                    number=number*10+(str[i+1]-'0');
                    i++;
                }
                if(t=='s')
                {
                    number=sin(number);
                    num.push(number);
                }
                else if(t=='t')
                {
                    number=tan(number);
                    num.push(number);
                }
                else if(t=='c')
                {
                    number=cos(number);
                    num.push(number);
                }
                i++;
            }
            else if(str[i]=='l')
            {
                char t=str[i+1];
                i=i+3;
                number=str[i]-'0';
                while(str[i+1]>='0'&&str[i+1]<='9')
                {
                    number=number*10+(str[i+1]-'0');
                    i++;
                }
                if(t=='g')
                {
                    number=log10(number);
                    num.push(number);
                }
                else if(t=='n')
                {
                    number=log(number);
                    num.push(number);
                }
                i++;
            }
        }
        while(chr.size()>0&&chr.size()<100&&num.size()!=1)
        {
            char temp=chr.top();chr.pop();
            double x=num.top();num.pop();
            double y=num.top();num.pop();
            double n=0;
            if(temp=='+')
                n=x+y;
            else if(temp=='-')
                n=y-x;
            else if(temp=='(')
                continue;
            num.push(n);
        }
        printf("%.6f",num.top());cout<<endl;
    }
    return 0;
}

P.S.这个题,实际上很简单,就是把里面所包含的函数都算出来重新作为num加入栈,其他的就和“简单计算器”一样了(实际上我觉得这里比王道里的简单计算器写的好,倒是让我想起了被编译原理支配的恐惧,没有自己写一次主要是因为懒)。注意问题是char向num的转换,运算符的优先级问题,这些函数需要的头文件,这些函数在头文件里的函数名,以及最后注意要保留小数点后6位。

 
原文地址:https://www.cnblogs.com/xym4869/p/8659026.html