表达式求值(代码填空)

大家经常使用各种编程语言,但大家有没有想过,代码中的表达式是怎么实现的?

假设一个表达式中包含加法乘法,以及一个自定义的运算 Smax(x,y) 表示分别求出 x,y值的各位数字之和,在从中找出最大数。

代码框中的代码是一种实现,请分析并填写缺失的代码。

样例输入
3
12+2*3
12*(2+3)
12*(2+3)+Smax(333,220+280)
样例输出
18
60
69
import java.util.*;
import java.math.*;

public class Main {
    public static final int N = 10010;
    public static char[] str = new char[N];
    public static Stack<Integer> num =new Stack<Integer>();
    public static Stack<String> op=new Stack<String>();

    public static int max(int a, int b) {
        return a > b  ? a : b;
    }

    public static int priority(String c) {
        if (c.equals("*")) {
            return 2;
        }
        if (c.equals("+")) {
            return 1;
        }
        return 0;
    }

    public static void calculate() {
        int suma = 0, sumb = 0;
        int b = num.peek();
        num.pop();
        int a = num.peek();
        num.pop();
        switch (op.peek()) {
            case "+":
                num.push(a + b);
                break;
            case "*":
                num.push(a * b);
                break;
            case "S":
                while (b != 0) {
                    sumb += b % 10;
                    b /= 10;
                }
                while (a != 0) {
                    suma += a % 10;
                    a /= 10;
                }
                num.push(max(suma, sumb));
                break;
        }
        op.pop();
    }

    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        int i = 0, j = 0, t = 0, n = 0, m = 0, tt = 0, tmp = 0;
        t = cin.nextInt();
        while(t-- != 0) {
            String s = cin.next();
            s += "=";
            str = s.toCharArray();

            while (!op.empty()) {
                op.pop();
            }
            while (!num.empty()) {
                num.pop();
            }
            int len = str.length;

            for (i = 0; i < len; i++) {
                if (str[i] >= '0' && str[i] <= '9') {
                    //sscanf(str + i, "%d%n", &tmp, &n);
                    //i += n - 1;
                    tmp = 0;
                    for (; i < len; ++i) {
                        if (str[i] >= '0' && str[i] <= '9') {
                            tmp = tmp * 10 + (int)(str[i] -'0');
                        }
                        else {
                            break;
                        }
                    }
                    --i;
                    num.push(tmp);
                }
                else if (str[i] == '(') {
                    op.push(String.valueOf(str[i]));
                }
                else if (str[i] == ')') {
                    while (op.peek().equals("(") != true) {
                        calculate();
                    }
                    op.pop();
                }
                else if (str[i] == 'S') {
                    op.push("(");
                    op.push("S");
                    i += 4;
                }
                else if (str[i] == ',') {
                }
                else if (/*在这里填写必要的代码*/str[i]=='*') {
                    op.push(String.valueOf(str[i]));
                }
                else {
                    while (false == op.empty() && priority(String.valueOf(str[i])) <= priority(op.peek())) {
                        calculate();
                    }
                    op.push(String.valueOf(str[i]));
                }
            }
            System.out.printf("%d\n", num.peek());
        }
    }
}

 

转载于:https://www.cnblogs.com/henuLiGang/p/8643525.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值