From 096215a33aa4de22b30347679840608e5d1d341a Mon Sep 17 00:00:00 2001 From: selim Date: Tue, 26 Dec 2023 04:49:00 +0100 Subject: [PATCH] Spreadsheet calculate minor fix and new ui method: updateSpreadsheet. --- .../informatik/spreadsheet/Axel.java | 14 ++++------- .../informatik/spreadsheet/Spreadsheet.java | 23 +++++++++---------- 2 files changed, 16 insertions(+), 21 deletions(-) diff --git a/Axel/src/de/hs_mannheim/informatik/spreadsheet/Axel.java b/Axel/src/de/hs_mannheim/informatik/spreadsheet/Axel.java index df720b3..41c5da2 100644 --- a/Axel/src/de/hs_mannheim/informatik/spreadsheet/Axel.java +++ b/Axel/src/de/hs_mannheim/informatik/spreadsheet/Axel.java @@ -11,17 +11,13 @@ 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.put("B9", "=41+A2"); - spr.put("J5", "=7*6"); - spr.put("J6", "=3/2"); + + spr.cells[2][7].setFormula("=5+3"); + spr.updateSpreadsheet(); + 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. } diff --git a/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java b/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java index f7877d0..94ddeb5 100644 --- a/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java +++ b/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java @@ -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();