【23王道数据结构】表达式求值 C++&&Java

acwing 表达式求值, 题号3302

#include <iostream>
#include <cstring>
#include <algorithm>
#include <unordered_map>
#include <stack>

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()
{
    //定义优先级
    unordered_map<char, int> pr { {'+', 1}, {'-' , 1}, {'*', 2}, {'/', 2} };

    string st;
    cin >> st;
    getchar();

    //处理每个字符
    for (int i = 0; i < st.size(); i ++)
    {
        char c = st[i];

        //如果是数字的话,判断下一个是否也是数字,因为出现的不一定是个位数
        if (isdigit(c))
        {
            int x = 0, j = i;
            while (j < st.size() && isdigit(st[j]))
                x = x * 10 + st[j ++] - '0';
            // 第j个不是数字,所以要把i更新成j - 1, 因为之后还要i++
            i = j - 1;
            num.push(x);
        }
        // 左括号,直接运算符入栈
        else if (c == '(') op.push(c);

        // 如果是右括号的的话,如果操作符栈顶不是左括号,就一直取运算符运算
        else if (c == ')')
        {
            while (op.top() != '(')calc();
            op.pop();
        }
        // 如果是加减乘除的话,只要栈顶元素优先级大于当前元素
        //计算栈顶元素
        // 不大于的话入栈
        else 
        {
            while (op.size() && op.top() != '(' && pr[op.top()] >= pr[c]) calc();
            op.push(c);
        }
    }
    // 处理剩余的运算符
    while (op.size()) calc();
    cout << num.top() << endl;
    return 0;
}


import java.util.HashMap;
import java.util.Scanner;
import java.util.Stack;

public class Main {
    private static Stack<Integer> num = new Stack<Integer>();
    private static Stack<Character> op = new Stack<Character>();
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String st = scanner.nextLine();
        HashMap<Character, Integer> pr = new HashMap<>();
        pr.put('+', 1);
        pr.put('-', 1);
        pr.put('*', 2);
        pr.put('/', 2);

        for (int i = 0; i < st.length(); i++)
        {
            char c = st.charAt(i);
            if (isdigit(c))
            {
                int x = 0, j = i;
                while (j < st.length() && isdigit(st.charAt(j)))
                {
                    x = x * 10 + st.charAt(j++) - '0';
                }
                i = j - 1;
                num.push(x);
            }
            else if (c == '(') {
                op.push(c);
            }
            else if (c == ')') {
                while (op.peek() != '(') {
                    calc();
                }
                op.pop();
            }
            else
            {
                while (!op.empty() && op.peek() != '(' && pr.get(op.peek()) >= pr.get(c))
                {
                    calc();
                }
                op.push(c);
            }
        }
        while (!op.empty())
        {
            calc();
        }

        System.out.println(num.peek());
    }

    public static boolean isdigit(char c) {
        if (c >= '0' && c <= '9') return true;
        else {
            return false;
        }
    }

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值