From a3da7cf436612584583cf5c26f8c82b8cb2fab73 Mon Sep 17 00:00:00 2001 From: 3011357 <3011357@stud.hs-mannheim.de> Date: Fri, 5 Jan 2024 19:18:41 +0100 Subject: [PATCH] =?UTF-8?q?calculate()=20Methode=20erg=C3=A4nzt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../informatik/spreadsheet/Spreadsheet.java | 51 ++++++++++++++++--- 1 file changed, 43 insertions(+), 8 deletions(-) diff --git a/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java b/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java index 3822697..389d180 100644 --- a/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java +++ b/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java @@ -3,6 +3,7 @@ package de.hs_mannheim.informatik.spreadsheet; import java.io.FileNotFoundException; import java.io.IOException; import java.io.PrintWriter; +import java.sql.SQLOutput; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -110,7 +111,8 @@ public class Spreadsheet { /** * This method does the actual evaluation/calcluation of a specific cell - * @param cellName the name of the cell to be evaluated + * @param row the row of the cell to be evaluated + * @param col the col of the cell to be evaluated * @return Nothing. */ private void evaluateCell(int row, int col) { @@ -165,17 +167,50 @@ public class Spreadsheet { Matcher m = Pattern.compile("([A-Z][0-9]*)|[-\\+\\*/]|[0-9]*").matcher(formula); long res = 0; + char operator = '+'; - // 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() + while (m.find()) { String s = m.group(); - if (!s.isEmpty()) { - System.out.println(s); + long value; + + if(s.isEmpty()){ + continue; + } + + System.out.println("Beginning of loop " + s); + if (!(s.matches("[-\\+\\*/]"))) { + if(s.matches("[A-Z][0-9]*")) { + int col = this.getCol(s); + int row = this.getRow(s); + + String cellValue = cells[row][col].getValue(); + value = Long.parseLong(cells[row][col].getValue()); + + }else{ + System.out.println("in number in try"); + value = Long.parseLong(s); + } + + switch(operator) { + case '+': + res += value; + break; + case '-': + res -= value; + break; + case '*': + res *= value; + break; + case '/': + if (res != 0) { + res /= value; + break; + } + } + }else{ + operator = s.charAt(0); } } - return res; }