表达式求解

计算一个表达式的运算结果

使用C++自带stack堆栈对象来实现

输入

第一个输入t,表示有t个实例

第二行起,每行输入一个表达式,每个表达式末尾带#表示结束

输入t行

 

输出

每行输出一个表达式的计算结果,计算结果用浮点数(含4位小数)的格式表示

用cout控制浮点数输出的小数位数,需要增加一个库文件,并使用fixed和setprecision函数,代码如下:

#include
#include
using namespace std;

int main()

{ double temp = 12.34

cout<

}

输出结果为12.3400

 

样例输入

2

1+2*3-4/5#

(66+(((11+22)*2-33)/3+6)*2)-45.6789#

样例输出

6.2000

54.3211

本题用两个栈(一个表达式栈,一个数字栈)来实现。

 

#include
using namespace std;
// >....1
// <....2
// =....0
//优先级的顺序 
//pri[i] 表示前一个即top(),pri[i][j]表示当前的,即后一个 
// >入栈 
// <计算前两个数字 
// =说明结束了 
char op_all[7]={'+','-','*','/','(',')','#'};
int priority[7][7]={
    {2,2,2,2,1,2,1},
    {2,2,2,2,1,2,1},
    {1,1,2,2,1,2,1},
    {1,1,2,2,1,2,1},
    {1,1,1,1,1,2,1},
    {2,2,2,2,2,2,1},
    {0,0,0,0,0,0,0}};
double calculate(double a,double b,char type)
{
    double c;
    if(type=='+')
        c=a+b;
    else if(type=='-')
        c=a-b;
    else if(type=='*')
        c=a*b;
    else if(type=='/')
        c=a/b;
    return c;
} 
int get_num(char x)
{
    for(int j=0;j<7;j++)
        if(op_all[j]==x)
            return j;
    return 0;
}
int main()
{
    int temp;
    int t,i,flag,start;
    int l_op,r_op,len;
    char ch;
    double a,b,c;
    char type;
    double num;
    string expression,ss;
    cin>>t;
    while(t--)
    {
        temp=0;
        flag=0;
        start=0;
        cin>>expression;
        stack op_num;
        stack op;
        op.push('#');
        len = expression.length();
        for(i=0;i='0'&&ch<='9'||ch=='.')&&flag==0)
            {
                start=i-temp;
                flag=1;
            }
            else if((ch<'0'||ch>'9')&&ch!='.')
            {
                if(ch=='(')
                {
                    op.push(ch);
                    continue;
                }
                if(ch=='-'&&expression[i-1]=='(') 
                {
                    temp=1;
                    continue;
                }
                if(flag==1)
                {
                    stringstream ss(expression.substr(start,i-start));
                    ss>>num;
                    op_num.push(num);
                    flag=0; 
                    temp=0;
                }
                r_op=get_num(op.top());//栈顶的 
                l_op=get_num(ch);//现在的 
                if(priority[l_op][r_op]==1)
                {
                    op.push(ch);
                 } 
                else if(priority[l_op][r_op]==2)
                {
                    while(1)
                    {   
                        b=op_num.top();
                        op_num.pop();
                        a=op_num.top();
                        op_num.pop();
                        type=op.top();
                        op.pop();
                        if(ch==')')
                        {
                            if(op.top()=='(')
                            {
                                op.pop();
                                //ch=')'
                                ch='#'; 
                                l_op=get_num(ch);
                            }
                        }   
                        c=calculate(a,b,type);
                        op_num.push(c);
                        if(priority[l_op][get_num(op.top())]!=2)
                            break;
                    }
                    if(ch!=')'&&ch!='#')
                        op.push(ch);
                     
                }
                else if(priority[l_op][r_op]==0)
                {
                    while(op.top()!='#')
                    {
                        b=op_num.top();
                        op_num.pop();
                        a=op_num.top();
                        op_num.pop();
                        c=calculate(a,b,op.top());
                        op_num.push(c);
                        op.pop();
                    }
                    printf("%.4lf\n",op_num.top());
                    op_num.pop();
                    op.pop();
                }
            }
        }
    }
    return 0;
}

 

你可能感兴趣的:(表达式求解)