表达式求值 整数的加减乘除带括号运算

最近闲着无聊在看表达式求值,

自己做了一个整数 加减乘除的小程序

代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<stack>
#include<algorithm>
#include<cmath>
using namespace std;
int a1[7][7]={
        {0,0,0,0,0,0,0},
        {0,1,0,0,1,1,0},
        {0,1,1,0,1,1,1},
        {0,0,0,1,1,0,0},
        {0,1,1,1,1,1,1},
        {0,1,0,0,1,1,0},
        {0,1,1,0,1,1,1},
    };
int bj(char a,char b)//判断算符优先顺序
{
    int i=0,j=0;
    if(a=='+')i=1;if(a=='*')i=2;if(a=='(')i=3;if(a==')')i=4;if(a=='-')i=5;if(a=='/')i=6;
    if(b=='+')j=1;if(b=='*')j=2;if(b=='(')j=3;if(b==')')j=4;if(b=='-')j=5;if(b=='/')j=6;
    return a1[i][j];
}
int main()
{
    int k;
    cin>>k;
    while(k--)
    {
        stack<double>sta1;
        stack<char>sta2;
        char str1[10000];
        cin>>str1;
        int len1=strlen(str1);
        int n1=0;
        sta2.push('#');
        for(int i=0;i<len1;i++){
            if(str1[i]<='9'&&str1[i]>='0')//添加数字时,可能是一个多位数,所以要
            {
                double n2=(double)(str1[i]-'0');
                i++;
                while(str1[i]>='0'&&str1[i]<='9'&&i<len1)
                {
                    n2=n2*10+double(str1[i]-'0');
                    i++;
                }
                sta1.push(n2);
                if(i>=len1)
                    break;
            }
            if((str1[i]<'0'||str1[i]>'9')&&sta2.top()=='#')//添加第一个算符
                sta2.push(str1[i]);
            else if(str1[i]=='(')//若是(,则直接插入栈
            {
                sta2.push(str1[i]);
                continue;
            }
            else if(bj(sta2.top(),str1[i])){//若栈顶算符优先,则进行相对应操作
                double k1=sta1.top();sta1.pop();
                double k2=sta1.top();sta1.pop();
                if(sta2.top()=='*'){
                    k1=k1*k2;
                    sta1.push(k1);
                }
                if(sta2.top()=='+'){
                    k1=k1+k2;
                    sta1.push(k1);
                }
                if(sta2.top()=='-'){
                    k1=k2-k1;
                    sta1.push(k1);
                }
                if(sta2.top()=='/'){
                    k1=k2/k1;
                    sta1.push(k1);
                }
                sta2.pop();//栈顶算符已使用完毕,应出栈;
                if(str1[i]!=')'){
                    sta2.push(str1[i]);
                }
                else {
                    if(sta2.top()=='(')sta2.pop();
                    else i=i-1;//若输入的算符为),但栈顶并不是(,则重复一次
                }
            }
            else if(!bj(sta2.top(),str1[i])){
                sta2.push(str1[i]);
            }
        }
        while(sta2.top()!='#')//此时将sta2中的算符进行完
        {
            double k3=sta1.top();sta1.pop();
            double k4=sta1.top();sta1.pop();
            if(sta2.top()=='+')k3=k3+k4;
            if(sta2.top()=='*')k3=k3*k4;
            if(sta2.top()=='-')k3=k4-k3;
            if(sta2.top()=='/')k3=k4/k3;
            sta1.push(k3);
            sta2.pop();
        }
        cout<<sta1.top()<<endl;
        sta1.pop();
    }
    return 0;
}

猜你喜欢

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