1
0
Fork 0
main
Jan 2024-01-07 12:34:39 +01:00
parent 95081cb010
commit f5c29329fe
1 changed files with 97 additions and 12 deletions

View File

@ -147,11 +147,11 @@ public class Spreadsheet {
}else if (formula.startsWith("MID(")) { // e.g. MITTELWERT(A3:A5) }else if (formula.startsWith("MID(")) { // e.g. MITTELWERT(A3:A5)
result += mid(formula.substring(offset, diff), formula.substring(diff+1, offsetEnd)); result += mid(formula.substring(offset, diff), formula.substring(diff+1, offsetEnd));
}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 += stabw(formula.substring(offset, diff), formula.substring(diff+1, offsetEnd));
}else if (formula.startsWith("MIN(")) {// e.g. MIN(C13:H13) -> größter Wert }else if (formula.startsWith("MIN(")) {// e.g. MIN(C13:H13) -> kleinster Wert
result = "TODO"; // TODO result += min(formula.substring(offset, diff), formula.substring(diff+1, offsetEnd));
}else if (formula.startsWith("MAX(")) { // e.g. MAX(A1:A10) -> Standardabweichung }else if (formula.startsWith("MAX(")) { // e.g. MAX(A1:A10) -> größter Wert
result = "TODO"; // TODO result += max(formula.substring(offset, diff), formula.substring(diff+1, offsetEnd));
}else if (!formula.isEmpty()) { }else if (!formula.isEmpty()) {
try { try {
result = "" + calculate(formula); result = "" + calculate(formula);
@ -171,8 +171,8 @@ public class Spreadsheet {
*/ */
private long sum(String startCellName, String endCellName) { private long sum(String startCellName, String endCellName) {
long res = 0; long res = 0;
for(int j = getRow(startCellName); j< (getRow(endCellName)+1);j++){ for(int j = getRow(startCellName); j<= (getRow(endCellName));j++){
for(int i = getCol(startCellName); i< (getCol(endCellName)+1);i++) { for(int i = getCol(startCellName); i<= (getCol(endCellName));i++) {
if(!cells[j][i].getValue().isBlank()) { if(!cells[j][i].getValue().isBlank()) {
res += Long.parseLong(cells[j][i].getValue()); res += Long.parseLong(cells[j][i].getValue());
} }
@ -189,8 +189,8 @@ public class Spreadsheet {
private long prod(String startCellName, String endCellName) { private long prod(String startCellName, String endCellName) {
long res = 1; long res = 1;
boolean anyNumbers= false; boolean anyNumbers= false;
for(int j = getRow(startCellName); j< (getRow(endCellName)+1);j++){ for(int j = getRow(startCellName); j<= (getRow(endCellName));j++){
for(int i = getCol(startCellName); i< (getCol(endCellName)+1);i++) { for(int i = getCol(startCellName); i<= (getCol(endCellName));i++) {
if(!cells[j][i].getValue().isBlank()) { if(!cells[j][i].getValue().isBlank()) {
res*= Long.parseLong(cells[j][i].getValue()); res*= Long.parseLong(cells[j][i].getValue());
anyNumbers = true; anyNumbers = true;
@ -211,8 +211,8 @@ public class Spreadsheet {
private long mid(String startCellName, String endCellName) { private long mid(String startCellName, String endCellName) {
long res = 0; long res = 0;
int Numbers= 0; int Numbers= 0;
for(int j = getRow(startCellName); j< (getRow(endCellName)+1);j++){ for(int j = getRow(startCellName); j<= (getRow(endCellName));j++){
for(int i = getCol(startCellName); i< (getCol(endCellName)+1);i++) { for(int i = getCol(startCellName); i<= (getCol(endCellName));i++) {
if(!cells[j][i].getValue().isBlank()) { if(!cells[j][i].getValue().isBlank()) {
res+= Long.parseLong(cells[j][i].getValue()); res+= Long.parseLong(cells[j][i].getValue());
Numbers++; Numbers++;
@ -222,7 +222,92 @@ public class Spreadsheet {
res/=Numbers; res/=Numbers;
return res; return res;
} }
/**
* Method for calculating the Standard deviation 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 Standard deviation calculated.
*/
private long stabw(String startCellName, String endCellName) {
long res=0;
long midSum=0;
//mid(startCellName,endCellName)
//mid ist nicht verwendbar weil wir die anzahl der Nummern sowieso brauchen
int numbers= 0;
for(int j = getRow(startCellName); j<= (getRow(endCellName));j++){
for(int i = getCol(startCellName); i<= (getCol(endCellName));i++) {
if(!cells[j][i].getValue().isBlank()) {
midSum+= Long.parseLong(cells[j][i].getValue());
numbers++;
}
}
}
double midVal = (double) midSum / numbers;
double sum = 0.0;
for (int j = getRow(startCellName); j <= getRow(endCellName); j++) {
for (int i = getCol(startCellName); i <= getCol(endCellName); i++) {
if (!cells[j][i].getValue().isBlank()) {
long value = Long.parseLong(cells[j][i].getValue());
sum += Math.pow(value - midVal, 2);
}
}
}
double stabw = Math.sqrt(sum / numbers);
return (long) stabw;
}
/**
* Method for calculating the minimum value 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 minimum value calculated.
*/
private long min(String startCellName, String endCellName) {
long res = 0;
List<Long> args = new ArrayList<>();
for(int j = getRow(startCellName); j<= (getRow(endCellName));j++){
for(int i = getCol(startCellName); i<= (getCol(endCellName));i++) {
if(!cells[j][i].getValue().isBlank()) {
args.add(Long.parseLong(cells[j][i].getValue()));
}
}
}
if(args.size()>0) {
res=args.get(0);
}
for(int i = 0; i<args.size();i++) {
if(res>args.get(i)) {
res=args.get(i);
}
}
return res;
}
/**
* Method for calculating the maximum value 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 maximum value calculated.
*/
private long max(String startCellName, String endCellName) {
long res = 0;
List<Long> args = new ArrayList<>();
for(int j = getRow(startCellName); j<= (getRow(endCellName));j++){
for(int i = getCol(startCellName); i<= (getCol(endCellName));i++) {
if(!cells[j][i].getValue().isBlank()) {
args.add(Long.parseLong(cells[j][i].getValue()));
}
}
}
if(args.size()>0) {
res=args.get(0);
}
for(int i = 0; i<args.size();i++) {
if(res<args.get(i)) {
res=args.get(i);
}
}
return res;
}
/** /**
* 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
* expressions like =B4 or =2+A3-B2, i.e. only with int numbers and other cells and with plus, * expressions like =B4 or =2+A3-B2, i.e. only with int numbers and other cells and with plus,