From c7af9a2a5e0d8fe85d2801a9084ff1e09bb7fdce Mon Sep 17 00:00:00 2001 From: devra Date: Mon, 8 Jan 2024 22:33:18 +0100 Subject: [PATCH] Maximal-Berechnung Implementiert --- .../informatik/spreadsheet/Spreadsheet.java | 35 ++++++++++++++++--- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java b/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java index 5a1d555..6abb43a 100644 --- a/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java +++ b/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java @@ -174,7 +174,7 @@ public class Spreadsheet { - else if (formula.startsWith("STABW(")) { // e.g. STABW(C6:D8) -> Standardabweichung + else if (formula.startsWith("STABW(")) { // e.g. STABW(C6:D8) -> minimal-wert String[] onlyCells = formula.substring(6, formula.length() - 1).split(":"); if(checkIfCellExists(onlyCells[0])&&checkIfCellExists(onlyCells[1])) { @@ -194,8 +194,13 @@ public class Spreadsheet { - else if (formula.startsWith("MAX(")) // e.g. MAX(A1:A10) -> Standardabweichung - result = "TODO"; // TODO + else if (formula.startsWith("MAX(")) {// e.g. MAX(A1:A10) -> maximal-wert + String[] onlyCells = formula.substring(4, formula.length() - 1).split(":"); + if(checkIfCellExists(onlyCells[0])&&checkIfCellExists(onlyCells[1])) { + result = "" + max(onlyCells[0], onlyCells[1]); + } + } + else if (!formula.isEmpty()) { try { result = "" + calculate(formula); //calculates the result of a "normal" algebraic expression. @@ -213,6 +218,8 @@ public class Spreadsheet { + + /** * Method for calculating the sum 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. @@ -294,11 +301,29 @@ public class Spreadsheet { minValue = werte[i]; } } - return minValue; - } + + + private long max(String startCellName, String endCellName) { + int werte[]=getValues(startCellName,endCellName); + + if (werte.length == 0) { + return 0; // Handle an empty array as needed + } + + int maxValue = werte[0]; + + for (int i = 1; i < werte.length; i++) { + if (werte[i] > maxValue) { + maxValue = werte[i]; + } + } + return maxValue; + } + + // TODO: Javadoc comments private int[] getValues(String startCellName, String endCellName) { int startZeile=getRow(startCellName);