From 75dec01910563352c8df19578eb064e579d03a81 Mon Sep 17 00:00:00 2001 From: <3010678@stud.hs-mannheim.de> Date: Fri, 5 Jan 2024 16:44:25 +0100 Subject: [PATCH] //implemented calculate method --- .../informatik/spreadsheet/Spreadsheet.java | 62 ++++++++++++++++++- 1 file changed, 59 insertions(+), 3 deletions(-) diff --git a/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java b/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java index dacee48..daa772e 100644 --- a/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java +++ b/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java @@ -373,16 +373,72 @@ public class Spreadsheet { Matcher m = Pattern.compile("([A-Z][0-9]*)|[-\\+\\*/]|[0-9]*").matcher(formula); long res = 0; - + String cellName=""; + String operand=""; + long value=0; + int row; + int col; + int lock=0; + int cnt=0; // 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); +// System.out.println(s); + + if(Character.isLetter(s.charAt(0))) { + cellName= s; + row=getRow(cellName); + col=getCol(cellName); + value= Integer.parseInt(cells[row][col].getValue()); + lock=0; + + }else if(Character.isDigit(s.charAt(0))) { + value= Integer.parseInt(s); + + lock=0; + }else { + operand = s; + System.out.println(operand); + lock=1; + } + + if(cnt==0) + res=value; + + if(lock==0) { + switch(operand) { + + case "+": res+= value; + break; + + case "-": res-= value; + + break; + + case "*": res*= value; + break; + case "/": if(value==0) { + System.out.println("division by 0 is not possible"); + }else res/= value; + break; + + } + + } + + } + + cnt++; + } - } + return res; }