fix duplicate digit at end

This commit is contained in:
krolxon 2024-02-15 00:09:10 +05:30
parent 4cea67a0cb
commit d134acdbc1
4 changed files with 22 additions and 5 deletions

View File

@ -1,8 +1,14 @@
public class Calculator { public class Calculator {
public static void main(String[] args) { public static void main(String[] args) {
String expr = "(84 / 4 * 3 - 9) * 2 + 1 / 5"; // 108.2 String expr = "(84 / 4 * 3 - 9) * 2 + 1 / 5"; // 108.2
Parser p = new Parser(expr); Parser p;
if (args.length == 0) {
p = new Parser(expr);
} else {
p = new Parser(args[0]);
}
String postfix = p.toPostFix(); String postfix = p.toPostFix();
// System.out.println("pfix => \t " + postfix);
System.out.println(p.evalExpr(postfix)); System.out.println(p.evalExpr(postfix));
} }
} }

View File

@ -1,4 +1,6 @@
/* Todo /* Todo
* - Fix this:
* when expression = 5+10*(10), it misses +
* - Add support for trigonometric functions * - Add support for trigonometric functions
* - Remove all edge cases * - Remove all edge cases
*/ */
@ -70,6 +72,9 @@ public class Parser {
// Main Expression Iteration // Main Expression Iteration
for (int i = 0; i < infix.length; i++) { for (int i = 0; i < infix.length; i++) {
char c = infix[i]; char c = infix[i];
// Keep appending digits to the operand StringBuilder and skip iterations till
// we find a operator.
if (isOperand(c)) { if (isOperand(c)) {
operand.append(infix[i]); operand.append(infix[i]);
if (i < infix.length - 1) { if (i < infix.length - 1) {
@ -90,14 +95,22 @@ public class Parser {
} }
operatorStack.pop(); operatorStack.pop();
} else { } else {
// Manage precedence order of operatorStack operators
while (!operatorStack.isEmpty() && getPresedence(c) <= getPresedence((char) operatorStack.peek()) while (!operatorStack.isEmpty() && getPresedence(c) <= getPresedence((char) operatorStack.peek())
&& hasLeftAssociativity(c)) { && hasLeftAssociativity(c)) {
output.add(operatorStack.pop().toString()); output.add(operatorStack.pop().toString());
} }
operatorStack.push(c);
// For whatever reason, the last digit gets added to the operatorStack, leading
// to a duplicate of last digit in the expression
// To fix this, we again make sure that the current char is not a operand.
if (!isOperand(c)) {
operatorStack.push(c);
}
} }
} }
// pop all the operators left in the operatorStack after the loop is done.
while (!operatorStack.isEmpty()) { while (!operatorStack.isEmpty()) {
if (operatorStack.peek() == '(') { if (operatorStack.peek() == '(') {
return "This expression is invalid"; return "This expression is invalid";
@ -105,9 +118,7 @@ public class Parser {
output.add(operatorStack.pop().toString()); output.add(operatorStack.pop().toString());
} }
// last digit gets duplicate entry for some reason, so have to remove it. return output.toString();
String op = output.toString();
return op.substring(0, op.length() -2);
} }
private Double evaluate(String operator, Double op1, Double op2) { private Double evaluate(String operator, Double op1, Double op2) {

BIN
bin/Calculator.class Normal file

Binary file not shown.

BIN
bin/Parser.class Normal file

Binary file not shown.