package de.hs_mannheim.informatik.spreadsheet; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * A simplified spreadsheet class for the PR1 programming lab at Hochschule Mannheim. * One aspect worth mentioning is that it only supports long numbers, not doubles. * * @author Oliver Hummel */ public class Spreadsheet { Cell[][] cells; /** * Constructor that creates a Spreadsheet of size rows * cols. * * @param rows number of rows * @param cols number of columns */ public Spreadsheet(int rows, int cols) { if(rows>99) rows = 99; else if(rows<1) rows = 1; if(cols < 1) cols = 1; else if(cols > 26) cols = 26; cells = new Cell[rows][cols]; for (int r = 0; r < rows; r++) for (int c = 0; c < cols; c++) cells[r][c] = new Cell(); } // ----- // retrieve or change values of cells private String get(int row, int col) { return cells[row][col].getValue(); } public String get(String cellName) { cellName = cellName.toUpperCase(); return get(getRow(cellName), getCol(cellName)); } public String getCellName(int row, int col) { return "" + ((char) ('A' + col)) + (row+1); } private void put(int row, int col, String value) { if (!value.startsWith("=")) cells[row][col].setValue(value); else { cells[row][col].setFormula(value); evaluateCell(row, col); } } public void put(String cellName, String value) { cellName = cellName.toUpperCase(); put(getRow(cellName), getCol(cellName), value); } private int getCol(String cellName) { return cellName.charAt(0) - 'A'; } private int getRow(String cellName) { if(cellName.length()>2) return Integer.parseInt(""+cellName.charAt(1)+cellName.charAt(2)) - 1; return cellName.charAt(1) - '1'; } // ----- // business logic /** * A method for reading in data from a CSV file. * * @param path The file to read. * @param separator The char used to split up the input, e.g. a comma or a semicolon. * @param starCellName The upper left cell where data from the CSV file should be inserted. * @return Nothing. * @throws IOException If path does not exist. */ public void readCsv(String path, char separator, String startCellName) throws FileNotFoundException { Scanner sc = new Scanner(new File(path)); ArrayList memList = new ArrayList<>(); while (sc.hasNextLine()) { memList.add(sc.nextLine()); } String[][] memArr = new String[memList.size()][]; for(int i = 0; i < memList.size(); i++) memArr[i] = (memList.get(i)).split(String.valueOf(separator)); for (int r = getRow(startCellName); r < memList.size(); r++) for (int c = getCol(startCellName); c < memList.get(0).length(); c++) if(c 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) -> größter Wert result = "TODO"; // TODO else if (formula.startsWith("MAX(")) // e.g. MAX(A1:A10) -> Standardabweichung result = "TODO"; // TODO else if (!formula.isEmpty()) { try { result = "" + calculate(formula); } catch (ArithmeticException ae) { result = "exc."; } } cells[row][col].setValue(result); } /** * Method for calculating the sum of a rectangular block of cells, such as from A1 to B3. * * @param startCellName The name of the cell in the upper left corner of the rectangle. * @param endCellName The name of the cell in the lower right corner of the rectangle. * @return The sum calculated. */ private long sum(String startCellName, String endCellName) { long result = 0; for(int r = getRow(startCellName); r cellNames = new ArrayList<>(); long avg = 0; int counter = 0; for(int r = getRow(startCellName); r copyCellNames = new ArrayList<>(cellNames); double[] frequency = new double[cellNames.size()]; Arrays.fill(frequency,1); ArrayList relativeFrequency = new ArrayList<>(); double mem = 0; for(int i = 0; i< cellNames.size(); i++) { for(int t = 0; t