zoukankan      html  css  js  c++  java
  • 对于运算表达式的运算优先级的解决方法(不带括号)

    #include<iostream>
    #include<cstring>
    #include<iomanip>
    #include<string>
    #include<stack>
    using namespace std;
    char a[6];
    stack<int>Num;
    stack<char>Operator;
    int main()
    {
        int n;
        cin>>n;
        int t=n;
        while(t--)
        {
            while(!Num.empty())Num.pop();
            while(!Operator.empty())Operator.pop();
            int t1=1;
            for(int i=1; i<=n; i++)
            {
                cin>>a[i];
                if(i==1)
                {
                    Num.push(a[i]-'0');
                }
                else
                {
                    if(t1%2==0)
                    {
                        Operator.push(a[i]);
                    }
                    else
                    {
                        Num.push(a[i]-'0');
                        if(Operator.top()=='*')
                        {
                            int t1=Num.top();
                            Num.pop();
                            int t2=Num.top();
                            Num.pop();
                            Num.push(t1*t2);
                            Operator.pop();
                        }
                        else if(Operator.top()=='/')
                        {
                            int t1=Num.top();
                            Num.pop();
                            int t2=Num.top();
                            Num.pop();
                            Num.push(t2/t1);
                            Operator.pop();
                        }
                        else if(Operator.top()=='%')
                        {
                            int t1=Num.top();
                            Num.pop();
                            int t2=Num.top();
                            Num.pop();
                            Num.push(t2%t1);
                            Operator.pop();
                        }
                    }
                }
                t1++;
            }
            int e=Num.top();
            int flag=1;
            int sum=0;
            while(!Operator.empty())
            {
                flag=0;
                char temp=Operator.top();
                Operator.pop();
                int t1=Num.top();
                Num.pop();
                int t2=Num.top();
                Num.pop();
                if(temp=='+')
                {
                    Num.push(t1+t2);
                }
                else if(temp=='-')
                {
                    Num.push(t2-t1);
                }
                sum=Num.top();
            }
            if(flag==0)
                cout<<sum<<endl;
            else
            {


                cout<<e<<endl;
            }
        }
        return 0;
    }
  • 相关阅读:
    跳跃游戏
    不同路径
    最大子序和
    最长回文子序列
    最长公共子序列
    零钱兑换
    合并区间
    寻找数组的中心索引
    制造小程序中的一些经验
    h5写的一个签到积分系统
  • 原文地址:https://www.cnblogs.com/letlifestop/p/10263006.html
Copyright © 2011-2022 走看看