diff --git a/Axel/src/de/hs_mannheim/informatik/spreadsheet/Axel.java b/Axel/src/de/hs_mannheim/informatik/spreadsheet/Axel.java index 41c5da2..39d4cdf 100644 --- a/Axel/src/de/hs_mannheim/informatik/spreadsheet/Axel.java +++ b/Axel/src/de/hs_mannheim/informatik/spreadsheet/Axel.java @@ -1,6 +1,7 @@ package de.hs_mannheim.informatik.spreadsheet; import java.io.FileNotFoundException; +import java.util.Scanner; /** * Part of a simplified spreadsheet system for the PR1 programming lab at Hochschule Mannheim. @@ -10,16 +11,20 @@ import java.io.FileNotFoundException; public class Axel { public static void main(String[] args) throws FileNotFoundException { - Spreadsheet spr = new Spreadsheet(10,10); + Spreadsheet spr = new Spreadsheet(10, 10); + Scanner sc = new Scanner(System.in); - spr.cells[2][7].setFormula("=5+3"); - spr.updateSpreadsheet(); + while (true) { - - System.out.println(spr); - - // TODO: You might want to put "UI loop" for entering value and formulas here resp. in some UI methods. + spr.cells[sc.nextInt()-1][5].setFormula(sc.next()); + spr.updateSpreadsheet(); + + + System.out.println(spr); + + + // 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 68dc7c5..b86d043 100644 --- a/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java +++ b/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java @@ -151,67 +151,89 @@ public class Spreadsheet { String formula = cells[row][col].getFormula(); String result = ""; - String substringStartCell1 = formula.substring(formula.length() - 6, formula.length() - 4); - String substringStart2 = formula.substring(formula.length() - 6, formula.length() - 3); - String substringEndCell = formula.substring(formula.length() - 3, formula.length() - 1); + if(formula.length()>10) { + String substringStartCell1 = formula.substring(formula.length() - 6, formula.length() - 4); + String substringStart2 = formula.substring(formula.length() - 6, formula.length() - 3); + String substringEndCell = formula.substring(formula.length() - 3, formula.length() - 1); + + if (formula.startsWith("SUMME(")) // e.g. SUMME(A3:A8) + if (formula.length() < 13) + result = "" + sum(substringStartCell1, substringEndCell); + else if (formula.length() < 14 && substringStart2.contains(":")) + result = "" + sum(substringStartCell1, substringEndCell); + else if (formula.length() < 14 && !substringStart2.contains(":")) + result = "" + sum(substringStart2, substringEndCell); + else if (formula.length() < 15) + result = "" + sum(substringStart2, substringEndCell); + else + result = "0"; - if (formula.startsWith("SUMME(")) // e.g. SUMME(A3:A8) - if(formula.length()<13) - result = "" + sum(substringStartCell1, substringEndCell); - else if(formula.length()<14&&substringStart2.contains(":")) - result = "" + sum(substringStartCell1, substringEndCell); - else if(formula.length()<14&&!substringStart2.contains(":")) - result = "" + sum(substringStart2, substringEndCell); - else if(formula.length()<15) - result = "" + sum(substringStart2, substringEndCell); - else - result = "0"; + else if (formula.startsWith("PRODUKT(")) // e.g. PRODUKT(A3:B9) + if (formula.length() < 15) + result = "" + product(substringStartCell1, substringEndCell); + else if (formula.length() < 16 && substringStart2.contains(":")) + result = "" + product(substringStartCell1, substringEndCell); + else if (formula.length() < 16 && !substringStart2.contains(":")) + result = "" + product(substringStart2, substringEndCell); + else if (formula.length() < 17) + result = "" + product(substringStart2, substringEndCell); + else + result = "0"; - else if (formula.startsWith("PRODUKT(")) // e.g. PRODUKT(A3:B9) - if(formula.length()<15) - result = "" + product(substringStartCell1, substringEndCell); - else if(formula.length()<16&&substringStart2.contains(":")) - result = "" + product(substringStartCell1, substringEndCell); - else if(formula.length()<16&&!substringStart2.contains(":")) - result = "" + product(substringStart2, substringEndCell); - else if(formula.length()<17) - result = "" + product(substringStart2, substringEndCell); - else - result = "0"; + else if (formula.startsWith("MITTELWERT(")) // e.g. MITTELWERT(A3:A5) + if (formula.length() < 17) + result = "" + average(substringStartCell1, substringEndCell); + else if (formula.length() < 18 && substringStart2.contains(":")) + result = "" + average(substringStartCell1, substringEndCell); + else if (formula.length() < 18 && !substringStart2.contains(":")) + result = "" + average(substringStart2, substringEndCell); + else if (formula.length() < 19) + result = "" + average(substringStart2, substringEndCell); + else + result = "0"; - else if (formula.startsWith("MITTELWERT(")) // e.g. MITTELWERT(A3:A5) - if(formula.length()<17) - result = "" + average(substringStartCell1, substringEndCell); - else if(formula.length()<18&&substringStart2.contains(":")) - result = "" + average(substringStartCell1, substringEndCell); - else if(formula.length()<18&&!substringStart2.contains(":")) - result = "" + average(substringStart2, substringEndCell); - else if(formula.length()<19) - result = "" + average(substringStart2, substringEndCell); - else - result = "0"; + else if (formula.startsWith("STABW(")) // e.g. STABW(C6:D8) -> Standardabweichung + if (formula.length() < 13) + result = "" + standardDeviation(substringStartCell1, substringEndCell); + else if (formula.length() < 14 && substringStart2.contains(":")) + result = "" + standardDeviation(substringStartCell1, substringEndCell); + else if (formula.length() < 14 && !substringStart2.contains(":")) + result = "" + standardDeviation(substringStart2, substringEndCell); + else if (formula.length() < 15) + result = "" + standardDeviation(substringStart2, substringEndCell); + else + result = "0"; - else if (formula.startsWith("STABW(")) // e.g. STABW(C6:D8) -> Standardabweichung - if(formula.length()<13) - result = "" + standardDeviation(substringStartCell1, substringEndCell); - else if(formula.length()<14&&substringStart2.contains(":")) - result = "" + standardDeviation(substringStartCell1, substringEndCell); - else if(formula.length()<14&&!substringStart2.contains(":")) - result = "" + standardDeviation(substringStart2, substringEndCell); - else if(formula.length()<15) - result = "" + standardDeviation(substringStart2, substringEndCell); - else - result = "0"; + else if (formula.startsWith("MIN(")) // e.g. MIN(C13:H13) -> kleinster Wert + if (formula.length() < 11) + result = "" + min(substringStartCell1, substringEndCell); + else if (formula.length() < 12 && substringStart2.contains(":")) + result = "" + min(substringStartCell1, substringEndCell); + else if (formula.length() < 12 && !substringStart2.contains(":")) + result = "" + min(substringStart2, substringEndCell); + else if (formula.length() < 13) + result = "" + min(substringStart2, substringEndCell); + else + result = "0"; - else if (formula.startsWith("MIN(")) // e.g. MIN(C13:H13) -> größter Wert - result = "TODO"; // TODO - else if (formula.startsWith("MAX(")) // e.g. MAX(A1:A10) -> Standardabweichung - result = "TODO"; // TODO + else if (formula.startsWith("MAX(")) // e.g. MAX(A1:A10) -> größter Wert + if (formula.length() < 11) + result = "" + max(substringStartCell1, substringEndCell); + else if (formula.length() < 12 && substringStart2.contains(":")) + result = "" + max(substringStartCell1, substringEndCell); + else if (formula.length() < 12 && !substringStart2.contains(":")) + result = "" + max(substringStart2, substringEndCell); + else if (formula.length() < 13) + result = "" + max(substringStart2, substringEndCell); + else + result = "0"; + } + else if (!formula.isEmpty()) { try { result = "" + calculate(formula); @@ -252,8 +274,8 @@ public class Spreadsheet { return result; } - private long average(String startCellName, String endCellName){ - long result = 0; + private double average(String startCellName, String endCellName){ + double result = 0; int counter = 0; for(int r = getRow(startCellName); r copyCellNames = new ArrayList<>(cellNames); double[] frequency = new double[cellNames.size()]; @@ -330,6 +351,38 @@ public class Spreadsheet { return Math.sqrt(mem); //standardDeviation formula } + private long min(String startCellName, String endCellName){ + ArrayList cellNames = new ArrayList<>(); + + for(int r = getRow(startCellName); rLong.parseLong(get(cellNames.get(i)))) + result = Long.parseLong(get(cellNames.get(i))); + + return result; + } + private long max(String startCellName, String endCellName){ + ArrayList cellNames = new ArrayList<>(); + + for(int r = getRow(startCellName); r