Не судите строго начинающего программиста! Пыталась решить, как смогла. Все тесты проходит, но валидатор не пропускает. Подскажите, в чём может быть причина.
package com.javarush.task.task34.task3404;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
/*
Рекурсия для мат. выражения
*/
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
}
public void recurse(final String expression, int countOperation) {
//implement
String str = expression;
str = str.replaceAll(" ", "");
str = str.toLowerCase();
if (countOperation == 0) {
countOperation = getCountOperation(str, countOperation);
}
str = str.replaceAll("-\\(", "-1*\\(");
str = str.replaceAll("\\(", " \\(");
str = str.replaceAll("\\)", "\\) ");
if (str.matches("^[-]?[0-9]*[.,]?[0-9]+$")) {
if (str.equals("-0")) str = "0";
str = new DecimalFormat("#.##").format(Double.valueOf(str)).replace(",", ".");
System.out.println(str + " " + countOperation);
return;
}
String mat = "";
String[] parse = str.split(" ");
int index = 0;
if (str.contains("(")) {
for (int i = 0; i < parse.length; i++) {
if (!parse[i].isEmpty() && parse[i].charAt(0) == '(' && parse[i].charAt(parse[i].length() - 1) == ')') {
index = i;
}
}
mat = parse[index].substring(1, parse[index].length() - 1);
} else {
mat = parse[index];
}
while (mat.contains("^")) {
mat = getPow(mat);
}
while (mat.contains("sin")) {
mat = getSin(mat);
}
while (mat.contains("cos")) {
mat = getCos(mat);
}
while (mat.contains("tan")) {
mat = getTan(mat);
}
if (mat.contains("*") || mat.contains("/")) {
for (int i = 0; i < mat.length(); i++) {
if (mat.charAt(i) == '*') {
mat = getMultiple(mat);
i = 0;
}
if (mat.charAt(i) == '/') {
mat = getDel(mat);
i = 0;
}
}
}
mat = mat.replaceAll("\\+-", "-");
mat = mat.replaceAll("--", "+");
if (mat.contains("-") || mat.contains("+")) {
for (int i = 0; i < mat.length(); i++) {
if (mat.charAt(i) == '-') {
if (!getLeftOperand(mat, i).equals("-")) {
mat = getMinus(mat, i);
i = 0;
}
}
if (mat.charAt(i) == '+') {
mat = getPlus(mat);
i = 0;
}
}
}
parse[index] = mat;
str = "";
for (String s : parse) {
str += s;
}
recurse(str, countOperation);
}
public String getLeftOperand (String mat, int index) {
String result = "";
while (index >= 0 && (Character.isDigit(mat.charAt(index))
|| mat.charAt(index) == '.'
|| mat.charAt(index) == '-')) {
if (mat.charAt(index) == '-') {
if (index -1 >= 0 && Character.isDigit(mat.charAt(index - 1))) {
return result;
}
if (index -1 < 0 || !Character.isDigit(mat.charAt(index - 1))) {
result = mat.charAt(index) + result;
return result;
}
}
result = mat.charAt(index) + result;
index--;
}
return result;
}
public String getRightOperand (String mat, int index) {
String result = "";
if (mat.charAt(index) == '-') {
result += mat.charAt(index);
index++;
}
while (index < mat.length() && (Character.isDigit(mat.charAt(index))
|| mat.charAt(index) == '.')) {
result += mat.charAt(index);
index++;
}
return result;
}
public String getPow (String mat) {
int index = mat.indexOf("^");
String leftOperand = getLeftOperand(mat, index - 1);
String rightOperand = getRightOperand(mat, index + 1);
mat = mat.substring(0, index) + " ^ " + mat.substring(index +1, mat.length());
String substring = leftOperand + " ^ " + rightOperand;
Double pow = Math.pow(Double.parseDouble(leftOperand), Double.parseDouble(rightOperand));
mat = mat.replace(substring, getRounding(pow));
return mat;
}
public String getSin (String mat) {
int index = mat.indexOf("i");
String rightOperand = getRightOperand(mat, index + 2);
String substring = "sin" + rightOperand;
Double radians = Math.toRadians(Double.parseDouble(rightOperand));
Double sin = Math.sin(radians);
mat = mat.replace(substring, getRounding(sin));
return mat;
}
public String getCos (String mat) {
int index = mat.indexOf("o");
String rightOperand = getRightOperand(mat, index + 2);
String substring = "cos" + rightOperand;
Double radians = Math.toRadians(Double.parseDouble(rightOperand));
Double cos = Math.cos(radians);
mat = mat.replace(substring, getRounding(cos));
return mat;
}
public String getTan (String mat) {
int index = mat.indexOf("a");
String rightOperand = getRightOperand(mat, index + 2);
String substring = "tan" + rightOperand;
Double radians = Math.toRadians(Double.parseDouble(rightOperand));
Double tan = Math.tan(radians);
mat = mat.replace(substring, getRounding(tan));
return mat;
}
public String getMultiple (String mat) {
int index = mat.indexOf("*");
String leftOperand = getLeftOperand(mat, index - 1);
String rightOperand = getRightOperand(mat, index + 1);
mat = mat.substring(0, index) + " * " + mat.substring(index +1, mat.length());
String substring = leftOperand + " * " + rightOperand;
Double multiple = Double.valueOf(leftOperand) * Double.valueOf(rightOperand);
mat = mat.replace(substring, getRounding(multiple));
return mat;
}
public String getDel (String mat) {
int index = mat.indexOf("/");
String leftOperand = getLeftOperand(mat, index - 1);
String rightOperand = getRightOperand(mat, index + 1);
mat = mat.substring(0, index) + " / " + mat.substring(index +1, mat.length());
String substring = leftOperand + " / " + rightOperand;
Double del = Double.valueOf(leftOperand) / Double.valueOf(rightOperand);
mat = mat.replace(substring, getRounding(del));
return mat;
}
public String getPlus (String mat) {
int index = mat.indexOf("+");
String leftOperand = getLeftOperand(mat, index - 1);
String rightOperand = getRightOperand(mat, index + 1);
mat = mat.substring(0, index) + " + " + mat.substring(index +1, mat.length());
String substring = leftOperand + " + " + rightOperand;
Double plus = Double.valueOf(leftOperand) + Double.valueOf(rightOperand);
mat = mat.replace(substring, getRounding(plus));
return mat;
}
public String getMinus (String mat, int index) {
String leftOperand = getLeftOperand(mat, index - 1);
String rightOperand = getRightOperand(mat, index + 1);
mat = mat.substring(0, index) + " - " + mat.substring(index +1, mat.length());
String substring = leftOperand + " - " + rightOperand;
Double minus = Double.valueOf(leftOperand) - Double.valueOf(rightOperand);
mat = mat.replace(substring, getRounding(minus));
return mat;
}
public int getCountOperation (String mat, int countOperation) {
for (int i = 0; i < mat.length(); i++) {
if (mat.charAt(i) == '+' || mat.charAt(i) == '-' ||
mat.charAt(i) == '*' || mat.charAt(i) == '/'
|| mat.charAt(i) == 's' || mat.charAt(i) == 't' ||
mat.charAt(i) == '^') {
countOperation++;
}
}
return countOperation;
}
public String getRounding (Double number) {
return String.valueOf(new BigDecimal(number).setScale(4, RoundingMode.HALF_UP).doubleValue());
}
public Solution() {
//don't delete
}
}