Spreadsheet calculate/evaluateCell method fix.
parent
138275e3e3
commit
5fbf2658d4
|
@ -22,7 +22,6 @@ public class Axel {
|
||||||
|
|
||||||
System.out.println(spr.toStringShowFormula());
|
System.out.println(spr.toStringShowFormula());
|
||||||
System.out.println(spr);
|
System.out.println(spr);
|
||||||
System.out.println(spr.cells[0][5]);
|
|
||||||
|
|
||||||
|
|
||||||
// TODO: You might want to put "UI loop" for entering value and formulas here resp. in some UI methods.
|
// TODO: You might want to put "UI loop" for entering value and formulas here resp. in some UI methods.
|
||||||
|
|
|
@ -150,80 +150,86 @@ public class Spreadsheet {
|
||||||
private void evaluateCell(int row, int col) {
|
private void evaluateCell(int row, int col) {
|
||||||
String formula = cells[row][col].getFormula();
|
String formula = cells[row][col].getFormula();
|
||||||
String result = "";
|
String result = "";
|
||||||
|
String substringStartCell1 = "";
|
||||||
|
String substringStartCell2 = "";
|
||||||
|
String substringStartCell3 = "";
|
||||||
|
String substringEndCell1 = "";
|
||||||
|
String substringEndCell2 = "";
|
||||||
|
|
||||||
if(formula.length()>10) {
|
if(formula.length()>10) {
|
||||||
String substringStartCell1 = formula.substring(formula.length() - 6, formula.length() - 4);
|
substringStartCell1 = formula.substring(formula.length() - 6, formula.length() - 4);
|
||||||
String substringStartCell2 = formula.substring(formula.length() - 7, formula.length() - 5);
|
substringStartCell2 = formula.substring(formula.length() - 7, formula.length() - 5);
|
||||||
String substringStartCell3 = formula.substring(formula.length() - 8, formula.length() - 5);
|
substringStartCell3 = formula.substring(formula.length() - 8, formula.length() - 5);
|
||||||
String substringEndCell1 = formula.substring(formula.length() - 3, formula.length() - 1);
|
substringEndCell1 = formula.substring(formula.length() - 3, formula.length() - 1);
|
||||||
String substringEndCell2 = formula.substring(formula.length() - 4, formula.length() - 1);
|
substringEndCell2 = formula.substring(formula.length() - 4, formula.length() - 1);
|
||||||
|
|
||||||
if (formula.startsWith("SUMME("))// e.g. SUMME(A3:A8)
|
|
||||||
if (formula.length() < 13)
|
|
||||||
result = "" + sum(substringStartCell1, substringEndCell1);
|
|
||||||
else if (formula.length() < 14)
|
|
||||||
result = "" + sum(substringStartCell2, substringEndCell2);
|
|
||||||
else if (formula.length() < 15)
|
|
||||||
result = "" + sum(substringStartCell3, substringEndCell2);
|
|
||||||
else
|
|
||||||
result = "0";
|
|
||||||
|
|
||||||
|
|
||||||
else if (formula.startsWith("PRODUKT(")) // e.g. PRODUKT(A3:B9)
|
|
||||||
if (formula.length() < 15)
|
|
||||||
result = "" + product(substringStartCell1, substringEndCell1);
|
|
||||||
else if (formula.length() < 16 && !substringEndCell2.contains(":"))
|
|
||||||
result = "" + product(substringStartCell2, substringEndCell2);
|
|
||||||
else if (formula.length() < 17)
|
|
||||||
result = "" + product(substringStartCell3, substringEndCell2);
|
|
||||||
else
|
|
||||||
result = "0";
|
|
||||||
|
|
||||||
|
|
||||||
else if (formula.startsWith("MITTELWERT(")) // e.g. MITTELWERT(A3:A5)
|
|
||||||
if (formula.length() < 17)
|
|
||||||
result = "" + average(substringStartCell1, substringEndCell1);
|
|
||||||
else if (formula.length() < 18)
|
|
||||||
result = "" + average(substringStartCell2, substringEndCell2);
|
|
||||||
else if (formula.length() < 19)
|
|
||||||
result = "" + average(substringStartCell3, substringEndCell2);
|
|
||||||
else
|
|
||||||
result = "0";
|
|
||||||
|
|
||||||
|
|
||||||
else if (formula.startsWith("STABW(")) // e.g. STABW(C6:D8) -> Standardabweichung
|
|
||||||
if (formula.length() < 13)
|
|
||||||
result = "" + standardDeviation(substringStartCell1, substringEndCell1);
|
|
||||||
else if (formula.length() < 14)
|
|
||||||
result = "" + standardDeviation(substringStartCell2, substringEndCell2);
|
|
||||||
else if (formula.length() < 15)
|
|
||||||
result = "" + standardDeviation(substringStartCell3, substringEndCell2);
|
|
||||||
else
|
|
||||||
result = "0";
|
|
||||||
|
|
||||||
|
|
||||||
else if (formula.startsWith("MIN(")) // e.g. MIN(C1:H13) -> kleinster Wert
|
|
||||||
if (formula.length() < 11)
|
|
||||||
result = "" + min(substringStartCell1, substringEndCell1);
|
|
||||||
else if (formula.length() < 12)
|
|
||||||
result = "" + min(substringStartCell2, substringEndCell2);
|
|
||||||
else if (formula.length() < 13)
|
|
||||||
result = "" + min(substringStartCell3, substringEndCell2);
|
|
||||||
else
|
|
||||||
result = "0";
|
|
||||||
|
|
||||||
|
|
||||||
else if (formula.startsWith("MAX(")) // e.g. MAX(A1:A10) -> größter Wert
|
|
||||||
if (formula.length() < 11)
|
|
||||||
result = "" + max(substringStartCell1, substringEndCell1);
|
|
||||||
else if (formula.length() < 12)
|
|
||||||
result = "" + max(substringStartCell2, substringEndCell2);
|
|
||||||
else if (formula.length() < 13)
|
|
||||||
result = "" + max(substringStartCell3, substringEndCell2);
|
|
||||||
else
|
|
||||||
result = "0";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (formula.startsWith("SUMME("))// e.g. SUMME(A3:A8)
|
||||||
|
if (formula.length() < 13)
|
||||||
|
result = "" + sum(substringStartCell1, substringEndCell1);
|
||||||
|
else if (formula.length() < 14)
|
||||||
|
result = "" + sum(substringStartCell2, substringEndCell2);
|
||||||
|
else if (formula.length() < 15)
|
||||||
|
result = "" + sum(substringStartCell3, substringEndCell2);
|
||||||
|
else
|
||||||
|
result = "0";
|
||||||
|
|
||||||
|
|
||||||
|
else if (formula.startsWith("PRODUKT(")) // e.g. PRODUKT(A3:B9)
|
||||||
|
if (formula.length() < 15)
|
||||||
|
result = "" + product(substringStartCell1, substringEndCell1);
|
||||||
|
else if (formula.length() < 16 && !substringEndCell2.contains(":"))
|
||||||
|
result = "" + product(substringStartCell2, substringEndCell2);
|
||||||
|
else if (formula.length() < 17)
|
||||||
|
result = "" + product(substringStartCell3, substringEndCell2);
|
||||||
|
else
|
||||||
|
result = "0";
|
||||||
|
|
||||||
|
|
||||||
|
else if (formula.startsWith("MITTELWERT(")) // e.g. MITTELWERT(A3:A5)
|
||||||
|
if (formula.length() < 17)
|
||||||
|
result = "" + average(substringStartCell1, substringEndCell1);
|
||||||
|
else if (formula.length() < 18)
|
||||||
|
result = "" + average(substringStartCell2, substringEndCell2);
|
||||||
|
else if (formula.length() < 19)
|
||||||
|
result = "" + average(substringStartCell3, substringEndCell2);
|
||||||
|
else
|
||||||
|
result = "0";
|
||||||
|
|
||||||
|
|
||||||
|
else if (formula.startsWith("STABW(")) // e.g. STABW(C6:D8) -> Standardabweichung
|
||||||
|
if (formula.length() < 13)
|
||||||
|
result = "" + standardDeviation(substringStartCell1, substringEndCell1);
|
||||||
|
else if (formula.length() < 14)
|
||||||
|
result = "" + standardDeviation(substringStartCell2, substringEndCell2);
|
||||||
|
else if (formula.length() < 15)
|
||||||
|
result = "" + standardDeviation(substringStartCell3, substringEndCell2);
|
||||||
|
else
|
||||||
|
result = "0";
|
||||||
|
|
||||||
|
|
||||||
|
else if (formula.startsWith("MIN(")) // e.g. MIN(C1:H13) -> kleinster Wert
|
||||||
|
if (formula.length() < 11)
|
||||||
|
result = "" + min(substringStartCell1, substringEndCell1);
|
||||||
|
else if (formula.length() < 12)
|
||||||
|
result = "" + min(substringStartCell2, substringEndCell2);
|
||||||
|
else if (formula.length() < 13)
|
||||||
|
result = "" + min(substringStartCell3, substringEndCell2);
|
||||||
|
else
|
||||||
|
result = "0";
|
||||||
|
|
||||||
|
|
||||||
|
else if (formula.startsWith("MAX(")) // e.g. MAX(A1:A10) -> größter Wert
|
||||||
|
if (formula.length() < 11)
|
||||||
|
result = "" + max(substringStartCell1, substringEndCell1);
|
||||||
|
else if (formula.length() < 12)
|
||||||
|
result = "" + max(substringStartCell2, substringEndCell2);
|
||||||
|
else if (formula.length() < 13)
|
||||||
|
result = "" + max(substringStartCell3, substringEndCell2);
|
||||||
|
else
|
||||||
|
result = "0";
|
||||||
|
|
||||||
|
|
||||||
else if (!formula.isEmpty()) {
|
else if (!formula.isEmpty()) {
|
||||||
try {
|
try {
|
||||||
result = "" + calculate(formula);
|
result = "" + calculate(formula);
|
||||||
|
@ -380,7 +386,7 @@ public class Spreadsheet {
|
||||||
* @return The result calculated.
|
* @return The result calculated.
|
||||||
*/
|
*/
|
||||||
private long calculate(String formula) throws ArithmeticException {
|
private long calculate(String formula) throws ArithmeticException {
|
||||||
Matcher m = Pattern.compile("([A-Z][0-9]*)|[-+*/]|[0-9]+").matcher(formula);
|
Matcher m = Pattern.compile("([A-Z][1-9][0-9]*)|[-+*/]|[0-9]+").matcher(formula);
|
||||||
|
|
||||||
long result = 0;
|
long result = 0;
|
||||||
long currentOperand = 0;
|
long currentOperand = 0;
|
||||||
|
@ -392,9 +398,9 @@ public class Spreadsheet {
|
||||||
|
|
||||||
if (s.matches(("[0-9]+")))
|
if (s.matches(("[0-9]+")))
|
||||||
currentOperand = Long.parseLong(s);
|
currentOperand = Long.parseLong(s);
|
||||||
else if ((s.matches("[A-Z][1-9]*") || s.matches("[A-Z][1-9]*+")) && get(s).isEmpty())
|
else if (s.matches("[A-Z][1-9][0-9]*") && get(s).isEmpty())
|
||||||
currentOperand = 0;
|
currentOperand = 0;
|
||||||
else if ((s.matches("[A-Z][1-9]*") || s.matches("[A-Z][1-9]*+"))) {
|
else if (s.matches("[A-Z][1-9][0-9]*")) {
|
||||||
currentOperand = Long.parseLong(get(s));
|
currentOperand = Long.parseLong(get(s));
|
||||||
} else {
|
} else {
|
||||||
if (!firstOperator) {
|
if (!firstOperator) {
|
||||||
|
|
Loading…
Reference in New Issue