1
0
Fork 0

Mittelwert

main
ERANZER 2024-01-05 19:09:52 +01:00
parent 7f44b09aa4
commit 85905a5d6d
1 changed files with 25 additions and 6 deletions

View File

@ -120,22 +120,27 @@ public class Spreadsheet {
String formula = cells[row][col].getFormula(); String formula = cells[row][col].getFormula();
String result = ""; String result = "";
if (formula.startsWith("SUMME(")) // e.g. SUMME(A3:A8) if (formula.startsWith("SUMME("))
if(formula.length()==14) if(formula.length()==14)
result = "" + sum(formula.substring(6, 9), formula.substring(10, 13)); result = "" + sum(formula.substring(6, 9), formula.substring(10, 13));
else if(formula.length()==13) else if(formula.length()==13)
result = "" + sum(formula.substring(6, 8), formula.substring(9, 12)); result = "" + sum(formula.substring(6, 8), formula.substring(9, 12));
else else
result = "" + sum(formula.substring(6, 8), formula.substring(9, 11)); 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) if(formula.length()==16)
result = "" + product(formula.substring(8, 11), formula.substring(12, 15)); result = "" + product(formula.substring(8, 11), formula.substring(12, 15));
else if(formula.length()==15) else if(formula.length()==15)
result = "" + product(formula.substring(8, 10), formula.substring(11, 14)); result = "" + product(formula.substring(8, 10), formula.substring(11, 14));
else else
result = "" + product(formula.substring(8, 10), formula.substring(11, 13)); result = "" + product(formula.substring(8, 10), formula.substring(11, 13));
else if (formula.startsWith("MITTELWERT(")) // e.g. MITTELWERT(A3:A5) else if (formula.startsWith("MITTELWERT("))
result = "TODO"; // TODO 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 else if (formula.startsWith("STABW(")) // e.g. STABW(C6:D8) -> Standardabweichung
result = "TODO"; // TODO result = "TODO"; // TODO
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
@ -160,7 +165,7 @@ public class Spreadsheet {
* @return The sum calculated. * @return The sum calculated.
*/ */
private long sum(String startCellName, String endCellName) { 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 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()) {
@ -172,7 +177,7 @@ public class Spreadsheet {
return sum; return sum;
} }
private long product(String startCellName, String endCellName) { 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 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()) {
@ -183,6 +188,20 @@ public class Spreadsheet {
} }
return product; 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 * This method calculates the result of a "normal" algebraic expression. It only needs to support