Standardabweichung

ERANZER 2024-01-05 19:28:29 +01:00
parent 85905a5d6d
commit 99017baabe
1 changed files with 23 additions and 3 deletions

View File

@ -141,8 +141,13 @@ public class Spreadsheet {
result = "" + average(formula.substring(11, 13), formula.substring(14, 17)); result = "" + average(formula.substring(11, 13), formula.substring(14, 17));
else else
result = "" + average(formula.substring(11, 13), formula.substring(14, 16)); result = "" + average(formula.substring(11, 13), formula.substring(14, 16));
else if (formula.startsWith("STABW(")) // e.g. STABW(C6:D8) -> Standardabweichung else if (formula.startsWith("STABW("))
result = "TODO"; // TODO 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 else if (formula.startsWith("MIN(")) // e.g. MIN(C13:H13) -> größter Wert
result = "TODO"; // TODO result = "TODO"; // TODO
else if (formula.startsWith("MAX(")) // e.g. MAX(A1:A10) -> Standardabweichung 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) { private long average(String startCellName, String endCellName) {
long average=0; long average=0;
int counter=0; long counter=0;
for(int i=startCellName.charAt(0)-'A'; i<=endCellName.charAt(0)-'A'; i++) { 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++) { 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()) {
@ -202,6 +207,21 @@ public class Spreadsheet {
} }
return (long) average/counter; 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 * This method calculates the result of a "normal" algebraic expression. It only needs to support