From d8a057e995fe22040df29bfbdb4dd30f43e09b8a Mon Sep 17 00:00:00 2001 From: 3012330 <3012330@stud.hs-mannheim.de> Date: Sat, 6 Jan 2024 03:31:15 +0100 Subject: [PATCH] SUMME(, PRODUKT(, MAX(, MIN( added --- .../informatik/spreadsheet/Spreadsheet.java | 87 +++++++++++++++++-- 1 file changed, 81 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 c838f84..6196db2 100644 --- a/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java +++ b/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java @@ -187,15 +187,15 @@ public class Spreadsheet { if (formula.startsWith("SUMME(")) // e.g. SUMME(A3:A8) result = "" + sum(formula.substring(6, 8), formula.substring(9, 11)); // TODO adapt to cells with two digits else if (formula.startsWith("PRODUKT(")) // e.g. PRODUKT(A3:B9) - result = "TODO"; // TODO + result = "" + pro(formula.substring(8, 10), formula.substring(11, 13)); else if (formula.startsWith("MITTELWERT(")) // e.g. MITTELWERT(A3:A5) result = "TODO"; // TODO 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 + else if (formula.startsWith("MIN(")) // e.g. MIN(C13:H13) -> kleinster Wert + result = "" + min(formula.substring(4, 6), formula.substring(7, 9)); + else if (formula.startsWith("MAX(")) // e.g. MAX(A1:A10) -> größter Wert + result = "" + max(formula.substring(4, 6), formula.substring(7, 9)); else if (!formula.isEmpty()) { try { result = "" + calculate(formula); //geht nicht? @@ -215,8 +215,83 @@ public class Spreadsheet { */ private long sum(String startCellName, String endCellName) { // TODO implement + int startCellRow = getRow(startCellName); + int startCellCol = getCol(startCellName); + int endCellRow = getRow(endCellName); + int endCellCol = getCol(endCellName); - return 0; + long res = 0; + + for (int i = startCellRow; i <= endCellRow; i++) { + for (int j = startCellCol; j <= endCellCol; j++) { + res = res + (Integer.parseInt(cells[i][j].getValue())); + } + } + + return res; + } + + private long pro(String startCellName, String endCellName) { + int startCellRow = getRow(startCellName); + int startCellCol = getCol(startCellName); + int endCellRow = getRow(endCellName); + int endCellCol = getCol(endCellName); + + long res = 0; + + for (int i = startCellRow; i <= endCellRow; i++) { + for (int j = startCellCol; j <= endCellCol; j++) { + if (res == 0) { + res = Integer.parseInt(cells[i][j].getValue()); + } else { + res = res * (Integer.parseInt(cells[i][j].getValue())); + } + } + } + + return res; + } + + private long min(String startCellName, String endCellName) { + int startCellRow = getRow(startCellName); + int startCellCol = getCol(startCellName); + int endCellRow = getRow(endCellName); + int endCellCol = getCol(endCellName); + + long res = Long.MAX_VALUE; + long merker; + + for (int i = startCellRow; i <= endCellRow; i++) { + for (int j = startCellCol; j <= endCellCol; j++) { + merker = Integer.parseInt(cells[i][j].getValue()); + if (merker < res) { + res = merker; + } + } + } + + return res; + } + + private long max(String startCellName, String endCellName) { + int startCellRow = getRow(startCellName); + int startCellCol = getCol(startCellName); + int endCellRow = getRow(endCellName); + int endCellCol = getCol(endCellName); + + long res = Long.MIN_VALUE; + long merker; + + for (int i = startCellRow; i <= endCellRow; i++) { + for (int j = startCellCol; j <= endCellCol; j++) { + merker = Integer.parseInt(cells[i][j].getValue()); + if (merker > res) { + res = merker; + } + } + } + + return res; } /**