From f5c29329fe17cd9fc4dd29d4cd84b0834441fb48 Mon Sep 17 00:00:00 2001 From: Jan Date: Sun, 7 Jan 2024 12:34:39 +0100 Subject: [PATCH] Min Max --- .../informatik/spreadsheet/Spreadsheet.java | 109 ++++++++++++++++-- 1 file changed, 97 insertions(+), 12 deletions(-) diff --git a/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java b/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java index 1dc5535..ec281eb 100644 --- a/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java +++ b/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java @@ -147,11 +147,11 @@ public class Spreadsheet { }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 - result = "TODO"; // TODO - }else if (formula.startsWith("MAX(")) { // e.g. MAX(A1:A10) -> Standardabweichung - result = "TODO"; // TODO + result += stabw(formula.substring(offset, diff), formula.substring(diff+1, offsetEnd)); + }else if (formula.startsWith("MIN(")) {// e.g. MIN(C13:H13) -> kleinster Wert + result += min(formula.substring(offset, diff), formula.substring(diff+1, offsetEnd)); + }else if (formula.startsWith("MAX(")) { // e.g. MAX(A1:A10) -> größter Wert + result += max(formula.substring(offset, diff), formula.substring(diff+1, offsetEnd)); }else if (!formula.isEmpty()) { try { result = "" + calculate(formula); @@ -171,8 +171,8 @@ public class Spreadsheet { */ private long sum(String startCellName, String endCellName) { long res = 0; - for(int j = getRow(startCellName); j< (getRow(endCellName)+1);j++){ - for(int i = getCol(startCellName); i< (getCol(endCellName)+1);i++) { + for(int j = getRow(startCellName); j<= (getRow(endCellName));j++){ + for(int i = getCol(startCellName); i<= (getCol(endCellName));i++) { if(!cells[j][i].getValue().isBlank()) { res += Long.parseLong(cells[j][i].getValue()); } @@ -189,8 +189,8 @@ public class Spreadsheet { private long prod(String startCellName, String endCellName) { long res = 1; boolean anyNumbers= false; - for(int j = getRow(startCellName); j< (getRow(endCellName)+1);j++){ - for(int i = getCol(startCellName); i< (getCol(endCellName)+1);i++) { + for(int j = getRow(startCellName); j<= (getRow(endCellName));j++){ + for(int i = getCol(startCellName); i<= (getCol(endCellName));i++) { if(!cells[j][i].getValue().isBlank()) { res*= Long.parseLong(cells[j][i].getValue()); anyNumbers = true; @@ -211,8 +211,8 @@ public class Spreadsheet { 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++) { + for(int j = getRow(startCellName); j<= (getRow(endCellName));j++){ + for(int i = getCol(startCellName); i<= (getCol(endCellName));i++) { if(!cells[j][i].getValue().isBlank()) { res+= Long.parseLong(cells[j][i].getValue()); Numbers++; @@ -222,7 +222,92 @@ public class Spreadsheet { res/=Numbers; return res; } - + /** + * Method for calculating the Standard deviation 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 Standard deviation calculated. + */ + private long stabw(String startCellName, String endCellName) { + long res=0; + long midSum=0; + //mid(startCellName,endCellName) + //mid ist nicht verwendbar weil wir die anzahl der Nummern sowieso brauchen + int numbers= 0; + for(int j = getRow(startCellName); j<= (getRow(endCellName));j++){ + for(int i = getCol(startCellName); i<= (getCol(endCellName));i++) { + if(!cells[j][i].getValue().isBlank()) { + midSum+= Long.parseLong(cells[j][i].getValue()); + numbers++; + } + } + } + double midVal = (double) midSum / numbers; + + double sum = 0.0; + for (int j = getRow(startCellName); j <= getRow(endCellName); j++) { + for (int i = getCol(startCellName); i <= getCol(endCellName); i++) { + if (!cells[j][i].getValue().isBlank()) { + long value = Long.parseLong(cells[j][i].getValue()); + sum += Math.pow(value - midVal, 2); + } + } + } + double stabw = Math.sqrt(sum / numbers); + return (long) stabw; + } + /** + * Method for calculating the minimum 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 minimum value calculated. + */ + private long min(String startCellName, String endCellName) { + long res = 0; + List args = new ArrayList<>(); + for(int j = getRow(startCellName); j<= (getRow(endCellName));j++){ + for(int i = getCol(startCellName); i<= (getCol(endCellName));i++) { + if(!cells[j][i].getValue().isBlank()) { + args.add(Long.parseLong(cells[j][i].getValue())); + } + } + } + if(args.size()>0) { + res=args.get(0); + } + for(int i = 0; iargs.get(i)) { + res=args.get(i); + } + } + return res; + } + /** + * Method for calculating the maximum 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 maximum value calculated. + */ + private long max(String startCellName, String endCellName) { + long res = 0; + List args = new ArrayList<>(); + for(int j = getRow(startCellName); j<= (getRow(endCellName));j++){ + for(int i = getCol(startCellName); i<= (getCol(endCellName));i++) { + if(!cells[j][i].getValue().isBlank()) { + args.add(Long.parseLong(cells[j][i].getValue())); + } + } + } + if(args.size()>0) { + res=args.get(0); + } + for(int i = 0; i