数据结构——四则表达式运算(栈的应用)

实现思路

先输入string类型的中缀表达式,然后转换为后缀表达式,最后感觉string类型的后缀表达式来计算值

实现代码

#include <bits/stdc++.h>
using namespace std;
stack<char> optstack;
stack<int> numstack;
int compute(string line)
{
    int len = line.length();
    for (int i = 0; i < len; i++)
    {
        char ch = line[i];
        if (ch == '+')
        {
            int a = numstack.top();
            numstack.pop();
            int b = numstack.top();
            numstack.pop();
            numstack.push(a + b);
        }
        else if (ch == '-')
        {
            int a = numstack.top();
            numstack.pop();
            int b = numstack.top();
            numstack.pop();
            numstack.push(b - a);
        }
        else if (ch == '*')
        {
            int a = numstack.top();
            numstack.pop();
            int b = numstack.top();
            numstack.pop();
            numstack.push(a * b);
        }
        else if (ch == '/')
        {
            int a = numstack.top();
            numstack.pop();
            int b = numstack.top();
            numstack.pop();
            numstack.push(b / a);
        }
        //如果是数字就直接加入
        else
        {
            numstack.push(ch - '0');
        }
    }
    return numstack.top();
}
string toPostFix(string line)
{
    string postfix;
    int len = line.length();
    stack<char> buffer;
    for (int i = 0; i < len; i++)
    {
        //操作数直接输出
        if (line[i] >= '0' && line[i] <= '9')
            postfix.push_back(line[i]);
        else
        {
            //若为左括号或栈为空
            if (buffer.size() == 0 || line[i] == '(')
            {
                buffer.push(line[i]);
                continue;
            }
            else
            {
                if (line[i] == ')')
                {
                    while (buffer.size() != 0 && buffer.top() != '(')
                    {
                        postfix.push_back(buffer.top());
                        buffer.pop();
                    }
                    buffer.pop();
                }
                if (line[i] == '+' || line[i] == '-')
                {
                    while (buffer.size() != 0 && buffer.top() != '(')
                    {
                        postfix.push_back(buffer.top());
                        buffer.pop();
                    }
                    buffer.push(line[i]);
                }
                if (line[i] == '*' || line[i] == '/')
                {
                    while (buffer.size() != 0 && (buffer.top() == '*' || buffer.top() == '/'))
                    {
                        postfix.push_back(buffer.top());
                        buffer.pop();
                    }
                    buffer.push(line[i]);
                }
            }
        }
    }
    while (!buffer.empty())
    {
        postfix.push_back(buffer.top());
        buffer.pop();
    }
    return postfix;
}

int main()
{
    string line;
    cin >> line;
    string postfix = toPostFix(line);
    // cout << postfix << endl;
    cout << compute(postfix) << endl;
}

缺陷

  • 不能计算二位及以上位数的数字运算(需要对数字的位数进行判断,并用合适的集合存储)
  • 没有涉及判断错误的中缀表达式
  • 不能计算浮点数
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值