Does anyone know why this will not pass? I've looked at others' code and eventually implemented some code I found online for the parsing which seems to be accurate for the cases that I've tried.
package com.codegym.task.task34.task3404;
/*
Recursion for mathematical expressions
*/
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.Locale;
public class Solution {
public static void main(String[] args) {
Solution solution = new Solution();
solution.recurse("sin(2*(-5+1.5*4)+28)", 0); // Expected output: 0.5 6
solution.recurse("tan(2025 ^ 0.5)", 0); // Expected output: 1 2
solution.recurse("1+2.41+1", 0); // Expected 4.41 2
solution.recurse("2^2", 0); // Expected 4 1
solution.recurse(" 3 + 4 * 2 / ( 1 - 5 ) ^ 2 ^ 3", 0); //Expected 3 7
}
public void recurse(final String expression, int countOperation) {
// Implement
if (countOperation > 0) {
System.out.println(expression + " " + countOperation);
return;
}
DecimalFormat df = new DecimalFormat("#.##", new DecimalFormatSymbols(Locale.US));
df.setRoundingMode(RoundingMode.HALF_EVEN);
String result = df.format(eval(expression));
countOperation = findOperationCount(expression);
recurse(String.valueOf(result), countOperation);
}
public int findOperationCount(String expression) {
int countOperation = 0;
expression = expression.replace(" ", "").trim();
for (int i = 0; i < expression.length(); i++) {
if (expression.charAt(i) == '-') {
try {
if (expression.charAt(i - 1) != '(' && expression.charAt(i - 1) != ')' &&
expression.charAt(i - 1) != '^' && expression.charAt(i - 1) != '*' &&
expression.charAt(i - 1) != '/' && expression.charAt(i - 1) != '+' &&
expression.charAt(i - 1) != '-') {
countOperation++;
}
} catch (Exception ignored) {
}
}
switch (expression.charAt(i)) {
case '(':
countOperation++;
break;
case '^':
countOperation++;
break;
case '*':
countOperation++;
break;
case '/':
countOperation++;
break;
case '+':
countOperation++;
break;
default:
break;
}
}
return countOperation;
}
public static double eval(final String str) {
return new Object() {
int pos = -1, ch;
void nextChar() {
ch = (++pos < str.length()) ? str.charAt(pos) : -1;
}
boolean eat(int charToEat) {
while (ch == ' ') nextChar();
if (ch == charToEat) {
nextChar();
return true;
}
return false;
}
double parse() {
nextChar();
double x = parseExpression();
if (pos < str.length()) throw new RuntimeException("Unexpected: " + (char) ch);
return x;
}
// Grammar:
// expression = term | expression `+` term | expression `-` term
// term = factor | term `*` factor | term `/` factor
// factor = `+` factor | `-` factor | `(` expression `)` | number
// | functionName `(` expression `)` | functionName factor
// | factor `^` factor
double parseExpression() {
double x = parseTerm();
for (; ; ) {
if (eat('+')) x += parseTerm(); // addition
else if (eat('-')) x -= parseTerm(); // subtraction
else return x;
}
}
double parseTerm() {
double x = parseFactor();
for (; ; ) {
if (eat('*')) x *= parseFactor(); // multiplication
else if (eat('/')) x /= parseFactor(); // division
else return x;
}
}
double parseFactor() {
if (eat('+')) return +parseFactor(); // unary plus
if (eat('-')) return -parseFactor(); // unary minus
double x;
int startPos = this.pos;
if (eat('(')) { // parentheses
x = parseExpression();
if (!eat(')')) throw new RuntimeException("Missing ')'");
} else if ((ch >= '0' && ch <= '9') || ch == '.') { // numbers
while ((ch >= '0' && ch <= '9') || ch == '.') nextChar();
x = Double.parseDouble(str.substring(startPos, this.pos));
} else if (ch >= 'a' && ch <= 'z') { // functions
while (ch >= 'a' && ch <= 'z') nextChar();
String func = str.substring(startPos, this.pos);
if (eat('(')) {
x = parseExpression();
if (!eat(')')) throw new RuntimeException("Missing ')' after argument to " + func);
} else {
x = parseFactor();
}
if (func.equals("sqrt")) x = Math.sqrt(x);
else if (func.equals("sin")) x = Math.sin(Math.toRadians(x));
else if (func.equals("cos")) x = Math.cos(Math.toRadians(x));
else if (func.equals("tan")) x = Math.tan(Math.toRadians(x));
else throw new RuntimeException("Unknown function: " + func);
} else {
throw new RuntimeException("Unexpected: " + (char) ch);
}
if (eat('^')) x = Math.pow(x, parseFactor()); // exponentiation
return x;
}
}.parse();
}
public Solution() {
// Don't delete
}
}