Discuss / Java / 5.28 中缀转后缀及指定变量运算 作业

5.28 中缀转后缀及指定变量运算 作业

Topic source

ANGERIED

#1 Created at ... [Delete] [Delete and Lock User]
// 这一题实际刚好用了第二题的代码,稍微加了一点代码就可以了,但是bug依旧是数字的分离问题。
 public static void main(String[] args) {
        String exp = "x + 2 * (y - 5)";
        SuffixExpression se = compile(exp);
        Map<String, Integer> env = Map.of("x", 1, "y", 9);
        int result = se.execute(env);
        System.out.println(exp + " = " + result + " " + (result == 1 + 2 * (9 - 5) ? "✓" : "✗"));
    }

    static SuffixExpression compile(String exp) {
        // TODO:        
        Deque<String> deque = new LinkedList<>();
        char[] chars = exp.toCharArray();

        for (char c : chars) {
            if (getYorN(c)) {
                deque.offerFirst(String.valueOf(c));
            } else if (getIntegYorN(c)) {
                deque.offerLast(String.valueOf(c));
            } else {
                continue;
            }
        }

        return new SuffixExpression(deque);
    }

    static boolean getYorN(char it) {
        switch (it) {
            case '+':
            case '-':
            case '*':
            case '/':
                System.out.println(it);
                return true;
            default:
                return false;
        }
    }

    static boolean getIntegYorN(char it) {
        switch (it) {
            case '1':
            case '2':
            case '3':
            case '4':
            case '5':
            case '6':
            case '7':
            case '8':
            case '9':
            case '0':
            case 'x':
            case 'y':
                System.out.println(it);
                return true;
            default:
                return false;
        }
    }
}

class SuffixExpression {
    Deque<String> deque;

    public SuffixExpression(Deque<String> deque) {
        this.deque = deque;
    }

    int execute(Map<String, Integer> env) {
        // TODO:        
        String x = String.valueOf(env.get("x"));
        String y = String.valueOf(env.get("y"));

        do {
            String c1 = deque.pollLast();
            String c2 = deque.pollLast();
            String opt = deque.pollFirst();
            if (c1.equals("x")) {
                c1 = x;
            }
            if (c1.equals("y")) {
                c1 = y;
            }
            if (c2.equals("x")) {
                c2 = x;
            }
            if (c2.equals("y")) {
                c2 = y;
            }
            deque.offerLast(String.valueOf(getNum(c1, c2, opt)));
        }
        while (deque.size() != 1);

        return Integer.valueOf(deque.poll());
    }

    static int getNum(String c1, String c2, String opt) {
        Integer i1 = Integer.valueOf(c1);
        Integer i2 = Integer.valueOf(c2);
        switch (opt) {
            case "+":
                return i2 + i1;
            case "-":
                return i2 - i1;
            case "*":
                return i2 * i1;
            case "/":
                return i2 / i1;
            default:
                return 0;
        }
    }

江舟独行

#2 Created at ... [Delete] [Delete and Lock User]

解决多位数分离,只需要可在遍历字符并压栈时添加一flag,判断上一字符是否为数字类型:

1.如果上了字符为数字,删除栈尾元素,将删除的元素与当前数字字符组合压入到栈尾;

2.如果上一字符不为数字,直接将当前数字压入栈尾。

原代码处:

static SuffixExpression compile(String exp) {...}

修改为:

static SuffixExpression compile(String exp) {
    // TODO:    
    Deque<String> deque = new LinkedList<>();
    char[] chars = exp.toCharArray();
    boolean lastisnum=false;//添加判断旗帜
    for (char c : chars) {

        if (getYorN(c)) {
            deque.offerFirst(String.valueOf(c));
            lastisnum=false;//非数字
        } else if (getIntegYorN(c)) {
            if (lastisnum){
                deque.offerLast(deque.pollLast()+String.valueOf(c));
            }
            else {
                deque.offerLast(String.valueOf(c));
            }
            lastisnum=true;//是数字
        } else {
            lastisnum=false;//非数字
            continue;
        }

    }
    System.out.println(deque.toString());
    return new SuffixExpression(deque);
}

  • 1

Reply