From f9983c112e7cd6366ce755ebf0f55f40238cea0a Mon Sep 17 00:00:00 2001 From: devra Date: Sun, 7 Jan 2024 14:30:17 +0100 Subject: [PATCH] Fehlererkennung beim eingeben von Falschen Formeln implementiert --- .../informatik/spreadsheet/Axel.java | 59 +++++++-- .../informatik/spreadsheet/Spreadsheet.java | 116 +++++++++--------- 2 files changed, 107 insertions(+), 68 deletions(-) diff --git a/Axel/src/de/hs_mannheim/informatik/spreadsheet/Axel.java b/Axel/src/de/hs_mannheim/informatik/spreadsheet/Axel.java index 4f4ac59..90261ee 100644 --- a/Axel/src/de/hs_mannheim/informatik/spreadsheet/Axel.java +++ b/Axel/src/de/hs_mannheim/informatik/spreadsheet/Axel.java @@ -1,6 +1,8 @@ package de.hs_mannheim.informatik.spreadsheet; import java.io.FileNotFoundException; +import java.io.IOException; +import java.util.Scanner; /** * Part of a simplified spreadsheet system for the PR1 programming lab at Hochschule Mannheim. @@ -10,27 +12,60 @@ import java.io.FileNotFoundException; public class Axel { public static void main(String[] args) throws FileNotFoundException { + Scanner scanner = new Scanner(System.in); Spreadsheet spr = new Spreadsheet(10,10); System.out.println(); - spr.put("A1", "13"); - spr.put("A2", "1"); - spr.put("A3", "123"); +// spr.put("A1", "13"); +// spr.put("A2", "1"); +// spr.put("A3", "123"); +// +// spr.put("B1", "=3"); +// spr.put("c1", "=A2-A1+30"); +// spr.put("B9", "=41+A1"); +// spr.put("J5", "=7*6/5"); +// spr.put("J6", "=3/2"); - spr.put("B1", "=A2"); - spr.put("c1", "=A2-A1+30"); - spr.put("B9", "=41+A1"); - spr.put("J5", "=7*6/5"); - spr.put("J6", "=3/2"); - System.out.println(spr); - + int test=1; + System.out.println("Wilkommen zum coolsten Spreadsheet Programm der Welt!"); + System.out.println("Beispiel-Formel: 'B2=15+5' "); - //spr.saveCsv("C:\\Users\\devra\\git\\Axel\\Axel\\resources\\zahlen.csv"); + while(test==1) { + System.out.println("Geben sie ihre Formel ein: "); + System.out.print("-> "); + String userInput = scanner.nextLine(); + + if(userInput.matches("[A-Za-z][1-9][0-9]?=" + + "((((([A-Za-z][1-9][0-9]?)|([0-9]+))(\\*|\\+|\\-|\\/))*" + + "(([A-Za-z][1-9][0-9]?)|([0-9]+)))|" + + "((SUMME|PRODUKT|MID|STABW|MIN|MAX)\\(([A-Za-z][1-9][0-9]*\\:[A-Za-z][1-9][0-9]*)\\)))")) + { + String[] parts = userInput.split("="); + spr.put(parts[0],"="+ parts[1]); + +// System.out.println(parts[0]); + System.out.println(spr); + } + else { + System.out.println("Formel ist nicht valide! "); + + } + + +// System.out.println("Was möchten sie machen?"); +// +// System.out.println("1. Dokument öffnen"); +// System.out.println("2. neues Dokument erstellen"); + + + } + + spr.saveCsv("C:\\Users\\devra\\git\\Axel\\Axel\\resources\\zahlen.csv"); // 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 c4a396c..224b329 100644 --- a/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java +++ b/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java @@ -69,7 +69,15 @@ public class Spreadsheet { } private int getRow(String cellName) { - return cellName.charAt(1) - '1'; + if(cellName.length()>2) { + int tenDigit=cellName.charAt(1)-'0'; + int oneDigit=cellName.charAt(2)-'0'; + int combined= tenDigit*10+oneDigit; + return combined -1; + } + else { + return cellName.charAt(1) - '1'; //adjust for indexing at 0 + } } // ----- @@ -156,64 +164,60 @@ public class Spreadsheet { return 0; } - /** - * This method calculates the result of a "normal" algebraic expression. It only needs to support - * expressions like =B4 or =2+A3-B2, i.e. only with int numbers and other cells and with plus, - * minus, times, split only. An expression always starts with either a number or a cell name. If it - * continues, it is guaranteed that this is followed by an operator and either a number or a - * cell name again. It is NOT required to implement dot before dash or parentheses in formulas. - * @param formula The expression to be evaluated. - * @return The result calculated. - */ - private long calculate(String formula) throws ArithmeticException { - System.out.println(formula); - Matcher m = Pattern.compile("([A-Z][0-9]*)|[-\\+\\*/]|[0-9]*").matcher(formula); - - - ArrayList zahlen = new ArrayList<>(); - - while(m.find()) { - String s = m.group(); - if (!s.isEmpty()) { - zahlen.add(s); - } - } + /** + * This method calculates the result of a "normal" algebraic expression. It only needs to support + * expressions like =B4 or =2+A3-B2, i.e. only with int numbers and other cells and with plus, + * minus, times, split only. An expression always starts with either a number or a cell name. If it + * continues, it is guaranteed that this is followed by an operator and either a number or a + * cell name again. It is NOT required to implement dot before dash or parentheses in formulas. + * @param formula The expression to be evaluated. + * @return The result calculated. + */ + private long calculate(String formula) throws ArithmeticException { + //System.out.println(formula); + Matcher m = Pattern.compile("([A-Z][0-9]*)|[-\\+\\*/]|[0-9]*").matcher(formula); - int ersteZahl = Integer.parseInt(get(zahlen.get(0))); - - int zweiteZahl=0; - - for (int i = 0; i < zahlen.size(); i+=2) { - if(i zahlen = new ArrayList<>(); + + while(m.find()) { + String s = m.group(); + if (!s.isEmpty()) { + zahlen.add(s); + } } + + int ersteZahl = Integer.parseInt(get(zahlen.get(0))); + int zweiteZahl=0; + + for (int i = 0; i < zahlen.size()-2; i+=2) { + String operant=zahlen.get(i+1); + zweiteZahl=Integer.parseInt(get(zahlen.get(i+2))); + + switch(operant) { + case "+": + ersteZahl=ersteZahl+zweiteZahl; + break; + + case "-": + ersteZahl=ersteZahl-zweiteZahl; + break; + + case "*": + ersteZahl=ersteZahl*zweiteZahl; + break; + + case "/": + ersteZahl=ersteZahl/zweiteZahl; + break; + } + } + + + return ersteZahl; + } + // -----