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,13 +150,19 @@ 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.startsWith("SUMME("))// e.g. SUMME(A3:A8)
|
||||||
if (formula.length() < 13)
|
if (formula.length() < 13)
|
||||||
|
@ -222,7 +228,7 @@ public class Spreadsheet {
|
||||||
result = "" + max(substringStartCell3, substringEndCell2);
|
result = "" + max(substringStartCell3, substringEndCell2);
|
||||||
else
|
else
|
||||||
result = "0";
|
result = "0";
|
||||||
}
|
|
||||||
|
|
||||||
else if (!formula.isEmpty()) {
|
else if (!formula.isEmpty()) {
|
||||||
try {
|
try {
|
||||||
|
@ -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