From 5fbf2658d49fc98f7c5f4f72f162f6e7312ddfbb Mon Sep 17 00:00:00 2001 From: selim Date: Tue, 26 Dec 2023 17:40:50 +0100 Subject: [PATCH] Spreadsheet calculate/evaluateCell method fix. --- .../informatik/spreadsheet/Axel.java | 1 - .../informatik/spreadsheet/Spreadsheet.java | 152 +++++++++--------- 2 files changed, 79 insertions(+), 74 deletions(-) diff --git a/Axel/src/de/hs_mannheim/informatik/spreadsheet/Axel.java b/Axel/src/de/hs_mannheim/informatik/spreadsheet/Axel.java index bdb2500..fb7971b 100644 --- a/Axel/src/de/hs_mannheim/informatik/spreadsheet/Axel.java +++ b/Axel/src/de/hs_mannheim/informatik/spreadsheet/Axel.java @@ -22,7 +22,6 @@ public class Axel { System.out.println(spr.toStringShowFormula()); System.out.println(spr); - System.out.println(spr.cells[0][5]); // TODO: You might want to put "UI loop" for entering value and formulas here resp. in some UI methods. diff --git a/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java b/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java index 83aa9d3..3149129 100644 --- a/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java +++ b/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java @@ -150,80 +150,86 @@ public class Spreadsheet { private void evaluateCell(int row, int col) { String formula = cells[row][col].getFormula(); String result = ""; + String substringStartCell1 = ""; + String substringStartCell2 = ""; + String substringStartCell3 = ""; + String substringEndCell1 = ""; + String substringEndCell2 = ""; if(formula.length()>10) { - String substringStartCell1 = formula.substring(formula.length() - 6, formula.length() - 4); - String substringStartCell2 = formula.substring(formula.length() - 7, formula.length() - 5); - String substringStartCell3 = formula.substring(formula.length() - 8, formula.length() - 5); - String substringEndCell1 = formula.substring(formula.length() - 3, formula.length() - 1); - String substringEndCell2 = formula.substring(formula.length() - 4, formula.length() - 1); - - if (formula.startsWith("SUMME("))// e.g. SUMME(A3:A8) - if (formula.length() < 13) - result = "" + sum(substringStartCell1, substringEndCell1); - else if (formula.length() < 14) - result = "" + sum(substringStartCell2, substringEndCell2); - else if (formula.length() < 15) - result = "" + sum(substringStartCell3, substringEndCell2); - else - result = "0"; - - - else if (formula.startsWith("PRODUKT(")) // e.g. PRODUKT(A3:B9) - if (formula.length() < 15) - result = "" + product(substringStartCell1, substringEndCell1); - else if (formula.length() < 16 && !substringEndCell2.contains(":")) - result = "" + product(substringStartCell2, substringEndCell2); - else if (formula.length() < 17) - result = "" + product(substringStartCell3, substringEndCell2); - else - result = "0"; - - - else if (formula.startsWith("MITTELWERT(")) // e.g. MITTELWERT(A3:A5) - if (formula.length() < 17) - result = "" + average(substringStartCell1, substringEndCell1); - else if (formula.length() < 18) - result = "" + average(substringStartCell2, substringEndCell2); - else if (formula.length() < 19) - result = "" + average(substringStartCell3, substringEndCell2); - else - result = "0"; - - - else if (formula.startsWith("STABW(")) // e.g. STABW(C6:D8) -> Standardabweichung - if (formula.length() < 13) - result = "" + standardDeviation(substringStartCell1, substringEndCell1); - else if (formula.length() < 14) - result = "" + standardDeviation(substringStartCell2, substringEndCell2); - else if (formula.length() < 15) - result = "" + standardDeviation(substringStartCell3, substringEndCell2); - else - result = "0"; - - - else if (formula.startsWith("MIN(")) // e.g. MIN(C1:H13) -> kleinster Wert - if (formula.length() < 11) - result = "" + min(substringStartCell1, substringEndCell1); - else if (formula.length() < 12) - result = "" + min(substringStartCell2, substringEndCell2); - else if (formula.length() < 13) - result = "" + min(substringStartCell3, substringEndCell2); - else - result = "0"; - - - else if (formula.startsWith("MAX(")) // e.g. MAX(A1:A10) -> größter Wert - if (formula.length() < 11) - result = "" + max(substringStartCell1, substringEndCell1); - else if (formula.length() < 12) - result = "" + max(substringStartCell2, substringEndCell2); - else if (formula.length() < 13) - result = "" + max(substringStartCell3, substringEndCell2); - else - result = "0"; + substringStartCell1 = formula.substring(formula.length() - 6, formula.length() - 4); + substringStartCell2 = formula.substring(formula.length() - 7, formula.length() - 5); + substringStartCell3 = formula.substring(formula.length() - 8, formula.length() - 5); + substringEndCell1 = formula.substring(formula.length() - 3, formula.length() - 1); + substringEndCell2 = formula.substring(formula.length() - 4, formula.length() - 1); } + if (formula.startsWith("SUMME("))// e.g. SUMME(A3:A8) + if (formula.length() < 13) + result = "" + sum(substringStartCell1, substringEndCell1); + else if (formula.length() < 14) + result = "" + sum(substringStartCell2, substringEndCell2); + else if (formula.length() < 15) + result = "" + sum(substringStartCell3, substringEndCell2); + else + result = "0"; + + + else if (formula.startsWith("PRODUKT(")) // e.g. PRODUKT(A3:B9) + if (formula.length() < 15) + result = "" + product(substringStartCell1, substringEndCell1); + else if (formula.length() < 16 && !substringEndCell2.contains(":")) + result = "" + product(substringStartCell2, substringEndCell2); + else if (formula.length() < 17) + result = "" + product(substringStartCell3, substringEndCell2); + else + result = "0"; + + + else if (formula.startsWith("MITTELWERT(")) // e.g. MITTELWERT(A3:A5) + if (formula.length() < 17) + result = "" + average(substringStartCell1, substringEndCell1); + else if (formula.length() < 18) + result = "" + average(substringStartCell2, substringEndCell2); + else if (formula.length() < 19) + result = "" + average(substringStartCell3, substringEndCell2); + else + result = "0"; + + + else if (formula.startsWith("STABW(")) // e.g. STABW(C6:D8) -> Standardabweichung + if (formula.length() < 13) + result = "" + standardDeviation(substringStartCell1, substringEndCell1); + else if (formula.length() < 14) + result = "" + standardDeviation(substringStartCell2, substringEndCell2); + else if (formula.length() < 15) + result = "" + standardDeviation(substringStartCell3, substringEndCell2); + else + result = "0"; + + + else if (formula.startsWith("MIN(")) // e.g. MIN(C1:H13) -> kleinster Wert + if (formula.length() < 11) + result = "" + min(substringStartCell1, substringEndCell1); + else if (formula.length() < 12) + result = "" + min(substringStartCell2, substringEndCell2); + else if (formula.length() < 13) + result = "" + min(substringStartCell3, substringEndCell2); + else + result = "0"; + + + else if (formula.startsWith("MAX(")) // e.g. MAX(A1:A10) -> größter Wert + if (formula.length() < 11) + result = "" + max(substringStartCell1, substringEndCell1); + else if (formula.length() < 12) + result = "" + max(substringStartCell2, substringEndCell2); + else if (formula.length() < 13) + result = "" + max(substringStartCell3, substringEndCell2); + else + result = "0"; + + else if (!formula.isEmpty()) { try { result = "" + calculate(formula); @@ -380,7 +386,7 @@ public class Spreadsheet { * @return The result calculated. */ private long calculate(String formula) throws ArithmeticException { - Matcher m = Pattern.compile("([A-Z][0-9]*)|[-+*/]|[0-9]+").matcher(formula); + Matcher m = Pattern.compile("([A-Z][1-9][0-9]*)|[-+*/]|[0-9]+").matcher(formula); long result = 0; long currentOperand = 0; @@ -392,9 +398,9 @@ public class Spreadsheet { if (s.matches(("[0-9]+"))) currentOperand = Long.parseLong(s); - else if ((s.matches("[A-Z][1-9]*") || s.matches("[A-Z][1-9]*+")) && get(s).isEmpty()) + else if (s.matches("[A-Z][1-9][0-9]*") && get(s).isEmpty()) currentOperand = 0; - else if ((s.matches("[A-Z][1-9]*") || s.matches("[A-Z][1-9]*+"))) { + else if (s.matches("[A-Z][1-9][0-9]*")) { currentOperand = Long.parseLong(get(s)); } else { if (!firstOperator) {