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