From d134acdbc12a2d16dadc129f233daeec2b1c9fb8 Mon Sep 17 00:00:00 2001 From: krolxon Date: Thu, 15 Feb 2024 00:09:10 +0530 Subject: [PATCH] fix duplicate digit at end --- Calculator.java | 8 +++++++- Parser.java | 19 +++++++++++++++---- bin/Calculator.class | Bin 0 -> 678 bytes bin/Parser.class | Bin 0 -> 3077 bytes 4 files changed, 22 insertions(+), 5 deletions(-) create mode 100644 bin/Calculator.class create mode 100644 bin/Parser.class diff --git a/Calculator.java b/Calculator.java index 031fa57..43804c3 100644 --- a/Calculator.java +++ b/Calculator.java @@ -1,8 +1,14 @@ public class Calculator { public static void main(String[] args) { 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(); + // System.out.println("pfix => \t " + postfix); System.out.println(p.evalExpr(postfix)); } } diff --git a/Parser.java b/Parser.java index c588052..de41371 100644 --- a/Parser.java +++ b/Parser.java @@ -1,4 +1,6 @@ /* Todo +* - Fix this: +* when expression = 5+10*(10), it misses + * - Add support for trigonometric functions * - Remove all edge cases */ @@ -70,6 +72,9 @@ public class Parser { // Main Expression Iteration for (int i = 0; i < infix.length; i++) { char c = infix[i]; + + // Keep appending digits to the operand StringBuilder and skip iterations till + // we find a operator. if (isOperand(c)) { operand.append(infix[i]); if (i < infix.length - 1) { @@ -90,14 +95,22 @@ public class Parser { } operatorStack.pop(); } else { + // Manage precedence order of operatorStack operators while (!operatorStack.isEmpty() && getPresedence(c) <= getPresedence((char) operatorStack.peek()) && hasLeftAssociativity(c)) { 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()) { if (operatorStack.peek() == '(') { return "This expression is invalid"; @@ -105,9 +118,7 @@ public class Parser { output.add(operatorStack.pop().toString()); } - // last digit gets duplicate entry for some reason, so have to remove it. - String op = output.toString(); - return op.substring(0, op.length() -2); + return output.toString(); } private Double evaluate(String operator, Double op1, Double op2) { diff --git a/bin/Calculator.class b/bin/Calculator.class new file mode 100644 index 0000000000000000000000000000000000000000..2b34176a17f8031429c7dba62dfb1bff8fa5c5b9 GIT binary patch literal 678 zcmZ`$+iuf95IvJP-PkxuX_Ip6#pP-{35HT6P!SSp=~G%F1w}$UZGu&~ICkW0s`4xR z0MGD{N+9(E_@)XmYaXho!Yj?3-JLn-tmpTipXUJHplzXmqJUwegayLlu{@DMER&<) z-QYMJD#F66D2dc-Lecg5Wt0i4?u%_FaJC)adFHg8mmY&Hr|CT9!{-7P_jF}82{Q{O zY|UIDG~CW5cTZ(eaa8l^#(WfY5ra8FrT>qJR-I2{eb?7bYsd}54b#BD3?$E&aO z`h-eP$>HY%InL_^{Q1}Be~-0#=`j)sIP;=avGul%|da8YB;<%Y~6oOq=+H!U)_@c4VFhZ!*5Z18*`l9V4 zj%}r<6#RV7D%3?|{UxlTG7&2sH{2tXdtbQKt~L&1}j@n%$|CLT7a8(WR1Q z@GDV|H5%6HXuvvJK5W=I)22=4$t)kWOF_hj>vU|uMs80|8+JE6_DB0-Jz-)s>ez%E z6e_LUP}a1K^rR?zVq+RME39<=%R5%8eZ(=6GhsBrCnnsaBZ`Oy+S?l=S_qHmcodIO zlFOzn7UNCjY4Q3UMg88Hq8HsHI+7;yiH&ax6~fG%0Nog-Oc)3*5FaHp(is* zq?P?_AcOhY3DZ7kxS6YRThM@!_1-I;%y1^>?6Xd4c#}POyvOr#FX^G-9hTNIF=n40 z$>i;%xzBRxQ0%W;B&7^|JwGMjsF3dKuGj^k@S?kZiMeG~@iOOEDA@tOyT2}e5h~tN zfL2>oyRv{)-yrf8^$M?Yu7;2EO75-Uhhh~fr6GF=$N1!D1YPSs=M3D+vDJIt!sq`E z|9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