中缀表达式转后缀表达式


前言

2021年9月18日,考研路上,今天复习了栈,顺手用了1个小时写了这个表达式求值算法,由于时间紧迫,代码未加注释,有些冗余,待有时间在修改.


一、中缀表达式转后缀表达式+表达式求值算法

代码如下(示例):

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

/*
 *      表达式求值:
 *  1.中缀表达式转后缀表达式
 *  2.后缀表达式求值
 *
 */
string transfer(string str)
{
    stack<char> temp;  //临时栈
    string result;
    int pointer = 0;
    while (pointer < str.length())
    {
        if (str[pointer] > '0' && str[pointer] < '9')
            result.push_back(str[pointer]);
        else if (str[pointer] == '(')
            temp.push(str[pointer]);
        else if (str[pointer] == ')')
        {
            while (!temp.empty() && temp.top() != '(')
            {
                result.push_back(temp.top());
                temp.pop();
            }
            if (temp.top() == '(')
                temp.pop();
        }
        else if (str[pointer] == '+' || str[pointer] == '-')
        {
            while (!temp.empty())
            {
                if (temp.top() == '(')
                {
                    temp.push(str[pointer]);
                    break;
                }
                else
                {
                    result.push_back(temp.top());
                    temp.pop();
                }
            }
            if (temp.empty())
                temp.push(str[pointer]);
        }
        else if (str[pointer] == '*' || str[pointer] == '/')
        {
            while (!temp.empty())
            {
                if (temp.top() == '(')
                {
                    temp.push(str[pointer]);
                    break;
                }
                else
                {
                    if (temp.top() == '*' || temp.top() == '/')
                    {
                        result.push_back(temp.top());
                        temp.pop();
                    }
                    else
                    {
                        temp.push(str[pointer]);
                        break;
                    }
                }
            }
            if (temp.empty())
                temp.push(str[pointer]);
        }
        pointer++;
    }
    while (!temp.empty())
    {
        result.push_back(temp.top());
        temp.pop();
    }
    return result;
}
int calculate(string str)
{
    string expression = transfer(str);
    stack<int> st;  //栈
    int pointer = 0;
    while (pointer < expression.length())
    {
        if (expression[pointer] > '0' && expression[pointer] < '9')
            st.push(expression[pointer] - '0');
        else
        {
            int right = st.top();  st.pop();
            int left = st.top(); st.pop();
            switch (expression[pointer]) {
                case '+':
                {
                    st.push(left + right);
                    break;
                }
                case '-':
                {
                    st.push(left - right);
                    break;
                }
                case '*':
                {
                    st.push(left * right);
                    break;
                }
                case '/':
                {
                    st.push(left / right);
                    break;
                }
            }
        }
        pointer++;
    }
    return st.top();
}
string input_data()
{
    cout<<"请输入表达式";
    char ch;
    string str;
    while ((ch = getchar()) != '\n')
        str.push_back(ch);
    return str;
}

int main()
{
    string str = "1+2*4/4+10";
    cout<<calculate(input_data())<<endl;
    return 0;
}

加油,加油考研上岸啊!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值