From 82406c871d9596c51832a2893b413a6c3c2e2c02 Mon Sep 17 00:00:00 2001 From: selim Date: Wed, 27 Dec 2023 03:29:44 +0100 Subject: [PATCH] Spreadsheet changed every function supporting double. --- .../informatik/spreadsheet/Spreadsheet.java | 99 ++++++++++--------- 1 file changed, 50 insertions(+), 49 deletions(-) diff --git a/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java b/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java index 416e81a..256189b 100644 --- a/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java +++ b/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java @@ -149,7 +149,7 @@ public class Spreadsheet { */ private void evaluateCell(int row, int col) { String formula = cells[row][col].getFormula(); - String result = ""; + double result = 0; String substringStartCell1 = ""; String substringStartCell2 = ""; String substringStartCell3 = ""; @@ -166,79 +166,80 @@ public class Spreadsheet { if (formula.startsWith("SUMME("))// e.g. SUMME(A3:A8) if (formula.length() < 13) - result = "" + sum(substringStartCell1, substringEndCell1); + result = sum(substringStartCell1, substringEndCell1); else if (formula.length() < 14) - result = "" + sum(substringStartCell2, substringEndCell2); + result = sum(substringStartCell2, substringEndCell2); else if (formula.length() < 15) - result = "" + sum(substringStartCell3, substringEndCell2); + result = sum(substringStartCell3, substringEndCell2); else - result = "0"; + result = 0; else if (formula.startsWith("PRODUKT(")) // e.g. PRODUKT(A3:B9) if (formula.length() < 15) - result = "" + product(substringStartCell1, substringEndCell1); + result = product(substringStartCell1, substringEndCell1); else if (formula.length() < 16 && !substringEndCell2.contains(":")) - result = "" + product(substringStartCell2, substringEndCell2); + result = product(substringStartCell2, substringEndCell2); else if (formula.length() < 17) - result = "" + product(substringStartCell3, substringEndCell2); + result = product(substringStartCell3, substringEndCell2); else - result = "0"; + result = 0; else if (formula.startsWith("MITTELWERT(")) // e.g. MITTELWERT(A3:A5) if (formula.length() < 18) - result = "" + average(substringStartCell1, substringEndCell1); + result = average(substringStartCell1, substringEndCell1); else if (formula.length() < 19) - result = "" + average(substringStartCell2, substringEndCell2); + result = average(substringStartCell2, substringEndCell2); else if (formula.length() < 20) - result = "" + average(substringStartCell3, substringEndCell2); + result = average(substringStartCell3, substringEndCell2); else - result = "0"; + result = 0; else if (formula.startsWith("STABW(")) // e.g. STABW(C6:D8) -> Standardabweichung if (formula.length() < 13) - result = "" + standardDeviation(substringStartCell1, substringEndCell1); + result = standardDeviation(substringStartCell1, substringEndCell1); else if (formula.length() < 14) - result = "" + standardDeviation(substringStartCell2, substringEndCell2); + result = standardDeviation(substringStartCell2, substringEndCell2); else if (formula.length() < 15) - result = "" + standardDeviation(substringStartCell3, substringEndCell2); + result = standardDeviation(substringStartCell3, substringEndCell2); else - result = "0"; + result = 0; else if (formula.startsWith("MIN(")) // e.g. MIN(C1:H13) -> kleinster Wert if (formula.length() < 11) - result = "" + min(substringStartCell1, substringEndCell1); + result = min(substringStartCell1, substringEndCell1); else if (formula.length() < 12) - result = "" + min(substringStartCell2, substringEndCell2); + result = min(substringStartCell2, substringEndCell2); else if (formula.length() < 13) - result = "" + min(substringStartCell3, substringEndCell2); + result = min(substringStartCell3, substringEndCell2); else - result = "0"; + result = 0; else if (formula.startsWith("MAX(")) // e.g. MAX(A1:A10) -> größter Wert if (formula.length() < 11) - result = "" + max(substringStartCell1, substringEndCell1); + result = max(substringStartCell1, substringEndCell1); else if (formula.length() < 12) - result = "" + max(substringStartCell2, substringEndCell2); + result = max(substringStartCell2, substringEndCell2); else if (formula.length() < 13) - result = "" + max(substringStartCell3, substringEndCell2); + result = max(substringStartCell3, substringEndCell2); else - result = "0"; + result = 0; else if (!formula.isEmpty()) { try { - result = "" + calculate(formula); + result = calculate(formula); } catch (ArithmeticException ae) { - result = "exc."; + result = 0; } } - cells[row][col].setValue(result); + if(!cells[row][col].getFormula().isEmpty()) + cells[row][col].setValue(String.format("%.1f",result)); } /** @@ -248,27 +249,27 @@ public class Spreadsheet { * @param endCellName The name of the cell in the lower right corner of the rectangle. * @return The sum calculated. */ - private long sum(String startCellName, String endCellName) { - long result = 0; + private double sum(String startCellName, String endCellName) { + double result = 0; for(int r = getRow(startCellName)-1; r0) - result *= Long.parseLong(cells[r][c].getValue()); + result *= Double.parseDouble(cells[r][c].getValue()); else { - result = Long.parseLong(cells[r][c].getValue()); + result = Double.parseDouble(cells[r][c].getValue()); counter++; } @@ -341,7 +342,7 @@ public class Spreadsheet { return Math.sqrt(mem); //standardDeviation formula } - private long min(String startCellName, String endCellName){ + private double min(String startCellName, String endCellName){ ArrayList cellNames = new ArrayList<>(); for(int r = getRow(startCellName)-1; r Long.parseLong(get(cellNames.get(i)))) - result = Long.parseLong(get(cellNames.get(i))); + if (result > Double.parseDouble(get(cellNames.get(i)))) + result = Double.parseDouble(get(cellNames.get(i))); } catch(NumberFormatException n){} return result; } - private long max(String startCellName, String endCellName){ + private double max(String startCellName, String endCellName){ ArrayList cellNames = new ArrayList<>(); for(int r = getRow(startCellName)-1; r