package de.hs_mannheim.informatik.spreadsheet; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; 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 { int rows=0; int cols=0; 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) { this.rows=Math.min(rows, 99); this.cols=Math.min(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) { if(containsOnlyNumbers(cellName)) { return cellName; } else { cellName = cellName.toUpperCase(); return get(getRow(cellName), getCol(cellName)); } } 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); } public int getCol(String cellName) { return cellName.charAt(0) - 'A'; } public int getRow(String cellName) { if(cellName.length()>2) { int tenDigit=cellName.charAt(1)-'0'; int oneDigit=cellName.charAt(2)-'0'; int combined= tenDigit*10+oneDigit; return combined -1; //adjust for indexing at 0 } else { 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. * @return Nothing. * @exception IOException If path does not exist. */ public void readCsv(String path, String separator) throws IOException { BufferedReader reader = new BufferedReader(new FileReader(path)); for(int row=0;row Standardabweichung result = "TODO"; // TODO 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); //calculates the result of a "normal" algebraic expression. } 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) { int werte[]=getValues(startCellName,endCellName); int summe=0; for(int i=0;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; } // ----- /** * This method checks if a given string contains one or more digits * to make sure that the string consists of only numbers, * @param input The input that is getting checked for only numbers * @return returns true when the string contains only numbers */ public boolean containsOnlyNumbers(String input) { return input.matches("[0-9]+"); } public String toString() { StringBuilder sb = new StringBuilder(); sb.append(" "); for (int i = 0; i < cells[0].length; i++) { sb.append(" " + (char)('A'+ i) + " | "); } int rc = 1; for (Cell[] r : cells) { sb.append(System.lineSeparator()); //newLine sb.append(String.format("%2s", rc++) + ": "); //Number: for (Cell c : r) { sb.append(c + " | "); //cell value } } return sb.toString(); } //TODO: Javadoc Comment!! public boolean checkIfCellExists(String cellName) { if((getRow(cellName)+1)<=cells.length&&(getCol(cellName)+1)<=cells[0].length) { return true; } else { System.err.println("Zelle '"+ cellName+ "' existiert nicht!"); return false; } } }