From 3f9b7a7091b88e890b51fd598c468071e576d7a9 Mon Sep 17 00:00:00 2001 From: ERANZER Date: Fri, 5 Jan 2024 20:14:01 +0100 Subject: [PATCH] max value of a cell block --- .../informatik/spreadsheet/Spreadsheet.java | 43 +++++++++++-------- 1 file changed, 25 insertions(+), 18 deletions(-) diff --git a/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java b/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java index ba99bd8..553edaf 100644 --- a/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java +++ b/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java @@ -148,9 +148,14 @@ public class Spreadsheet { result = "" + standardDeviation(formula.substring(6, 8), formula.substring(9, 12)); else result = "" + standardDeviation(formula.substring(6, 8), formula.substring(9, 11)); - 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 + else if (formula.startsWith("MAX(")) + if(formula.length()==12) + result = "" + max(formula.substring(4, 7), formula.substring(8, 11)); + else if(formula.length()==11) + result = "" + max(formula.substring(4, 6), formula.substring(7, 10)); + else + result = "" + max(formula.substring(4, 6), formula.substring(7, 9)); + else if (formula.startsWith("MIN(")) // e.g. MAX(A1:A10) -> Standardabweichung result = "TODO"; // TODO else if (!formula.isEmpty()) { try { @@ -162,20 +167,13 @@ public class Spreadsheet { cells[row][col].setValue("" + result); } - - /** - * 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. - * @param endCellName The name of the cell in the lower right corner of the rectangle. - * @return The sum calculated. - */ + //calculations for cell blocks private long sum(String startCellName, String endCellName) { long sum=0; for(int i=startCellName.charAt(0)-'A'; i<=endCellName.charAt(0)-'A'; i++) { for(int j=Integer.parseInt(startCellName.substring(1))-1; j<=Integer.parseInt(endCellName.substring(1))-1; j++) { - if(cells[j][i].isEmpty()) { + if(cells[j][i].isEmpty()) continue; - } sum+=Integer.parseInt(cells[j][i].getValue()); } } @@ -185,9 +183,8 @@ public class Spreadsheet { long product=1; for(int i=startCellName.charAt(0)-'A'; i<=endCellName.charAt(0)-'A'; i++) { for(int j=Integer.parseInt(startCellName.substring(1))-1; j<=Integer.parseInt(endCellName.substring(1))-1; j++) { - if(cells[j][i].isEmpty()) { + if(cells[j][i].isEmpty()) return 0; - } product=product*Integer.parseInt(cells[j][i].getValue()); } } @@ -198,9 +195,8 @@ public class Spreadsheet { long counter=0; for(int i=startCellName.charAt(0)-'A'; i<=endCellName.charAt(0)-'A'; i++) { for(int j=Integer.parseInt(startCellName.substring(1))-1; j<=Integer.parseInt(endCellName.substring(1))-1; j++) { - if(cells[j][i].isEmpty()) { + if(cells[j][i].isEmpty()) continue; - } average+=Integer.parseInt(cells[j][i].getValue()); counter++; } @@ -213,15 +209,26 @@ public class Spreadsheet { long value=0; for(int i=startCellName.charAt(0)-'A'; i<=endCellName.charAt(0)-'A'; i++) { for(int j=Integer.parseInt(startCellName.substring(1))-1; j<=Integer.parseInt(endCellName.substring(1))-1; j++) { - if(cells[j][i].isEmpty()) { + if(cells[j][i].isEmpty()) continue; - } value+=Math.pow(Integer.parseInt(cells[j][i].getValue())-average,2); counter++; } } return (long) Math.sqrt(value/(counter-1)); } + private int max(String startCellName,String endCellName) { + int max=Integer.MIN_VALUE; + for(int i=startCellName.charAt(0)-'A'; i<=endCellName.charAt(0)-'A'; i++) { + for(int j=Integer.parseInt(startCellName.substring(1))-1; j<=Integer.parseInt(endCellName.substring(1))-1; j++) { + if(cells[j][i].isEmpty()) + continue; + else if(max