forked from hummel/PR1-Spreadsheet
max value of a cell block
parent
99017baabe
commit
3f9b7a7091
|
@ -148,9 +148,14 @@ public class Spreadsheet {
|
|||
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
|
||||
else if (formula.startsWith("MAX("))
|
||||
if(formula.length()==12)
|
||||
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
|
||||
else if (!formula.isEmpty()) {
|
||||
try {
|
||||
|
@ -162,20 +167,13 @@ public class Spreadsheet {
|
|||
|
||||
cells[row][col].setValue("" + result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
//calculations for cell blocks
|
||||
private long sum(String startCellName, String endCellName) {
|
||||
long sum=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()) {
|
||||
if(cells[j][i].isEmpty())
|
||||
continue;
|
||||
}
|
||||
sum+=Integer.parseInt(cells[j][i].getValue());
|
||||
}
|
||||
}
|
||||
|
@ -185,9 +183,8 @@ public class Spreadsheet {
|
|||
long product=1;
|
||||
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()) {
|
||||
if(cells[j][i].isEmpty())
|
||||
return 0;
|
||||
}
|
||||
product=product*Integer.parseInt(cells[j][i].getValue());
|
||||
}
|
||||
}
|
||||
|
@ -198,9 +195,8 @@ public class Spreadsheet {
|
|||
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()) {
|
||||
if(cells[j][i].isEmpty())
|
||||
continue;
|
||||
}
|
||||
average+=Integer.parseInt(cells[j][i].getValue());
|
||||
counter++;
|
||||
}
|
||||
|
@ -213,15 +209,26 @@ public class Spreadsheet {
|
|||
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()) {
|
||||
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));
|
||||
}
|
||||
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
|
||||
|
|
Loading…
Reference in New Issue