Spreadsheet evaluateCell further implementation.
parent
096215a33a
commit
e9a9ac5527
|
@ -146,23 +146,50 @@ public class Spreadsheet {
|
|||
String formula = cells[row][col].getFormula();
|
||||
String result = "";
|
||||
|
||||
String substringStartCell1 = formula.substring(formula.length() - 6, formula.length() - 4);
|
||||
String substringStart2 = formula.substring(formula.length() - 6, formula.length() - 3);
|
||||
String substringEndCell = formula.substring(formula.length() - 3, formula.length() - 1);
|
||||
|
||||
|
||||
if (formula.startsWith("SUMME(")) // e.g. SUMME(A3:A8)
|
||||
if(formula.length()<13)
|
||||
result = "" + sum(formula.substring(6, 8), formula.substring(9, 11));
|
||||
else if(formula.length()<14&&formula.substring(6, 9).contains(":"))
|
||||
result = "" + sum(formula.substring(6, 8), formula.substring(9, 12));
|
||||
else if(formula.length()<14&&!formula.substring(6, 9).contains(":"))
|
||||
result = "" + sum(formula.substring(6, 9), formula.substring(10, 12));
|
||||
result = "" + sum(substringStartCell1, substringEndCell);
|
||||
else if(formula.length()<14&&substringStart2.contains(":"))
|
||||
result = "" + sum(substringStartCell1, substringEndCell);
|
||||
else if(formula.length()<14&&!substringStart2.contains(":"))
|
||||
result = "" + sum(substringStart2, substringEndCell);
|
||||
else if(formula.length()<15)
|
||||
result = "" + sum(formula.substring(6, 9), formula.substring(10, 13));
|
||||
result = "" + sum(substringStart2, substringEndCell);
|
||||
else
|
||||
result = "0";
|
||||
|
||||
|
||||
else if (formula.startsWith("PRODUKT(")) // e.g. PRODUKT(A3:B9)
|
||||
result = "TODO"; // TODO
|
||||
if(formula.length()<15)
|
||||
result = "" + product(substringStartCell1, substringEndCell);
|
||||
else if(formula.length()<16&&substringStart2.contains(":"))
|
||||
result = "" + product(substringStartCell1, substringEndCell);
|
||||
else if(formula.length()<16&&!substringStart2.contains(":"))
|
||||
result = "" + product(substringStart2, substringEndCell);
|
||||
else if(formula.length()<17)
|
||||
result = "" + product(substringStart2, substringEndCell);
|
||||
else
|
||||
result = "0";
|
||||
|
||||
|
||||
else if (formula.startsWith("MITTELWERT(")) // e.g. MITTELWERT(A3:A5)
|
||||
result = "TODO"; // TODO
|
||||
if(formula.length()<17)
|
||||
result = "" + average(substringStartCell1, substringEndCell);
|
||||
else if(formula.length()<18&&substringStart2.contains(":"))
|
||||
result = "" + average(substringStartCell1, substringEndCell);
|
||||
else if(formula.length()<18&&!substringStart2.contains(":"))
|
||||
result = "" + average(substringStart2, substringEndCell);
|
||||
else if(formula.length()<19)
|
||||
result = "" + average(substringStart2, substringEndCell);
|
||||
else
|
||||
result = "0";
|
||||
|
||||
|
||||
else if (formula.startsWith("STABW(")) // e.g. STABW(C6:D8) -> Standardabweichung
|
||||
result = "TODO"; // TODO
|
||||
else if (formula.startsWith("MIN(")) // e.g. MIN(C13:H13) -> größter Wert
|
||||
|
@ -198,6 +225,29 @@ public class Spreadsheet {
|
|||
|
||||
return result;
|
||||
}
|
||||
private long product(String startCellName, String endCellName) {
|
||||
long result = Long.parseLong(cells[0][0].getValue());;
|
||||
|
||||
for(int r = getRow(startCellName); r<getRow(endCellName)+1; r++)
|
||||
for(int c = getCol(startCellName); c<getCol(endCellName)+1; c++)
|
||||
try {
|
||||
result *= Long.parseLong(cells[r+1][c+1].getValue());
|
||||
} catch (NumberFormatException n){}
|
||||
|
||||
return result;
|
||||
}
|
||||
private long average(String startCellName, String endCellName){
|
||||
long result = 0;
|
||||
int counter = 0;
|
||||
for(int r = getRow(startCellName); r<getRow(endCellName)+1; r++)
|
||||
for(int c = getCol(startCellName); c<getCol(endCellName)+1; c++)
|
||||
try {
|
||||
result += Long.parseLong(cells[r][c].getValue());
|
||||
counter ++;
|
||||
} catch (NumberFormatException n){}
|
||||
result /= counter;
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method calculates the result of a "normal" algebraic expression. It only needs to support
|
||||
|
@ -212,7 +262,7 @@ public class Spreadsheet {
|
|||
private long calculate(String formula) throws ArithmeticException {
|
||||
Matcher m = Pattern.compile("([A-Z][0-9]*)|[-+*/]|[0-9]+").matcher(formula);
|
||||
|
||||
long res = 0;
|
||||
long result = 0;
|
||||
long currentOperand = 0;
|
||||
String currentOperator = "+";
|
||||
boolean firstOperator = true;
|
||||
|
@ -228,9 +278,9 @@ public class Spreadsheet {
|
|||
currentOperand = Long.parseLong(get(s));
|
||||
} else {
|
||||
if (!firstOperator) {
|
||||
res = evaluateOperator(res, currentOperand, currentOperator);
|
||||
result = evaluateOperator(result, currentOperand, currentOperator);
|
||||
} else {
|
||||
res = currentOperand;
|
||||
result = currentOperand;
|
||||
firstOperator = false;
|
||||
}
|
||||
currentOperator = s;
|
||||
|
@ -238,22 +288,22 @@ public class Spreadsheet {
|
|||
}
|
||||
|
||||
if (!firstOperator) {
|
||||
res = evaluateOperator(res, currentOperand, currentOperator);
|
||||
result = evaluateOperator(result, currentOperand, currentOperator);
|
||||
} else
|
||||
res = currentOperand;
|
||||
result = currentOperand;
|
||||
|
||||
return res;
|
||||
return result;
|
||||
}
|
||||
private long evaluateOperator(long res, long currentOperand, String currentOperator) {
|
||||
private long evaluateOperator(long result, long currentOperand, String currentOperator) {
|
||||
switch (currentOperator) {
|
||||
case "+":
|
||||
return res + currentOperand;
|
||||
return result + currentOperand;
|
||||
case "-":
|
||||
return res - currentOperand;
|
||||
return result - currentOperand;
|
||||
case "*":
|
||||
return res * currentOperand;
|
||||
return result * currentOperand;
|
||||
case "/":
|
||||
return res / currentOperand;
|
||||
return result / currentOperand;
|
||||
default:
|
||||
throw new IllegalArgumentException("Invalid operator: " + currentOperator);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue