【数据结构】 C++实现 四则运算表达式求值

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


string zero_pad(string s) {
    int n = s.size();
    for(int i=0; i<n; i++) {
        if(s[i]=='[' || s[i]=='{')
            s[i] = '(';
        else if(s[i]==']' || s[i]=='}')
            s[i] = ')';
    }

    for(int i=1; i<n; i++) {
        if(s[i]=='-' && s[i-1]=='(') {
            s.insert(i, "0");
            n++;
        }
    }

    return s;
}

int main()
{
    string s;
    getline(cin, s);
	
	// 针对 2+(-3*(6-2)) 的负号情况,在左括号和符号之间做补0处理
    s = zero_pad(s);
	
	// 根据中缀表达式 生成 后缀表达式(参考浙大数据结构表达式求值)
    stack<string> post;        // 存数字
    stack<string> symb;       // 存符号
    int left = 0;
    while(left < s.size()) {
        // 处理数字
        if(s[left]>='0' && s[left]<='9') {
            string num = "";
            while(s[left]>='0' && s[left]<='9') {
                num += s[left];
                left++;
            }
            post.push(num);
        }
        // 处理符号
        else {
            if(s[left]=='(') {
                symb.push("(");
            }
            else if(s[left]=='+') {
                while(!symb.empty() && (symb.top()=="+" ||symb.top()=="-" || symb.top()=="*" ||  symb.top()=="/" )) {
                    post.push(symb.top());
                    symb.pop();
                }
                symb.push("+");
            }
            else if(s[left]=='-') {
                while(!symb.empty() && (symb.top()=="+" ||symb.top()=="-" || symb.top()=="*" ||  symb.top()=="/" )) {
                    post.push(symb.top());
                    symb.pop();
                }
                symb.push("-");
            }
            else if(s[left]=='*') {
                while(!symb.empty() && ( symb.top()=="*" ||  symb.top()=="/" )) {
                    post.push(symb.top());
                    symb.pop();
                }
                symb.push("*");
            }
            else if(s[left] =='/') {
                while(!symb.empty() && ( symb.top()=="*" ||  symb.top()=="/" )) {
                    post.push(symb.top());
                    symb.pop();
                }
                symb.push("/");
            }

            else if(s[left]==')') {
                while(!symb.empty() &&  symb.top()!="(") {
                    post.push(symb.top());
                    symb.pop();
                }
                symb.pop();
            }
            left++;
        }
    }
	// 剩下的符号也要倒进去
    while(!symb.empty()) {
        post.push(symb.top());
        symb.pop();
    }
	
	// 把生成好的后缀表达式倒过来,便于处理
    stack<string> helper;
    while(!post.empty()) {
        helper.push(post.top());
        post.pop();
    }

    while(!helper.empty()) {
        if(helper.top() == "+") {
            int b = atoi(post.top().c_str());
            post.pop();
            int a = atoi(post.top().c_str());
            post.pop();
            post.push(to_string(a + b));
        }
        else if(helper.top() == "-") {
            int b = atoi(post.top().c_str());
            post.pop();
            int a = atoi(post.top().c_str());
            post.pop();
            post.push(to_string(a - b));
        }
        else if(helper.top() == "*") {
            int b = atoi(post.top().c_str());
            post.pop();
            int a = atoi(post.top().c_str());
            post.pop();
            post.push(to_string(a * b));
        }
        else if(helper.top() == "/") {
            int b = atoi(post.top().c_str());
            post.pop();
            int a = atoi(post.top().c_str());
            post.pop();
            post.push(to_string(a / b));
        }
        else {
            post.push(helper.top());
        }
        helper.pop();
    }

    cout << post.top();
    
    return 0;
}
// 3+2*{1+2*[-4/(8-6)+7]}
// 25
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值