diff --git a/Axel/resources/zahlen.csv b/Axel/resources/zahlen.csv index a50780e..8e8852f 100644 --- a/Axel/resources/zahlen.csv +++ b/Axel/resources/zahlen.csv @@ -1,10 +1,10 @@ -,,,,,,,,, +13,=69+420,,,,,,,, 1,,,,,,,,, 123,,,,,,,,, ,,,,,,,,, -,,,,,,,,,=7*6 -,,,,,,,,,=3/2 ,,,,,,,,, ,,,,,,,,, -,=41+A2,,,,,,,, +,,,,,,,,, +,,,,,,,,, +,,,,,,,,, ,,,,,,,,, diff --git a/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java b/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java index ceacf6f..c4a396c 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.util.ArrayList; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -39,8 +40,14 @@ public class Spreadsheet { } public String get(String cellName) { - cellName = cellName.toUpperCase(); - return get(getRow(cellName), getCol(cellName)); + if(containsOnlyNumbers(cellName)) { + return cellName; + } + else { + cellName = cellName.toUpperCase(); + return get(getRow(cellName), getCol(cellName)); + } + } private void put(int row, int col, String value) { @@ -128,7 +135,7 @@ public class Spreadsheet { result = "TODO"; // TODO else if (!formula.isEmpty()) { try { - result = "" + calculate(formula); + result = "" + calculate(formula); //calculates the result of a "normal" algebraic expression. } catch(ArithmeticException ae) { result = "exc."; } @@ -149,35 +156,75 @@ 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 { - Matcher m = Pattern.compile("([A-Z][0-9]*)|[-\\+\\*/]|[0-9]*").matcher(formula); - - long res = 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); + /** + * 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); + } } - } - return res; - } + int ersteZahl = Integer.parseInt(get(zahlen.get(0))); + + int zweiteZahl=0; + + for (int i = 0; i < zahlen.size(); i+=2) { + if(i