From 3f4f1d978acec92b970acd44343bf30ecf6645eb Mon Sep 17 00:00:00 2001 From: 3012330 <3012330@stud.hs-mannheim.de> Date: Fri, 5 Jan 2024 22:26:43 +0100 Subject: [PATCH] Methode "calculate" geht jetzt --- .../informatik/spreadsheet/Spreadsheet.java | 63 ++++++++++++++++++- 1 file changed, 61 insertions(+), 2 deletions(-) diff --git a/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java b/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java index a9c85e2..c838f84 100644 --- a/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java +++ b/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java @@ -198,7 +198,7 @@ public class Spreadsheet { result = "TODO"; // TODO else if (!formula.isEmpty()) { try { - result = "" + calculate(formula); + result = "" + calculate(formula); //geht nicht? } catch(ArithmeticException ae) { result = "exc."; } @@ -234,18 +234,77 @@ public class Spreadsheet { long res = 0; // TODO implement + int merker = 0; + int zaehler = 0; + int neue_zahl = 0; // 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); + zaehler++; + + switch (zaehler){ + case 1: + try { + res = Integer.parseInt(s); + }catch (Exception ignored){ + res = Integer.parseInt(get(s)); + } + break; + case 2: + switch(s) { + case "+": + merker = 1; + break; + case "-": + merker = 2; + break; + case "*": + merker = 3; + break; + case "/": + merker = 4; + break; + default: + System.out.println("ERROR"); + break; + } + break; + case 3: + try { + neue_zahl = Integer.parseInt(s); + }catch (Exception ignored){ + neue_zahl = Integer.parseInt(get(s)); + } + res = rechne(res, merker, neue_zahl); + + zaehler = 1; + break; + } } } - return res; } + private long rechne(long a, int merker, int b) { + switch(merker){ + case 1: + return a + b; + case 2: + return a - b; + case 3: + return a * b; + case 4: + return a / b; + default: + System.out.println("ERROR"); + break; + } + return 0; + } + // ----- public String toString() {