From baa6d8a0924f0d20a3c66baff4fcc21820dce667 Mon Sep 17 00:00:00 2001 From: Jan Date: Sat, 6 Jan 2024 22:59:39 +0100 Subject: [PATCH] Mittelwert done (Codewort MID) --- .../informatik/spreadsheet/Spreadsheet.java | 32 +++++++++++++++---- 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java b/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java index 2649af0..1dc5535 100644 --- a/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java +++ b/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java @@ -70,13 +70,13 @@ public class Spreadsheet { return cellName.charAt(0) - 'A'; } private int getRow(String cellName) { - if (cellName.length()==2) { - return cellName.charAt(1) - '1'; - }else { + if (cellName.length()==3) { int Row = 0; Row +=((cellName.charAt(1)-'0')*10); Row +=(cellName.charAt(2)-'0'); return Row-1; + }else { + return cellName.charAt(1) - '1'; } } // ----- @@ -143,9 +143,9 @@ public class Spreadsheet { if (formula.startsWith("SUMME(")) { // e.g. SUMME(A3:A8) result += sum(formula.substring(offset, diff), formula.substring(diff+1,offsetEnd)); }else if (formula.startsWith("PRODUKT(")) { // e.g. PRODUKT(A3:B9) - result += prod(formula.substring(offset, diff), formula.substring(diff+1, offsetEnd));; // TODO - }else if (formula.startsWith("MITTELWERT(")) { // e.g. MITTELWERT(A3:A5) - result = "TODO"; // TODO + result += prod(formula.substring(offset, diff), formula.substring(diff+1, offsetEnd)); + }else if (formula.startsWith("MID(")) { // e.g. MITTELWERT(A3:A5) + result += mid(formula.substring(offset, diff), formula.substring(diff+1, offsetEnd)); }else if (formula.startsWith("STABW(")) { // e.g. STABW(C6:D8) -> Standardabweichung result = "TODO"; // TODO }else if (formula.startsWith("MIN(")) {// e.g. MIN(C13:H13) -> größter Wert @@ -202,6 +202,26 @@ public class Spreadsheet { else return 0; } + /** + * Method for calculating the Mid Value of a rectangular block of cells, such as from A1 to B3. + * @param startCellName The name of the cell in the upper left corner of the rectangle. + * @param endCellName The name of the cell in the lower right corner of the rectangle. + * @return The Mid Value calculated. + */ + private long mid(String startCellName, String endCellName) { + long res = 0; + int Numbers= 0; + for(int j = getRow(startCellName); j< (getRow(endCellName)+1);j++){ + for(int i = getCol(startCellName); i< (getCol(endCellName)+1);i++) { + if(!cells[j][i].getValue().isBlank()) { + res+= Long.parseLong(cells[j][i].getValue()); + Numbers++; + } + } + } + res/=Numbers; + return res; + } /** * This method calculates the result of a "normal" algebraic expression. It only needs to support