From 85905a5d6d27d900bca3d819974cb196ad82c15d Mon Sep 17 00:00:00 2001 From: ERANZER Date: Fri, 5 Jan 2024 19:09:52 +0100 Subject: [PATCH] Mittelwert --- .../informatik/spreadsheet/Spreadsheet.java | 31 +++++++++++++++---- 1 file changed, 25 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 c5ad4e9..ae7f922 100644 --- a/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java +++ b/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java @@ -120,22 +120,27 @@ public class Spreadsheet { String formula = cells[row][col].getFormula(); String result = ""; - if (formula.startsWith("SUMME(")) // e.g. SUMME(A3:A8) + if (formula.startsWith("SUMME(")) if(formula.length()==14) result = "" + sum(formula.substring(6, 9), formula.substring(10, 13)); else if(formula.length()==13) result = "" + sum(formula.substring(6, 8), formula.substring(9, 12)); else result = "" + sum(formula.substring(6, 8), formula.substring(9, 11)); - else if (formula.startsWith("PRODUKT(")) // e.g. PRODUKT(A3:B9) + else if (formula.startsWith("PRODUKT(")) if(formula.length()==16) result = "" + product(formula.substring(8, 11), formula.substring(12, 15)); else if(formula.length()==15) result = "" + product(formula.substring(8, 10), formula.substring(11, 14)); else result = "" + product(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("MITTELWERT(")) + if(formula.length()==19) + result = "" + average(formula.substring(11, 14), formula.substring(15, 18)); + else if(formula.length()==18) + result = "" + average(formula.substring(11, 13), formula.substring(14, 17)); + else + result = "" + average(formula.substring(11, 13), formula.substring(14, 16)); 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 @@ -160,7 +165,7 @@ public class Spreadsheet { * @return The sum calculated. */ private long sum(String startCellName, String endCellName) { - int sum=0; + 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()) { @@ -172,7 +177,7 @@ public class Spreadsheet { return sum; } private long product(String startCellName, String endCellName) { - int product=1; + 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()) { @@ -183,6 +188,20 @@ public class Spreadsheet { } return product; } + private long average(String startCellName, String endCellName) { + long average=0; + int 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()) { + continue; + } + average+=Integer.parseInt(cells[j][i].getValue()); + counter++; + } + } + return (long) average/counter; + } /** * This method calculates the result of a "normal" algebraic expression. It only needs to support