1
0
Fork 0

MITTELWERT( added

main
Florian Hörner 2024-01-06 03:44:15 +01:00
parent d8a057e995
commit dc301c13fa
1 changed files with 22 additions and 2 deletions

View File

@ -189,7 +189,7 @@ public class Spreadsheet {
else if (formula.startsWith("PRODUKT(")) // e.g. PRODUKT(A3:B9)
result = "" + pro(formula.substring(8, 10), formula.substring(11, 13));
else if (formula.startsWith("MITTELWERT(")) // e.g. MITTELWERT(A3:A5)
result = "TODO"; // TODO
result = "" + mittelwert(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) -> kleinster Wert
@ -214,7 +214,6 @@ public class Spreadsheet {
* @return The sum calculated.
*/
private long sum(String startCellName, String endCellName) {
// TODO implement
int startCellRow = getRow(startCellName);
int startCellCol = getCol(startCellName);
int endCellRow = getRow(endCellName);
@ -294,6 +293,27 @@ public class Spreadsheet {
return res;
}
private long mittelwert(String startCellName, String endCellName) {
int startCellRow = getRow(startCellName);
int startCellCol = getCol(startCellName);
int endCellRow = getRow(endCellName);
int endCellCol = getCol(endCellName);
long anzahl = 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()));
anzahl++;
}
}
res = res/anzahl;
return res;
}
/**
* This method calculates the result of a "normal" algebraic expression. It only needs to support
* expressions like =B4 or =2+A3-B2, i.e. only with int numbers and other cells and with plus,