1
0
Fork 0

max value of a cell block

main
ERANZER 2024-01-05 20:14:01 +01:00
parent 99017baabe
commit 3f9b7a7091
1 changed files with 25 additions and 18 deletions

View File

@ -148,9 +148,14 @@ public class Spreadsheet {
result = "" + standardDeviation(formula.substring(6, 8), formula.substring(9, 12)); result = "" + standardDeviation(formula.substring(6, 8), formula.substring(9, 12));
else else
result = "" + standardDeviation(formula.substring(6, 8), formula.substring(9, 11)); 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("MAX("))
result = "TODO"; // TODO if(formula.length()==12)
else if (formula.startsWith("MAX(")) // e.g. MAX(A1:A10) -> Standardabweichung result = "" + max(formula.substring(4, 7), formula.substring(8, 11));
else if(formula.length()==11)
result = "" + max(formula.substring(4, 6), formula.substring(7, 10));
else
result = "" + max(formula.substring(4, 6), formula.substring(7, 9));
else if (formula.startsWith("MIN(")) // e.g. MAX(A1:A10) -> Standardabweichung
result = "TODO"; // TODO result = "TODO"; // TODO
else if (!formula.isEmpty()) { else if (!formula.isEmpty()) {
try { try {
@ -162,20 +167,13 @@ public class Spreadsheet {
cells[row][col].setValue("" + result); cells[row][col].setValue("" + result);
} }
//calculations for cell blocks
/**
* Method for calculating the sum of a rectangular block of cells, such as from A1 to B3.
* @param startCellName The name of the cell in the upper left corner of the rectangle.
* @param endCellName The name of the cell in the lower right corner of the rectangle.
* @return The sum calculated.
*/
private long sum(String startCellName, String endCellName) { private long sum(String startCellName, String endCellName) {
long 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())
continue; continue;
}
sum+=Integer.parseInt(cells[j][i].getValue()); sum+=Integer.parseInt(cells[j][i].getValue());
} }
} }
@ -185,9 +183,8 @@ public class Spreadsheet {
long 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())
return 0; return 0;
}
product=product*Integer.parseInt(cells[j][i].getValue()); product=product*Integer.parseInt(cells[j][i].getValue());
} }
} }
@ -198,9 +195,8 @@ public class Spreadsheet {
long 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())
continue; continue;
}
average+=Integer.parseInt(cells[j][i].getValue()); average+=Integer.parseInt(cells[j][i].getValue());
counter++; counter++;
} }
@ -213,15 +209,26 @@ public class Spreadsheet {
long value=0; long value=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())
continue; continue;
}
value+=Math.pow(Integer.parseInt(cells[j][i].getValue())-average,2); value+=Math.pow(Integer.parseInt(cells[j][i].getValue())-average,2);
counter++; counter++;
} }
} }
return (long) Math.sqrt(value/(counter-1)); return (long) Math.sqrt(value/(counter-1));
} }
private int max(String startCellName,String endCellName) {
int max=Integer.MIN_VALUE;
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;
else if(max<Integer.parseInt(cells[j][i].getValue()))
max=Integer.parseInt(cells[j][i].getValue());
}
}
return max;
}
/** /**
* 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