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 {
|
public static void main(String[] args) throws FileNotFoundException {
|
||||||
Spreadsheet spr = new Spreadsheet(10,10);
|
Spreadsheet spr = new Spreadsheet(10,10);
|
||||||
|
|
||||||
spr.put("A3", "123");
|
spr.cells[2][7].setFormula("=5+3");
|
||||||
spr.put("A2", "1");
|
spr.updateSpreadsheet();
|
||||||
|
|
||||||
spr.put("B9", "=41+A2");
|
|
||||||
spr.put("J5", "=7*6");
|
|
||||||
spr.put("J6", "=3/2");
|
|
||||||
|
|
||||||
System.out.println(spr);
|
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.
|
// 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.
|
* @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][0-9]*)|[-+*/]|[0-9]+").matcher(formula);
|
||||||
|
|
||||||
long res = 0;
|
long res = 0;
|
||||||
long currentOperand = 0;
|
long currentOperand = 0;
|
||||||
|
@ -244,28 +244,27 @@ public class Spreadsheet {
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
private long evaluateOperator(long res, long currentOperand, String currentOperator) {
|
private long evaluateOperator(long res, long currentOperand, String currentOperator) {
|
||||||
switch (currentOperator) {
|
switch (currentOperator) {
|
||||||
case "+":
|
case "+":
|
||||||
res += currentOperand;
|
return res + currentOperand;
|
||||||
break;
|
|
||||||
case "-":
|
case "-":
|
||||||
res -= currentOperand;
|
return res - currentOperand;
|
||||||
break;
|
|
||||||
case "*":
|
case "*":
|
||||||
res *= currentOperand;
|
return res * currentOperand;
|
||||||
break;
|
|
||||||
case "/":
|
case "/":
|
||||||
res /= currentOperand;
|
return res / currentOperand;
|
||||||
break;
|
|
||||||
default:
|
default:
|
||||||
throw new IllegalArgumentException("Invalid operator: " + currentOperator);
|
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() {
|
public String toString() {
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue