import java.util.Scanner; public class Rechner { public static final double Add = 0; public static final double Sub = 1; public static final double Mul = 2; public static final double Div = 3; public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.print("Rechnung: "); String rechnung = scan.nextLine(); int numOperands = 0; for (int i = 0; i < rechnung.length(); i++) { char c = rechnung.charAt(i); if (isOperand(c)) { numOperands++; } if (c == '(' && i > 0 && ((rechnung.charAt(i - 1) >= '0' && rechnung.charAt(i - 1) <= '9') || rechnung.charAt(i - 1) == '.' )) { rechnung = rechnung.substring(0, i) + "*" + rechnung.substring(i); i++; numOperands++; } if (c == ')' && i < rechnung.length() - 1 && ((rechnung.charAt(i + 1) >= '0' && rechnung.charAt(i + 1) <= '9') || rechnung.charAt(i + 1) == '.')) { rechnung = rechnung.substring(0, i + 1) + "*" + rechnung.substring(i + 1); } } double[] priorities = new double[numOperands]; double[] operands = new double[numOperands]; double[] terms = new double[numOperands + 1]; double priority = 0; double highestPriority = 0; int operandIndex = 0; String currentNum = ""; for (int i = 0; i < rechnung.length(); i++) { char c = rechnung.charAt(i); if (c == '(') { priority += 2; continue; } if (c == ')') { priority -= 2; continue; } if ((c >= '0' && c <= '9') || c == '.') { currentNum = currentNum + c; continue; } if (c == '-' && currentNum == "") { currentNum = "-"; continue; } if (isOperand(c)) { switch (c) { case '+': priorities[operandIndex] = priority; if (priority > highestPriority) { highestPriority = priority; } operands[operandIndex] = Rechner.Add; break; case '-': priorities[operandIndex] = priority; if (priority > highestPriority) { highestPriority = priority; } operands[operandIndex] = Rechner.Sub; break; case '*': priorities[operandIndex] = priority + 1; if (priority + 1> highestPriority) { highestPriority = priority + 1; } operands[operandIndex] = Rechner.Mul; break; case '/': priorities[operandIndex] = priority + 1; if (priority + 1> highestPriority) { highestPriority = priority + 1; } operands[operandIndex] = Rechner.Div; break; } terms[operandIndex] = Double.parseDouble(currentNum); currentNum = ""; operandIndex++; } } terms[operandIndex] = Double.parseDouble(currentNum); for (double prio = highestPriority; prio >= 0; prio--) { for (int i = 0; i < operands.length; i++) { if (priorities[i] == prio) { if (operands[i] == Rechner.Add) { terms[i + 1] = terms[i] + terms[i + 1]; } else if (operands[i] == Rechner.Sub) { terms[i + 1] = terms[i] - terms[i + 1]; } else if (operands[i] == Rechner.Mul) { terms[i + 1] = terms[i] * terms[i + 1]; } else if (operands[i] == Rechner.Div) { terms[i + 1] = terms[i] / terms[i + 1]; } terms = removeArrayElement(terms, i); operands = removeArrayElement(operands, i); priorities = removeArrayElement(priorities, i); i--; } } } System.out.println(terms[0]); } public static double[] removeArrayElement(double[] arr, int index) { if (index >= arr.length || index < 0) return arr; double[] ret = new double[arr.length - 1]; for (int i = 0; i < arr.length; i++) { if (i == index) continue; ret[i < index ? i : i -1] = arr[i]; } return ret; } public static boolean isOperand(char c) { return c == '+' || c == '-' || c == '*' || c == '/'; } }