Spreadsheet calculate minor fix and new ui method: updateSpreadsheet.
parent
197a39a16a
commit
096215a33a
|
@ -12,16 +12,12 @@ public class Axel {
|
|||
public static void main(String[] args) throws FileNotFoundException {
|
||||
Spreadsheet spr = new Spreadsheet(10,10);
|
||||
|
||||
spr.put("A3", "123");
|
||||
spr.put("A2", "1");
|
||||
spr.cells[2][7].setFormula("=5+3");
|
||||
spr.updateSpreadsheet();
|
||||
|
||||
spr.put("B9", "=41+A2");
|
||||
spr.put("J5", "=7*6");
|
||||
spr.put("J6", "=3/2");
|
||||
|
||||
System.out.println(spr);
|
||||
|
||||
spr.saveCsv("/tmp/test.csv");
|
||||
|
||||
// TODO: You might want to put "UI loop" for entering value and formulas here resp. in some UI methods.
|
||||
}
|
||||
|
|
|
@ -210,7 +210,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][0-9]*)|[-+*/]|[0-9]+").matcher(formula);
|
||||
|
||||
long res = 0;
|
||||
long currentOperand = 0;
|
||||
|
@ -244,28 +244,27 @@ public class Spreadsheet {
|
|||
|
||||
return res;
|
||||
}
|
||||
|
||||
private long evaluateOperator(long res, long currentOperand, String currentOperator) {
|
||||
switch (currentOperator) {
|
||||
case "+":
|
||||
res += currentOperand;
|
||||
break;
|
||||
return res + currentOperand;
|
||||
case "-":
|
||||
res -= currentOperand;
|
||||
break;
|
||||
return res - currentOperand;
|
||||
case "*":
|
||||
res *= currentOperand;
|
||||
break;
|
||||
return res * currentOperand;
|
||||
case "/":
|
||||
res /= currentOperand;
|
||||
break;
|
||||
return res / currentOperand;
|
||||
default:
|
||||
throw new IllegalArgumentException("Invalid operator: " + currentOperator);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
///---
|
||||
///--- ui methods
|
||||
public void updateSpreadsheet() {
|
||||
for (int r = 0; r < cells.length; r++)
|
||||
for (int c = 0; c < cells[r].length; c++)
|
||||
evaluateCell(r, c);
|
||||
}
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
|
|
Loading…
Reference in New Issue