【基础算法】- 算术表达式计算方法

0. 表达式树

xxx

1. 后缀表达式计算

后缀表达式:也叫逆波兰表达式,是表达式树的后序遍历。

在计算时,不用还原出整棵树,可用一个栈来辅助计算。

leetcode 150. 逆波兰表达式求值

class Solution {
public:

    int evalRPN(vector<string>& tokens) {

        int st[10005];
        int tt = 0;

        for( auto &x : tokens){
            if(x == "+" || x == "-" || x == "*" || x == "/") {
                int b = st[tt--];
                int a = st[tt--];
                if(x == "+") st[++tt] = a + b;
                else if(x == "-") st[++tt] = a - b;
                else if(x == "*") st[++tt] = a * b;
                else st[++tt] = a / b;
            }
            else st[++tt] = stoi(x);
        }
        return st[tt];

    }
};

2. 中缀表达式计算

法1:

  • 转成后缀表达式计算。

法2:

直接利用表达式树计算结果
需要两个栈:数字栈 和 符号栈

伪代码:

if 是数字
	入栈
else if 左括号 
	入栈
else if 右括号
	while 栈顶元素不是左括号
		消耗栈顶两个数字 和 一个符号 做一次计算
	弹出左括号
else //是普通符号
	while 当前符号优先级 <= 栈顶符号优先级
		消耗栈顶两个数字 和 一个符号 做一次计算(把之前可以计算的  都计算出来,用作下一次计算)
	当前符号入栈

/*
 * @Brief: 
 * @Author: 
 * @Date: 2021-07-14 
*/
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <string>
#include <stack>
#include <unordered_map>

using namespace std;

stack<int> num;
stack<char> op;

void calc(){
	int b = num.top(); num.pop();
	int a = num.top(); num.pop();
	
	char c = op.top(); op.pop();
	
	if(c == '+') num.push(a + b);
	else if(c == '-') num.push(a - b);
	else if(c == '*') num.push(a * b);
	else num.push(a / b);
	
}

int main(){

	string sc;
	cin >> sc;

	unordered_map<char, int> mp = {{'+', 1}, {'-', 1}, {'*', 2}, {'/', 2}};

	for(int i = 0; i < sc.size(); i++){
		auto c = sc[i];
		if( isdigit(c) ){
			int x = 0, j = i;
			while(j < sc.size() && isdigit(sc[j])) x = x * 10 + sc[j++] -'0';

			num.push(x);

			i = j - 1;
		}else if(c == '(') op.push(c);
		else if(c == ')'){
		    while(op.top() != '(') calc();
		    
		    // 弹出左括号
		    op.pop();
		      
		} else {
			// 普通运算符
			while(op.size() && mp[c] <= mp[op.top()]) calc();   // 把之前可以计算的  都计算出来
			op.push(c);

		}

	}
	while(op.size()) calc();

	cout << num.top() << endl;

	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值