如何让我的计算字符串方程的代码与 PI 和 E 一起使用

How do I allow my code that evaluates string equations to work with PI and E

我从之前回答的问题中提取了这段代码,我正在尝试对其进行扩展,以便当字符串包含“E”和“PI”时它可以与 E 和 PI 一起使用。我真的不明白代码是如何工作的,因为我是 Java 的新手,而且对原始评论的解释不是很好(不幸的是,我已经失去了对评论的 link)。

public static double eval(final String str) {
    return new Object() {
        int pos = -1, ch;
        void nextChar() {
            ch = (++pos < str.length()) ? str.charAt(pos) : -1;
        }

        boolean eat(int charToEat) {
            while (ch == ' ') nextChar();
            if (ch == charToEat) {
                nextChar();
                return true;
            }
            return false;
        }

        double parse() {
            nextChar();
            double x = parseExpression();
            if (pos < str.length()) throw new RuntimeException("Unexpected: " + (char)ch);
            return x;
        }

        // Grammar:
        // expression = term | expression `+` term | expression `-` term
        // term = factor | term `*` factor | term `/` factor
        // factor = `+` factor | `-` factor | `(` expression `)`
        //        | number | functionName factor | factor `^` factor

        double parseExpression() {
            double x = parseTerm();
            for (;;) {
                if      (eat('+')) x += parseTerm(); // addition
                else if (eat('-')) x -= parseTerm(); // subtraction
                else return x;
            }
        }

        double parseTerm() {
            double x = parseFactor();
            for (;;) {
                if      (eat('*')) x *= parseFactor(); // multiplication
                else if (eat('/')) x /= parseFactor(); // division
                else return x;
            }
        }

        double parseFactor() {
            if (eat('+')) return parseFactor(); // unary plus
            if (eat('-')) return -parseFactor(); // unary minus

            double x;
            int startPos = this.pos;
            if (eat('(')) { // parentheses
                x = parseExpression();
                eat(')');
            } else if ((ch >= '0' && ch <= '9') || ch == '.') { // numbers
                while ((ch >= '0' && ch <= '9') || ch == '.') nextChar();
                x = Double.parseDouble(str.substring(startPos, this.pos));
            } else if (ch >= 'a' && ch <= 'z') { // functions
                while (ch >= 'a' && ch <= 'z') nextChar();
                String func = str.substring(startPos, this.pos);
                x = parseFactor();
                if (func.equals("sqrt")) x = Math.sqrt(x);
                else if (func.equals("sin")) x = Math.sin(Math.toRadians(x));
                else if (func.equals("cos")) x = Math.cos(Math.toRadians(x));
                else if (func.equals("tan")) x = Math.tan(Math.toRadians(x));
                else if (func.equals("csc")) x = 1/Math.sin(Math.toRadians(x));
                else if (func.equals("sec")) x = 1/Math.cos(Math.toRadians(x));
                else if (func.equals("cot")) x = 1/Math.tan(Math.toRadians(x));
                else if (func.equals("log")) x = Math.log(x);
                else throw new RuntimeException("Unknown function: " + func);
            } else {
                throw new RuntimeException("Unexpected: " + (char)ch);
            }

            if (eat('^')) x = Math.pow(x, parseFactor()); // exponentiation

            return x;
        }
    }.parse();
}

解决方案的关键是修改语法以支持您支持的命名常量。因此,要求(如您的示例所示)命名常量采用大写字母:A - Z(以区分函数)。

(指定的语法不完整,因为它没有指定函数的语法,但代码表明它是 trig 和 log 函数子集中的小写字符。)

因此,语法更新为:

// Grammar:
// expression = term | expression `+` term | expression `-` term
// term = factor | term `*` factor | term `/` factor
// factor = `+` factor | `-` factor | `(` expression `)`
//        | number | functionName factor | factor `^` factor | namedConstant
// namedConstant = 'PI' | 'E'

唯一需要修改的是更新 parseFactor,唯一的改变是在底部添加 else if

double parseFactor() {
    if (eat('+')) return parseFactor(); // unary plus
    if (eat('-')) return -parseFactor(); // unary minus

    double x;
    int startPos = this.pos;
    if (eat('(')) { // parentheses
        //..no changes
    } else if ((ch >= '0' && ch <= '9') || ch == '.') { // numbers
        //..no changes
    } else if (ch >= 'a' && ch <= 'z') { // functions
        //..no changes

    } else if (ch >= 'A' && ch <= 'Z') { // named constants
        while (ch >= 'A' && ch <= 'Z') nextChar();
        String s = str.substring(startPos,this.pos);
        if (s.equals("PI")) {
            x = Math.PI;
        } else if (s.equals("E")) {
            x = Math.E;
        } else {
            throw new RuntimeException("Invalid constant: "+s);
        }
    } else {
        throw new RuntimeException("Unexpected: " + (char)ch);
    }

    //...continue with your code

所以这些测试:

    double d = eval("2.5");
    System.out.println(d);

    d = eval("-2.5");
    System.out.println(d);
    
    d = eval("2.5 * 10");
    System.out.println(d);
    
    d = eval("3 * PI");
    System.out.println(d);
    
    d = eval("E ^ 2");
    System.out.println(d);

    d = eval("7.5 + (2.5 * PI)");
    System.out.println(d);

    d = eval("sqrt PI");
    System.out.println(d);

    try {
        d = eval("K / 3");
    System.out.println(d);
    } catch (RuntimeException re) {
        System.out.println("--E--");
    }

生产:

2.5
-2.5
25.0
9.42477796076938
7.3890560989306495
15.353981633974483
1.7724538509055159
--E--

后记:

您的某些操作(术语和函数)无法防止无效输入,例如 divide-by-zero 和未定义的函数,例如 log 0.