简单实现了数学运算的先后问题

#include <iostream>
#include <stack>
#include <string>
#include <algorithm>


using namespace std;


int precede(const char a)
{
    switch(a)
    {
        case'+':
    case'-':
        return 0;
    case'*':
    case'/':
        return 1;
    default:
        return 2;
    }
};


double compute(char a, double b, double c)
{
    switch (a){
    case'+':    return b+c;
    case'-':    return b-c;
    case'*':    return b*c;
    case'/':    return b/c;
    default:
        return -1;
    }
};


void tian(const char*a)
{
    stack<char> s1;
    stack<double> s2;//s1为运算数栈,s2为操作符栈
    while(*a!='\0')
    {
        if((*a)>='0'&&(*a)<='9')
        {
            int x = *a-'0';
            s2.push(x);
            ++a;
            continue;
        }
        else{
            if(s1.empty())
            {
                s1.push(*a);
                a++;
                continue;
            }
            if(')'==*a)
            {
                while(s1.top()!='(')
                {
                    double x = s2.top();
                    s2.pop();
                    double y = s2.top();
                    s2.pop();
                    s2.push(compute(s1.top(),y,x));
                    s1.pop();


                }
                s1.pop();
                a++;
                continue;
            }
            if(precede(s1.top())<precede(*a))
            {
                s1.push(*a);
                a++;
                continue;
            }
            else if(s1.top()!='('){
                double x = s2.top();
                s2.pop();
                double y = s2.top();
                s2.pop();
                s2.push(compute(s1.top(),y,x));
                s1.pop();


                if(!s1.empty()&&(precede(s1.top())>precede(*a)))
                {
                    s1.push(*a);
                    a++;
                }
                else if(!s1.empty()){
                    double x = s2.top();
                    s2.pop();
                    double y = s2.top();
                    s2.pop();
                    s2.push(compute(s1.top(),y,x));
                    s1.pop();
                    s1.push(*a);
                    a++;
                }
                continue;
            }
            else{
                s1.push(*a);
                a++;
            }
        }
    }
    while(!s1.empty())
    {
        int x = s2.top();
        s2.pop();
        int y = s2.top();
        s2.pop();
        s2.push(compute(s1.top(),y,x));
        s1.pop();
    }


    while(!s2.empty())
    {
        cout<<s2.top()<<" ";
        s2.pop();
    }
    while(!s1.empty())
    {
        cout<<s1.top()<<" ";
        s1.pop();
    }
};


int main()
{
    cout<<"tian"<<endl;
    cout<<"hong"<<endl;
    stack<int>a;
    a.push(1);
    a.push(2);
    string s;
    cin>>s;


    tian(s.c_str());
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值