1
0
Fork 0

Spreadsheet calculate/evaluateCell method fix.

pull/1/head
selim 2023-12-26 17:40:50 +01:00
parent 138275e3e3
commit 5fbf2658d4
2 changed files with 79 additions and 74 deletions

View File

@ -22,7 +22,6 @@ public class Axel {
System.out.println(spr.toStringShowFormula());
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.

View File

@ -150,80 +150,86 @@ public class Spreadsheet {
private void evaluateCell(int row, int col) {
String formula = cells[row][col].getFormula();
String result = "";
String substringStartCell1 = "";
String substringStartCell2 = "";
String substringStartCell3 = "";
String substringEndCell1 = "";
String substringEndCell2 = "";
if(formula.length()>10) {
String substringStartCell1 = formula.substring(formula.length() - 6, formula.length() - 4);
String substringStartCell2 = formula.substring(formula.length() - 7, formula.length() - 5);
String substringStartCell3 = formula.substring(formula.length() - 8, formula.length() - 5);
String substringEndCell1 = formula.substring(formula.length() - 3, formula.length() - 1);
String 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";
substringStartCell1 = formula.substring(formula.length() - 6, formula.length() - 4);
substringStartCell2 = formula.substring(formula.length() - 7, formula.length() - 5);
substringStartCell3 = formula.substring(formula.length() - 8, formula.length() - 5);
substringEndCell1 = formula.substring(formula.length() - 3, 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";
else if (!formula.isEmpty()) {
try {
result = "" + calculate(formula);
@ -380,7 +386,7 @@ public class Spreadsheet {
* @return The result calculated.
*/
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 currentOperand = 0;
@ -392,9 +398,9 @@ public class Spreadsheet {
if (s.matches(("[0-9]+")))
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;
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));
} else {
if (!firstOperator) {