aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkrolxon <krolyxon@tutanota.com>2024-02-15 00:09:10 +0530
committerkrolxon <krolyxon@tutanota.com>2024-02-15 00:09:10 +0530
commitd134acdbc12a2d16dadc129f233daeec2b1c9fb8 (patch)
treeaef6fe3c0e17be3fcc6e4462cc8ce96bba44bb06
parent4cea67a0cb77359b83a9d6e40b060fefc2591ce5 (diff)
fix duplicate digit at end
-rw-r--r--Calculator.java8
-rw-r--r--Parser.java19
-rw-r--r--bin/Calculator.classbin0 -> 678 bytes
-rw-r--r--bin/Parser.classbin0 -> 3077 bytes
4 files changed, 22 insertions, 5 deletions
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 0000000..2b34176
--- /dev/null
+++ b/bin/Calculator.class
Binary files differ
diff --git a/bin/Parser.class b/bin/Parser.class
new file mode 100644
index 0000000..5332f06
--- /dev/null
+++ b/bin/Parser.class
Binary files differ