jcald134acd
fix duplicate digit at end
+22 −5
4 files changed
- M Calculator.java +7 −1
- M Parser.java +15 −4
- A bin/Calculator.class
- A bin/Parser.class
| @@ -1,8 +1,14 @@ | |||
| 1 | 1 | public class Calculator { | |
| 2 | 2 | public static void main(String[] args) { | |
| 3 | 3 | String expr = "(84 / 4 * 3 - 9) * 2 + 1 / 5"; // 108.2 | |
| 4 | − | Parser p = new Parser(expr); | |
| 4 | + | Parser p; | |
| 5 | + | if (args.length == 0) { | |
| 6 | + | p = new Parser(expr); | |
| 7 | + | } else { | |
| 8 | + | p = new Parser(args[0]); | |
| 9 | + | } | |
| 5 | 10 | String postfix = p.toPostFix(); | |
| 11 | + | // System.out.println("pfix => \t " + postfix); | |
| 6 | 12 | System.out.println(p.evalExpr(postfix)); | |
| 7 | 13 | } | |
| 8 | 14 | } | |
| @@ -1,4 +1,6 @@ | |||
| 1 | 1 | /* Todo | |
| 2 | + | * - Fix this: | |
| 3 | + | * when expression = 5+10*(10), it misses + | |
| 2 | 4 | * - Add support for trigonometric functions | |
| 3 | 5 | * - Remove all edge cases | |
| 4 | 6 | */ | |
| @@ -70,6 +72,9 @@ public class Parser { | |||
| 70 | 72 | // Main Expression Iteration | |
| 71 | 73 | for (int i = 0; i < infix.length; i++) { | |
| 72 | 74 | char c = infix[i]; | |
| 75 | + | ||
| 76 | + | // Keep appending digits to the operand StringBuilder and skip iterations till | |
| 77 | + | // we find a operator. | |
| 73 | 78 | if (isOperand(c)) { | |
| 74 | 79 | operand.append(infix[i]); | |
| 75 | 80 | if (i < infix.length - 1) { | |
| @@ -90,14 +95,22 @@ public class Parser { | |||
| 90 | 95 | } | |
| 91 | 96 | operatorStack.pop(); | |
| 92 | 97 | } else { | |
| 98 | + | // Manage precedence order of operatorStack operators | |
| 93 | 99 | while (!operatorStack.isEmpty() && getPresedence(c) <= getPresedence((char) operatorStack.peek()) | |
| 94 | 100 | && hasLeftAssociativity(c)) { | |
| 95 | 101 | output.add(operatorStack.pop().toString()); | |
| 96 | 102 | } | |
| 97 | − | operatorStack.push(c); | |
| 103 | + | ||
| 104 | + | // For whatever reason, the last digit gets added to the operatorStack, leading | |
| 105 | + | // to a duplicate of last digit in the expression | |
| 106 | + | // To fix this, we again make sure that the current char is not a operand. | |
| 107 | + | if (!isOperand(c)) { | |
| 108 | + | operatorStack.push(c); | |
| 109 | + | } | |
| 98 | 110 | } | |
| 99 | 111 | } | |
| 100 | 112 | ||
| 113 | + | // pop all the operators left in the operatorStack after the loop is done. | |
| 101 | 114 | while (!operatorStack.isEmpty()) { | |
| 102 | 115 | if (operatorStack.peek() == '(') { | |
| 103 | 116 | return "This expression is invalid"; | |
| @@ -105,9 +118,7 @@ public class Parser { | |||
| 105 | 118 | output.add(operatorStack.pop().toString()); | |
| 106 | 119 | } | |
| 107 | 120 | ||
| 108 | − | // last digit gets duplicate entry for some reason, so have to remove it. | |
| 109 | − | String op = output.toString(); | |
| 110 | − | return op.substring(0, op.length() -2); | |
| 121 | + | return output.toString(); | |
| 111 | 122 | } | |
| 112 | 123 | ||
| 113 | 124 | private Double evaluate(String operator, Double op1, Double op2) { | |
[Binary file]
[Binary file]