From 99017baabef9ddfed2cca2b49745447631b0bb7b Mon Sep 17 00:00:00 2001 From: ERANZER Date: Fri, 5 Jan 2024 19:28:29 +0100 Subject: [PATCH] Standardabweichung --- .../informatik/spreadsheet/Spreadsheet.java | 26 ++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java b/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java index ae7f922..ba99bd8 100644 --- a/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java +++ b/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java @@ -141,8 +141,13 @@ public class Spreadsheet { 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("STABW(")) + if(formula.length()==14) + result = "" + standardDeviation(formula.substring(6, 9), formula.substring(10, 13)); + else if(formula.length()==13) + 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 @@ -190,7 +195,7 @@ public class Spreadsheet { } private long average(String startCellName, String endCellName) { long average=0; - int counter=0; + 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()) { @@ -202,6 +207,21 @@ public class Spreadsheet { } return (long) average/counter; } + private long standardDeviation(String startCellName, String endCellName) { + long average=average(startCellName,endCellName); + long counter=0; + 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()) { + continue; + } + value+=Math.pow(Integer.parseInt(cells[j][i].getValue())-average,2); + counter++; + } + } + return (long) Math.sqrt(value/(counter-1)); + } /** * This method calculates the result of a "normal" algebraic expression. It only needs to support