中缀表达式转后缀表达式求值

中缀表达式转后缀表达式是数据结构栈和队列很好的应用例子。难点主要就在于如何将中缀表达式转换为后缀表达式的问题上。下面就是转换的规则和需要注意的地方。

首先需要开一个栈和队列。栈用作临时的存储空间。队列则用于存放最终的后缀表达式。

中缀表达式转后缀表达式的规则:

1.开始在栈中压入一个#。然后从中缀表达式的左边开始遍历,如果遇到数字直接放入队列。如果是操作符则和栈顶比较,
如果优先级大于栈顶则入栈,否则将栈顶压入队列并将当前操作符入栈。
2.如果遇到'('。则无条件入栈不用和栈顶进行比较。
3.如果遇到')'。不用入栈,从栈顶开始将栈内元素弹出并压入队列中,直到遇到'('。将'('弹出栈。
后缀表达式运算规则:需要设立一个栈用于存放计算结果。
1.从队列中取元素,如果是操作数直接出队入栈。
2.如果是运算符则出队,并且弹出栈的两个元素运算并将运算结果压入栈内。直到队列为空。

#include <iostream>
#include <stack>
#include <queue>
using namespace std;


stack<char>s;
queue<char>q;


int Operator(char ch)
{
    switch(ch)
    {
        case '+':
        case '-':
        case '*':
        case '/':
        case '(':
        case ')':
        case '#':
            return 1;
        default:
            return 0;
    }
}


int priority(char ch)
{
    switch(ch)
    {
        case '+':
        case '-':
            return 1;
        case '*':
        case '/':
            return 2;
        case '#':
            return -1;
        case '(':
            return 0;
    }
}


double caculate(double a,char ch,double b)
{
    switch(ch)
    {
        case '+':
            return a+b;
        case '-':
            return a-b;
        case '*':
            return a*b;
        case '/':
            return a/b;
    }   
}


void change(char *ch)
{
    s.push('#');
    int i = 0;
    while (ch[i] != '#')
    {
        if (!Operator(ch[i]))
        {
            q.push(ch[i]);
            i++;
        }
        else 
        {
            if (ch[i] == '(')
            {
                s.push(ch[i]);
                i++;
            }
            else if (ch[i] == ')')
            {
                while (s.top() != '(')
                {
                    q.push(s.top());
                    s.pop();
                }
                s.pop();
                i++;
            }
            else 
            {
                if (priority(ch[i]) > priority(s.top()))
                {
                    s.push(ch[i]);
                    i++;
                }
                else
                {
                    q.push(s.top());
                    s.pop();
                    s.push(ch[i]);
                    i++;
                }
            }
        }
    }
    /*将剩余的栈内的元素弹出栈存在队列中*/
    while (s.top() != '#')
    {
        q.push(s.top());
        s.pop();
    }
}


double result()
{
    stack<double>s;
    while (!q.empty())
    {
        if (!Operator(q.front()))
        {
            s.push(q.front()-48);
            q.pop();
        }
        else
        {
            double b = s.top();
            s.pop();
            double a = s.top();
            s.pop();
            char ch = q.front();
            s.push(caculate(a,ch,b));
            q.pop();
        }
    }
    cout << s.top() << endl;
}


int main()
{
    char c[10];
    gets(c);
    change(c);
    result();
    system("pause");
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值