From 365b569a677d31ec4937bb48dc6e3b291c62d43e Mon Sep 17 00:00:00 2001 From: ERANZER Date: Tue, 9 Jan 2024 13:25:44 +0100 Subject: [PATCH] Ausrechnen von Formeln --- .classpath | 1 + .../informatik/spreadsheet/Axel.java | 12 ++-- .../informatik/spreadsheet/Spreadsheet.java | 59 ++++++++++++++++--- 3 files changed, 57 insertions(+), 15 deletions(-) diff --git a/.classpath b/.classpath index c70d51a..348ad9a 100644 --- a/.classpath +++ b/.classpath @@ -2,5 +2,6 @@ + diff --git a/Axel/src/de/hs_mannheim/informatik/spreadsheet/Axel.java b/Axel/src/de/hs_mannheim/informatik/spreadsheet/Axel.java index 450705c..3eaae7c 100644 --- a/Axel/src/de/hs_mannheim/informatik/spreadsheet/Axel.java +++ b/Axel/src/de/hs_mannheim/informatik/spreadsheet/Axel.java @@ -20,11 +20,11 @@ public class Axel { cols=Integer.parseInt(kb.nextLine()); Spreadsheet spr=new Spreadsheet(rows,cols); //spr.put("A3", "3"); - //spr.put("A2", "1"); - - //spr.put("B9", "=41+A2"); - //spr.put("J5", "=7*6"); - //spr.put("J6", "=3/2"); + spr.put("A2", "2"); + spr.put("A1","=19+4"); + spr.put("B9", "=41+A2"); + spr.put("J5", "=7*6"); + spr.put("J6", "=4/2"); while(true) { System.out.println(spr); @@ -44,8 +44,6 @@ public class Axel { } spr.saveCsv("/tmp/test.csv"); kb.close(); - - // TODO: You might want to put "UI loop" for entering value and formulas here resp. in some UI methods. } } \ No newline at end of file diff --git a/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java b/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java index c077ec1..847bed2 100644 --- a/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java +++ b/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java @@ -21,15 +21,13 @@ public class Spreadsheet { * @param cols number of columns */ public Spreadsheet(int rows, int cols) { - - // TODO limit the maximum size on 99 (1..99) rows and 26 (A..Z) columns if (rows>99){ rows=99; } if (cols>26) { cols=26; } - + cells = new Cell[rows][cols]; for (int r = 0; r < rows; r++) @@ -260,14 +258,59 @@ public class Spreadsheet { Matcher m = Pattern.compile("([A-Z][0-9]*)|[-\\+\\*/]|[0-9]*").matcher(formula); long res = 0; + boolean first=true; + String operation=""; - // TODO implement - - // uncomment the following to see an example how the elements of a formula can be accessed while (m.find()) { // m.find() must always be used before m.group() String s = m.group(); - if (!s.isEmpty()) { - System.out.println(s); + if(!s.isEmpty()) { + if(first && (int)s.charAt(0)>=(int)'A'&&(int)s.charAt(0)<=(int)'Z') { + res=Integer.parseInt(cells[(Integer.parseInt(s.substring(1)))-1][(int)s.charAt(0)-(int)-'A'].getValue()); + first=false; + }else if(first) { + res=Integer.parseInt(s); + first=false; + }else if(s.equals("+")){ + operation="+"; + }else if(s.equals("-")){ + operation="-"; + }else if(s.equals("*")){ + operation="*"; + }else if(s.equals("/")){ + operation="/"; + }else if((int)s.charAt(0)>=(int)'A'&&(int)s.charAt(0)<=(int)'Z'){ + switch(operation) { + case("+"): + res+=Integer.parseInt(cells[(Integer.parseInt(s.substring(1)))-1][(int)s.charAt(0)-(int)'A'].getValue()); + break; + case("-"): + res-=Integer.parseInt(cells[(Integer.parseInt(s.substring(1)))-1][(int)s.charAt(0)-(int)'A'].getValue()); + break; + case("*"): + res*=Integer.parseInt(cells[(Integer.parseInt(s.substring(1)))-1][(int)s.charAt(0)-(int)'A'].getValue()); + break; + case("/"): + res/=Integer.parseInt(cells[(Integer.parseInt(s.substring(1)))-1][(int)s.charAt(0)-(int)'A'].getValue()); + break; + } + }else { + switch(operation) { + case("+"): + res+=Integer.parseInt(s); + break; + case("-"): + res-=Integer.parseInt(s); + break; + case("*"): + res*=Integer.parseInt(s); + break; + case("/"): + res/=Integer.parseInt(s); + break; + } + } + + } }