|
| 1 | +package world.bentobox.level.calculators; |
| 2 | + |
| 3 | +import java.text.ParseException; |
| 4 | + |
| 5 | +/** |
| 6 | + * @author tastybento |
| 7 | + */ |
| 8 | +public class EquationEvaluator { |
| 9 | + |
| 10 | + private static class Parser { |
| 11 | + private final String input; |
| 12 | + private int pos = -1; |
| 13 | + private int currentChar; |
| 14 | + |
| 15 | + public Parser(String input) { |
| 16 | + this.input = input; |
| 17 | + moveToNextChar(); |
| 18 | + } |
| 19 | + |
| 20 | + private void moveToNextChar() { |
| 21 | + currentChar = (++pos < input.length()) ? input.charAt(pos) : -1; |
| 22 | + } |
| 23 | + |
| 24 | + private boolean tryToEat(int charToEat) { |
| 25 | + while (currentChar == ' ') |
| 26 | + moveToNextChar(); |
| 27 | + if (currentChar == charToEat) { |
| 28 | + moveToNextChar(); |
| 29 | + return true; |
| 30 | + } |
| 31 | + return false; |
| 32 | + } |
| 33 | + |
| 34 | + public double evaluate() throws ParseException { |
| 35 | + double result = parseExpression(); |
| 36 | + if (pos < input.length()) { |
| 37 | + throw new ParseException("Unexpected character: " + (char) currentChar, pos); |
| 38 | + } |
| 39 | + return result; |
| 40 | + } |
| 41 | + |
| 42 | + private double parseExpression() throws ParseException { |
| 43 | + double result = parseTerm(); |
| 44 | + while (true) { |
| 45 | + if (tryToEat('+')) |
| 46 | + result += parseTerm(); |
| 47 | + else if (tryToEat('-')) |
| 48 | + result -= parseTerm(); |
| 49 | + else |
| 50 | + return result; |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + private double parseFactor() throws ParseException { |
| 55 | + if (tryToEat('+')) |
| 56 | + return parseFactor(); // unary plus |
| 57 | + if (tryToEat('-')) |
| 58 | + return -parseFactor(); // unary minus |
| 59 | + |
| 60 | + double x; |
| 61 | + int startPos = this.pos; |
| 62 | + if (tryToEat('(')) { // parentheses |
| 63 | + x = parseExpression(); |
| 64 | + tryToEat(')'); |
| 65 | + } else if ((currentChar >= '0' && currentChar <= '9') || currentChar == '.') { // numbers |
| 66 | + while ((currentChar >= '0' && currentChar <= '9') || currentChar == '.') |
| 67 | + moveToNextChar(); |
| 68 | + x = Double.parseDouble(input.substring(startPos, this.pos)); |
| 69 | + } else if (currentChar >= 'a' && currentChar <= 'z') { // functions |
| 70 | + while (currentChar >= 'a' && currentChar <= 'z') |
| 71 | + moveToNextChar(); |
| 72 | + String func = input.substring(startPos, this.pos); |
| 73 | + x = parseFactor(); |
| 74 | + switch (func) { |
| 75 | + case "sqrt": |
| 76 | + x = Math.sqrt(x); |
| 77 | + break; |
| 78 | + case "sin": |
| 79 | + x = Math.sin(Math.toRadians(x)); |
| 80 | + break; |
| 81 | + case "cos": |
| 82 | + x = Math.cos(Math.toRadians(x)); |
| 83 | + break; |
| 84 | + case "tan": |
| 85 | + x = Math.tan(Math.toRadians(x)); |
| 86 | + break; |
| 87 | + case "log": |
| 88 | + x = Math.log(x); |
| 89 | + break; |
| 90 | + default: |
| 91 | + throw new ParseException("Unknown function: " + func, startPos); |
| 92 | + } |
| 93 | + } else { |
| 94 | + throw new ParseException("Unexpected: " + (char) currentChar, startPos); |
| 95 | + } |
| 96 | + |
| 97 | + if (tryToEat('^')) |
| 98 | + x = Math.pow(x, parseFactor()); // exponentiation |
| 99 | + |
| 100 | + return x; |
| 101 | + } |
| 102 | + |
| 103 | + private double parseTerm() throws ParseException { |
| 104 | + double x = parseFactor(); |
| 105 | + for (;;) { |
| 106 | + if (tryToEat('*')) |
| 107 | + x *= parseFactor(); // multiplication |
| 108 | + else if (tryToEat('/')) |
| 109 | + x /= parseFactor(); // division |
| 110 | + else |
| 111 | + return x; |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + } |
| 116 | + |
| 117 | + public static double eval(final String equation) throws ParseException { |
| 118 | + return new Parser(equation).evaluate(); |
| 119 | + } |
| 120 | + |
| 121 | +} |
0 commit comments