【数据结构】 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

猜你喜欢

转载自blog.csdn.net/ayitime/article/details/125829514