Compare commits
25 Commits
Author | SHA1 | Date |
---|---|---|
Jan Bachmann | ba4469b358 | |
Jan Bachmann | fe45bcf50c | |
Jan Bachmann | e0344e5a1c | |
Jan Bachmann | 3d6e5482ec | |
Jan Bachmann | f09c112e2c | |
Jan Bachmann | 1aa39e7344 | |
Jan Bachmann | cf3be4d319 | |
Jan Bachmann | 21f9658355 | |
Jan Bachmann | a55fc4db6b | |
Jan Bachmann | dd011a83b6 | |
Jan Bachmann | 5307cd9e8f | |
Jan | ee9ea1374d | |
Jan | d950b04be5 | |
Jan | 883c36c929 | |
Jan | e5d531a7a4 | |
Jan | f5c29329fe | |
Jan | 95081cb010 | |
Dr.Janson | f10415dec8 | |
Jan | baa6d8a092 | |
Jan | 910e35dd0c | |
Dr.Janson | aba1d8bf54 | |
Dr.Janson | 203a833bc6 | |
Dr.Janson | dcceb207a1 | |
Dr.Janson | deabe361d4 | |
Dr.Janson | 98026d2e20 |
|
@ -1,4 +1,10 @@
|
||||||
1,2
|
,24,,,,
|
||||||
3,4
|
,28,,,,
|
||||||
5,6
|
123,24,,,,
|
||||||
7,8
|
,24,,,,
|
||||||
|
,5,,,,
|
||||||
|
,5,,,,
|
||||||
|
,200,,,,
|
||||||
|
,200,,,,
|
||||||
|
,=41+A2+10-2/3,,,,
|
||||||
|
,,,,,
|
||||||
|
|
|
|
@ -1,6 +1,10 @@
|
||||||
package de.hs_mannheim.informatik.spreadsheet;
|
package de.hs_mannheim.informatik.spreadsheet;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.PrintWriter;
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Part of a simplified spreadsheet system for the PR1 programming lab at Hochschule Mannheim.
|
* Part of a simplified spreadsheet system for the PR1 programming lab at Hochschule Mannheim.
|
||||||
|
@ -10,20 +14,105 @@ import java.io.FileNotFoundException;
|
||||||
public class Axel {
|
public class Axel {
|
||||||
|
|
||||||
public static void main(String[] args) throws FileNotFoundException {
|
public static void main(String[] args) throws FileNotFoundException {
|
||||||
Spreadsheet spr = new Spreadsheet(10,10);
|
Spreadsheet spr = new Spreadsheet(10,6);
|
||||||
|
boolean UiLoop = true;
|
||||||
|
@SuppressWarnings("resource")
|
||||||
|
Scanner input = new Scanner(System.in);
|
||||||
|
String filePath = "Axel/resources/";
|
||||||
|
System.out.println("Wilkommen zum coolsten Spreadsheet Programm der Welt!(Cooler als Devran Cakici´s 3009694)");
|
||||||
|
|
||||||
spr.put("A3", "123");
|
System.out.println("Was möchten sie machen?");
|
||||||
spr.put("A2", "1");
|
|
||||||
|
|
||||||
spr.put("B9", "=41+A2");
|
System.out.println("\n 1. Dokument öffnen");
|
||||||
spr.put("J5", "=7*6");
|
System.out.println(" 2. Neues Dokument erstellen");
|
||||||
spr.put("J6", "=3/2");
|
System.out.println(" 3. Programm schließen");
|
||||||
|
System.out.print("-> ");
|
||||||
|
String docSlct = input.nextLine();
|
||||||
|
|
||||||
|
switch(docSlct) {
|
||||||
|
case "1":
|
||||||
|
File folder = new File(filePath);
|
||||||
|
|
||||||
|
if (folder.exists() && folder.isDirectory()) {
|
||||||
|
File[] files = folder.listFiles((dir, name) -> name.toLowerCase().endsWith(".csv"));
|
||||||
|
|
||||||
|
if (files != null) {
|
||||||
|
System.out.println("\nWähle eine Datei aus: ");
|
||||||
|
System.out.println("");
|
||||||
|
for (int i = 0; i < files.length; i++) {
|
||||||
|
System.out.println(i+1 + ": " + files[i].getName());
|
||||||
|
}
|
||||||
|
System.out.println();
|
||||||
|
spr.readCsv(filePath+=files[Integer.parseInt(input.nextLine())-1].getName(),"");
|
||||||
|
} else {
|
||||||
|
System.out.println("Es wurden noch keine Datei angelegt.");
|
||||||
|
}
|
||||||
|
}else {
|
||||||
|
System.out.println("Ungültiges Verzeichnis oder Verzeichnis existiert nicht.");
|
||||||
|
System.out.println("Geben sie hier ihr verzeichnis ein in dem sie Arbeiten möchten: ");
|
||||||
|
filePath = input.nextLine();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
case "2":
|
||||||
|
System.out.println("Reihen: ");
|
||||||
|
int rows = Integer.parseInt(input.nextLine());
|
||||||
|
System.out.println("Spalten: ");
|
||||||
|
int cols = Integer.parseInt(input.nextLine());
|
||||||
|
spr.changeSize(rows, cols);
|
||||||
|
break;
|
||||||
|
case "3":
|
||||||
|
UiLoop = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
while(UiLoop){
|
||||||
|
String command = "";
|
||||||
|
String form = "";
|
||||||
|
String cell= "";
|
||||||
|
boolean cellTRUE = true;
|
||||||
|
|
||||||
|
System.out.println("Schreibe 'stop' zum anhalten.");
|
||||||
System.out.println(spr);
|
System.out.println(spr);
|
||||||
|
System.out.println("Befehl (z.B. D4_=7*6): ");
|
||||||
spr.saveCsv("/tmp/test.csv");
|
command = input.nextLine();
|
||||||
|
if(command.matches("[A-Za-z][1-9][0-9]?_="
|
||||||
// TODO: You might want to put "UI loop" for entering value and formulas here resp. in some UI methods.
|
+ "((((([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]*)\\)))")) {
|
||||||
|
for(int i = 0; i<command.length();i++) {
|
||||||
|
if(command.charAt(i)=='_') {
|
||||||
|
cellTRUE = false;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if(cellTRUE) {
|
||||||
|
cell+=command.charAt(i);
|
||||||
|
}else {
|
||||||
|
form+=command.charAt(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if((spr.getRow(cell)+1)<=spr.cells.length&&(spr.getCol(cell)+1)<=spr.cells[0].length) {
|
||||||
|
spr.put(cell, form);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
System.err.println("Zelle '"+ cell+ "' existiert nicht!");
|
||||||
|
}
|
||||||
|
}else if(command.matches("stop")){
|
||||||
|
break;
|
||||||
|
}else if(command.startsWith("save")){
|
||||||
|
String csvName=command.substring(5)+".csv";
|
||||||
|
String savePath = "Axel/resources/";
|
||||||
|
try (PrintWriter writer = new PrintWriter(new FileOutputStream(savePath+csvName))) {
|
||||||
|
spr.saveCsv(writer);
|
||||||
|
System.out.println("CSV file saved successfully.");
|
||||||
|
} catch (FileNotFoundException e) {
|
||||||
|
System.err.println("Error: " + e.getMessage());
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}else {
|
||||||
|
System.out.println("Die Eingabe war nicht korrekt. Die Tabelle bleibt auf folgendem stand:");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -1,5 +1,5 @@
|
||||||
package de.hs_mannheim.informatik.spreadsheet;
|
package de.hs_mannheim.informatik.spreadsheet;
|
||||||
|
//test
|
||||||
/**
|
/**
|
||||||
* Part of a simplified spreadsheet system for the PR1 programming lab at Hochschule Mannheim.
|
* Part of a simplified spreadsheet system for the PR1 programming lab at Hochschule Mannheim.
|
||||||
* A cell needs to be able to hold a formula and a value
|
* A cell needs to be able to hold a formula and a value
|
||||||
|
|
|
@ -1,11 +1,14 @@
|
||||||
package de.hs_mannheim.informatik.spreadsheet;
|
package de.hs_mannheim.informatik.spreadsheet;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.PrintWriter;
|
import java.io.PrintWriter;
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Scanner;
|
||||||
/**
|
/**
|
||||||
* A simplified spreadsheet class for the PR1 programming lab at Hochschule Mannheim.
|
* 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.
|
* One aspect worth mentioning is that it only supports long numbers, not doubles.
|
||||||
|
@ -14,36 +17,61 @@ import java.util.regex.Pattern;
|
||||||
*/
|
*/
|
||||||
public class Spreadsheet {
|
public class Spreadsheet {
|
||||||
Cell[][] cells;
|
Cell[][] cells;
|
||||||
|
/*
|
||||||
/**
|
|
||||||
* Constructor that creates a Spreadsheet of size rows * cols.
|
* Constructor that creates a Spreadsheet of size rows * cols.
|
||||||
* @param rows number of rows
|
* @param rows number of rows
|
||||||
* @param cols number of columns
|
* @param cols number of columns
|
||||||
*/
|
*/
|
||||||
public Spreadsheet(int rows, int cols) {
|
public Spreadsheet(int rows, int cols) {
|
||||||
|
|
||||||
// TODO limit the maximum size on 99 (1..99) rows and 26 (A..Z) columns
|
int rowWithMax99= Math.min(rows, 99);
|
||||||
|
int colWithMax26= Math.min(cols, 26);
|
||||||
|
|
||||||
cells = new Cell[rows][cols];
|
cells = new Cell[rowWithMax99][colWithMax26];
|
||||||
|
|
||||||
for (int r = 0; r < rows; r++)
|
for (int r = 0; r < rowWithMax99; r++)
|
||||||
for (int c = 0; c < cols; c++)
|
for (int c = 0; c < colWithMax26; c++)
|
||||||
|
cells[r][c] = new Cell();
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Sets a Spreadsheet of size rows * cols.
|
||||||
|
* @param rows number of rows
|
||||||
|
* @param cols number of columns
|
||||||
|
*/
|
||||||
|
public void changeSize(int rows, int cols) {
|
||||||
|
cells = new Cell[0][0];
|
||||||
|
|
||||||
|
int rowWithMax99= Math.min(rows, 99);
|
||||||
|
int colWithMax26= Math.min(cols, 26);
|
||||||
|
|
||||||
|
cells = new Cell[rowWithMax99][colWithMax26];
|
||||||
|
|
||||||
|
for (int r = 0; r < rowWithMax99; r++)
|
||||||
|
for (int c = 0; c < colWithMax26; c++)
|
||||||
cells[r][c] = new Cell();
|
cells[r][c] = new Cell();
|
||||||
}
|
}
|
||||||
|
|
||||||
// -----
|
// -----
|
||||||
|
/*
|
||||||
|
* @param str A String with multiple numbers.
|
||||||
|
* @return true if there are ONLY numbers in this sting.
|
||||||
|
*/
|
||||||
|
private boolean isNumber(String str) {
|
||||||
|
return str.matches("[0-9]+");
|
||||||
|
}
|
||||||
|
private boolean isCar(String str) {//Brum
|
||||||
|
return str.matches("[-\\+\\*/]+");
|
||||||
|
}
|
||||||
// retrieve or change values of cells
|
// retrieve or change values of cells
|
||||||
|
|
||||||
private String get(int row, int col) {
|
private String get(int row, int col) {
|
||||||
return cells[row][col].getValue();
|
return cells[row][col].getValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
public String get(String cellName) {
|
public String get(String cellName) {
|
||||||
cellName = cellName.toUpperCase();
|
cellName = cellName.toUpperCase();
|
||||||
return get(getRow(cellName), getCol(cellName));
|
return get(getRow(cellName), getCol(cellName));
|
||||||
}
|
}
|
||||||
|
void put(int row, int col, String value) {
|
||||||
private void put(int row, int col, String value) {
|
|
||||||
if (!value.startsWith("="))
|
if (!value.startsWith("="))
|
||||||
cells[row][col].setValue(value);
|
cells[row][col].setValue(value);
|
||||||
else {
|
else {
|
||||||
|
@ -51,33 +79,57 @@ public class Spreadsheet {
|
||||||
evaluateCell(row, col);
|
evaluateCell(row, col);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void put(String cellName, String value) {
|
public void put(String cellName, String value) {
|
||||||
cellName = cellName.toUpperCase();
|
cellName = cellName.toUpperCase();
|
||||||
put(getRow(cellName), getCol(cellName), value);
|
put(getRow(cellName), getCol(cellName), value);
|
||||||
}
|
}
|
||||||
|
public int getCol(String cellName) {
|
||||||
private int getCol(String cellName) {
|
|
||||||
return cellName.charAt(0) - 'A';
|
return cellName.charAt(0) - 'A';
|
||||||
}
|
}
|
||||||
|
public int getRow(String cellName) {
|
||||||
private int getRow(String cellName) {
|
if (cellName.length()==3) {
|
||||||
|
int Row = 0;
|
||||||
|
Row +=((cellName.charAt(1)-'0')*10);
|
||||||
|
Row +=(cellName.charAt(2)-'0');
|
||||||
|
return Row-1;
|
||||||
|
}else {
|
||||||
return cellName.charAt(1) - '1';
|
return cellName.charAt(1) - '1';
|
||||||
}
|
}
|
||||||
|
}
|
||||||
// -----
|
// -----
|
||||||
// business logic
|
// business logic
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A method for reading in data from a CSV file.
|
* A method for reading in data from a CSV file.
|
||||||
* @param path The file to read.
|
* @param path The file to read.
|
||||||
* @param separator The char used to split up the input, e.g. a comma or a semicolon.
|
* @param startCellName The upper left cell where data from the CSV file should be inserted.
|
||||||
* @param starCellName The upper left cell where data from the CSV file should be inserted.
|
|
||||||
* @return Nothing.
|
* @return Nothing.
|
||||||
* @exception IOException If path does not exist.
|
* @exception IOException If path does not exist.
|
||||||
*/
|
*/
|
||||||
public void readCsv(String path, char separator, String startCellName) throws FileNotFoundException {
|
public void readCsv(String path, String startCellName) throws FileNotFoundException {
|
||||||
// TODO: implement this
|
Scanner csvIn = new Scanner(new File(path));
|
||||||
|
int index= 0;
|
||||||
|
|
||||||
|
while(csvIn.hasNextLine()){
|
||||||
|
String line =csvIn.nextLine();
|
||||||
|
String all="";
|
||||||
|
int filled=0;
|
||||||
|
for(int i = 0;i<line.length();i++) {
|
||||||
|
if(line.charAt(i)==',') {
|
||||||
|
if(!all.isBlank()) {
|
||||||
|
put(index,i-filled,all);
|
||||||
|
all="";
|
||||||
|
}
|
||||||
|
}else {
|
||||||
|
filled++;
|
||||||
|
all+=line.charAt(i);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
index++;
|
||||||
|
}
|
||||||
|
|
||||||
|
csvIn.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -86,8 +138,7 @@ public class Spreadsheet {
|
||||||
* @return Nothing.
|
* @return Nothing.
|
||||||
* @exception IOException If path does not exist.
|
* @exception IOException If path does not exist.
|
||||||
*/
|
*/
|
||||||
public void saveCsv(String path) throws FileNotFoundException {
|
public void saveCsv(PrintWriter out) throws FileNotFoundException {
|
||||||
PrintWriter out = new PrintWriter(path);
|
|
||||||
|
|
||||||
for (Cell[] row : cells) {
|
for (Cell[] row : cells) {
|
||||||
for (Cell cell : row) {
|
for (Cell cell : row) {
|
||||||
|
@ -113,20 +164,32 @@ public class Spreadsheet {
|
||||||
private void evaluateCell(int row, int col) {
|
private void evaluateCell(int row, int col) {
|
||||||
String formula = cells[row][col].getFormula();
|
String formula = cells[row][col].getFormula();
|
||||||
String result = "";
|
String result = "";
|
||||||
|
int offset = 0;
|
||||||
if (formula.startsWith("SUMME(")) // e.g. SUMME(A3:A8)
|
int offsetEnd = 0;
|
||||||
result = "" + sum(formula.substring(6, 8), formula.substring(9, 11)); // TODO adapt to cells with two digits
|
int diff=0;
|
||||||
else if (formula.startsWith("PRODUKT(")) // e.g. PRODUKT(A3:B9)
|
for(int i = 0; i<formula.length();i++) {
|
||||||
result = "TODO"; // TODO
|
if(formula.charAt(i)=='(') {
|
||||||
else if (formula.startsWith("MITTELWERT(")) // e.g. MITTELWERT(A3:A5)
|
offset=i+1;
|
||||||
result = "TODO"; // TODO
|
}else if(formula.charAt(i)==')') {
|
||||||
else if (formula.startsWith("STABW(")) // e.g. STABW(C6:D8) -> Standardabweichung
|
offsetEnd=i;
|
||||||
result = "TODO"; // TODO
|
}
|
||||||
else if (formula.startsWith("MIN(")) // e.g. MIN(C13:H13) -> größter Wert
|
else if(formula.charAt(i)==':') {
|
||||||
result = "TODO"; // TODO
|
diff=i;
|
||||||
else if (formula.startsWith("MAX(")) // e.g. MAX(A1:A10) -> Standardabweichung
|
}
|
||||||
result = "TODO"; // TODO
|
}
|
||||||
else if (!formula.isEmpty()) {
|
if (formula.startsWith("SUMME(")) { // e.g. SUMME(A3:A8)
|
||||||
|
result += sum(formula.substring(offset, diff), formula.substring(diff+1,offsetEnd));
|
||||||
|
}else if (formula.startsWith("PRODUKT(")) { // e.g. PRODUKT(A3:B9)
|
||||||
|
result += prod(formula.substring(offset, diff), formula.substring(diff+1, offsetEnd));
|
||||||
|
}else if (formula.startsWith("MID(")) { // e.g. MITTELWERT(A3:A5)
|
||||||
|
result += mid(formula.substring(offset, diff), formula.substring(diff+1, offsetEnd));
|
||||||
|
}else if (formula.startsWith("STABW(")) { // e.g. STABW(C6:D8) -> Standardabweichung
|
||||||
|
result += stabw(formula.substring(offset, diff), formula.substring(diff+1, offsetEnd));
|
||||||
|
}else if (formula.startsWith("MIN(")) {// e.g. MIN(C13:H13) -> kleinster Wert
|
||||||
|
result += min(formula.substring(offset, diff), formula.substring(diff+1, offsetEnd));
|
||||||
|
}else if (formula.startsWith("MAX(")) { // e.g. MAX(A1:A10) -> größter Wert
|
||||||
|
result += max(formula.substring(offset, diff), formula.substring(diff+1, offsetEnd));
|
||||||
|
}else if (!formula.isEmpty()) {
|
||||||
try {
|
try {
|
||||||
result = "" + calculate(formula);
|
result = "" + calculate(formula);
|
||||||
} catch(ArithmeticException ae) {
|
} catch(ArithmeticException ae) {
|
||||||
|
@ -143,12 +206,145 @@ public class Spreadsheet {
|
||||||
* @param endCellName The name of the cell in the lower right corner of the rectangle.
|
* @param endCellName The name of the cell in the lower right corner of the rectangle.
|
||||||
* @return The sum calculated.
|
* @return The sum calculated.
|
||||||
*/
|
*/
|
||||||
private long sum(String startCellName, String endCellName) {
|
public long sum(String startCellName, String endCellName) {
|
||||||
// TODO implement
|
long res = 0;
|
||||||
|
for(int j = getRow(startCellName); j<= (getRow(endCellName));j++){
|
||||||
|
for(int i = getCol(startCellName); i<= (getCol(endCellName));i++) {
|
||||||
|
if(!cells[j][i].getValue().isBlank()) {
|
||||||
|
res += Long.parseLong(cells[j][i].getValue());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Method for calculating the product 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 product calculated.
|
||||||
|
*/
|
||||||
|
public long prod(String startCellName, String endCellName) {
|
||||||
|
long res = 1;
|
||||||
|
boolean anyNumbers= false;
|
||||||
|
for(int j = getRow(startCellName); j<= (getRow(endCellName));j++){
|
||||||
|
for(int i = getCol(startCellName); i<= (getCol(endCellName));i++) {
|
||||||
|
if(!cells[j][i].getValue().isBlank()) {
|
||||||
|
res*= Long.parseLong(cells[j][i].getValue());
|
||||||
|
anyNumbers = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(anyNumbers)
|
||||||
|
return res;
|
||||||
|
else
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* Method for calculating the Mid Value 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 Mid Value calculated.
|
||||||
|
*/
|
||||||
|
public long mid(String startCellName, String endCellName) {
|
||||||
|
long res = 0;
|
||||||
|
int Numbers= 0;
|
||||||
|
for(int j = getRow(startCellName); j<= (getRow(endCellName));j++){
|
||||||
|
for(int i = getCol(startCellName); i<= (getCol(endCellName));i++) {
|
||||||
|
if(!cells[j][i].getValue().isBlank()) {
|
||||||
|
res+= Long.parseLong(cells[j][i].getValue());
|
||||||
|
Numbers++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
res/=Numbers;
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Method for calculating the Standard deviation 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 Standard deviation calculated.
|
||||||
|
*/
|
||||||
|
public long stabw(String startCellName, String endCellName) {
|
||||||
|
long res=0;
|
||||||
|
long midSum=0;
|
||||||
|
//mid(startCellName,endCellName)
|
||||||
|
//mid ist nicht verwendbar weil wir die anzahl der Nummern sowieso brauchen
|
||||||
|
int numbers= 0;
|
||||||
|
for(int j = getRow(startCellName); j<= (getRow(endCellName));j++){
|
||||||
|
for(int i = getCol(startCellName); i<= (getCol(endCellName));i++) {
|
||||||
|
if(!cells[j][i].getValue().isBlank()) {
|
||||||
|
midSum+= Long.parseLong(cells[j][i].getValue());
|
||||||
|
numbers++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
double midVal = (double) midSum / numbers;
|
||||||
|
|
||||||
|
double sum = 0.0;
|
||||||
|
for (int j = getRow(startCellName); j <= getRow(endCellName); j++) {
|
||||||
|
for (int i = getCol(startCellName); i <= getCol(endCellName); i++) {
|
||||||
|
if (!cells[j][i].getValue().isBlank()) {
|
||||||
|
long value = Long.parseLong(cells[j][i].getValue());
|
||||||
|
sum += Math.pow(value - midVal, 2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
double stabw = Math.sqrt(sum / numbers);
|
||||||
|
return (long) stabw;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Method for calculating the minimum value 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 minimum value calculated.
|
||||||
|
*/
|
||||||
|
public long min(String startCellName, String endCellName) {
|
||||||
|
long res = 0;
|
||||||
|
List<Long> args = new ArrayList<>();
|
||||||
|
for(int j = getRow(startCellName); j<= (getRow(endCellName));j++){
|
||||||
|
for(int i = getCol(startCellName); i<= (getCol(endCellName));i++) {
|
||||||
|
if(!cells[j][i].getValue().isBlank()) {
|
||||||
|
args.add(Long.parseLong(cells[j][i].getValue()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(args.size()>0) {
|
||||||
|
res=args.get(0);
|
||||||
|
}
|
||||||
|
for(int i = 0; i<args.size();i++) {
|
||||||
|
if(res>args.get(i)) {
|
||||||
|
res=args.get(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Method for calculating the maximum value 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 maximum value calculated.
|
||||||
|
*/
|
||||||
|
public long max(String startCellName, String endCellName) {
|
||||||
|
long res = 0;
|
||||||
|
List<Long> args = new ArrayList<>();
|
||||||
|
for(int j = getRow(startCellName); j<= (getRow(endCellName));j++){
|
||||||
|
for(int i = getCol(startCellName); i<= (getCol(endCellName));i++) {
|
||||||
|
if(!cells[j][i].getValue().isBlank()) {
|
||||||
|
args.add(Long.parseLong(cells[j][i].getValue()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(args.size()>0) {
|
||||||
|
res=args.get(0);
|
||||||
|
}
|
||||||
|
for(int i = 0; i<args.size();i++) {
|
||||||
|
if(res<args.get(i)) {
|
||||||
|
res=args.get(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* This method calculates the result of a "normal" algebraic expression. It only needs to support
|
* 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,
|
* expressions like =B4 or =2+A3-B2, i.e. only with int numbers and other cells and with plus,
|
||||||
|
@ -161,19 +357,51 @@ public class Spreadsheet {
|
||||||
private long calculate(String formula) throws ArithmeticException {
|
private long calculate(String formula) throws ArithmeticException {
|
||||||
Matcher m = Pattern.compile("([A-Z][0-9]*)|[-\\+\\*/]|[0-9]*").matcher(formula);
|
Matcher m = Pattern.compile("([A-Z][0-9]*)|[-\\+\\*/]|[0-9]*").matcher(formula);
|
||||||
|
|
||||||
long res = 0;
|
List<String> args = new ArrayList<>();
|
||||||
|
|
||||||
// TODO implement
|
while (m.find()) {
|
||||||
|
if(!m.group().isEmpty()) {
|
||||||
// uncomment the following to see an example how the elements of a formula can be accessed
|
if(isNumber(m.group())||isCar(m.group())) {
|
||||||
while (m.find()) { // m.find() must always be used before m.group()
|
args.add(m.group());
|
||||||
String s = m.group();
|
}else {
|
||||||
if (!s.isEmpty()) {
|
if(cells[getRow(m.group())][getCol(m.group())].getValue().isEmpty())
|
||||||
System.out.println(s);
|
args.add("0");
|
||||||
|
else
|
||||||
|
args.add(m.group());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return res;
|
for(int i = 2; i<args.size();) {
|
||||||
|
String val = "";
|
||||||
|
switch(args.get(i-1)) {
|
||||||
|
case "+":
|
||||||
|
val =""+ (Integer.parseInt(args.get(i-2))+Integer.parseInt(args.get(i)));
|
||||||
|
args.set(i,val);
|
||||||
|
args.remove(i-1);
|
||||||
|
args.remove(i-2);
|
||||||
|
break;
|
||||||
|
case "-":
|
||||||
|
val =""+ (Integer.parseInt(args.get(i-2))-Integer.parseInt(args.get(i)));
|
||||||
|
args.set(i,val);
|
||||||
|
args.remove(i-1);
|
||||||
|
args.remove(i-2);
|
||||||
|
break;
|
||||||
|
case "*" :
|
||||||
|
val =""+ (Integer.parseInt(args.get(i-2))*Integer.parseInt(args.get(i)));
|
||||||
|
args.set(i,val);
|
||||||
|
args.remove(i-1);
|
||||||
|
args.remove(i-2);
|
||||||
|
break;
|
||||||
|
case "/":
|
||||||
|
val =""+ (Integer.parseInt(args.get(i-2))/Integer.parseInt(args.get(i)));
|
||||||
|
args.set(i,val);
|
||||||
|
args.remove(i-1);
|
||||||
|
args.remove(i-2);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return Long.parseLong(args.get(0));
|
||||||
}
|
}
|
||||||
|
|
||||||
// -----
|
// -----
|
||||||
|
|
|
@ -0,0 +1,139 @@
|
||||||
|
package de.hs_mannheim.informatik.spreadsheet;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
public class nr5 {
|
||||||
|
private Spreadsheet spr;
|
||||||
|
private String[] spreadsheetData = {
|
||||||
|
"40", "12", "87", "54", "32", "66", "78", "21", "90", "89",
|
||||||
|
"25", "74", "88", "91", "33", "98", "67", "45", "19", "11",
|
||||||
|
"65", "69", "50", "84", "74", "61", "53", "99", "29", "46",
|
||||||
|
"16", "89", "79", "62", "45", "31", "73", "23", "13", "81",
|
||||||
|
"60", "88", "45", "51", "36", "16", "46", "3", "18", "85",
|
||||||
|
"4", "27", "87", "91", "29", "84", "95", "65", "71", "94",
|
||||||
|
"84", "49", "3", "79", "67", "89", "10", "74", "77", "51",
|
||||||
|
"48", "38", "41", "59", "2", "15", "62", "18", "40", "29",
|
||||||
|
"93", "13", "5", "58", "30", "64", "58", "6", "67", "96",
|
||||||
|
"67", "77", "25", "81", "87", "27", "53", "31", "85", "98"};
|
||||||
|
|
||||||
|
public void setup() {
|
||||||
|
spr = new Spreadsheet(10,10);
|
||||||
|
|
||||||
|
for(int i=0;i<10;i++) {
|
||||||
|
for(int j=0;j<10;j++) {
|
||||||
|
spr.put(i,j, spreadsheetData[i+j*10]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSum() {
|
||||||
|
setup();
|
||||||
|
System.out.println(spr);
|
||||||
|
long expectedSum = 40 + 12 + 87 + 54 + 32 + 66 + 78 + 21 + 90 + 89 +
|
||||||
|
25 + 74 + 88 + 91 + 33 + 98 + 67 + 45 + 19 + 11 +
|
||||||
|
65 + 69 + 50 + 84 + 74 + 61 + 53 + 99 + 29 + 46 +
|
||||||
|
16 + 89 + 79 + 62 + 45 + 31 + 73 + 23 + 13 + 81 +
|
||||||
|
60 + 88 + 45 + 51 + 36 + 16 + 46 + 3 + 18 + 85 +
|
||||||
|
4 + 27 + 87 + 91 + 29 + 84 + 95 + 65 + 71 + 94 +
|
||||||
|
84 + 49 + 3 + 79 + 67 + 89 + 10 + 74 + 77 + 51 +
|
||||||
|
48 + 38 + 41 + 59 + 2 + 15 + 62 + 18 + 40 + 29 +
|
||||||
|
93 + 13 + 5 + 58 + 30 + 64 + 58 + 6 + 67 + 96 +
|
||||||
|
67 + 77 + 25 + 81 + 87 + 27 + 53 + 31 + 85 + 98;
|
||||||
|
|
||||||
|
assertEquals(expectedSum, spr.sum("A1", "J10"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testProd() {
|
||||||
|
setup();
|
||||||
|
long expectedProduct = 1;
|
||||||
|
for (String value : spreadsheetData) {
|
||||||
|
expectedProduct *= Integer.parseInt(value);
|
||||||
|
}
|
||||||
|
assertEquals(expectedProduct, spr.prod("A1", "J10"));
|
||||||
|
}
|
||||||
|
@Test
|
||||||
|
public void testMid() {
|
||||||
|
setup();
|
||||||
|
int mid = 0;
|
||||||
|
for (String value : spreadsheetData) {
|
||||||
|
mid += Integer.parseInt(value);
|
||||||
|
}
|
||||||
|
mid /= 100;
|
||||||
|
|
||||||
|
assertEquals((long) mid, spr.mid("A1", "J10"));
|
||||||
|
}
|
||||||
|
@Test
|
||||||
|
public void testStabw() {
|
||||||
|
setup();
|
||||||
|
double Stabw = stabw();
|
||||||
|
assertEquals((long) Stabw, spr.stabw("A1", "J10"));
|
||||||
|
}
|
||||||
|
@Test
|
||||||
|
public void testMin() {
|
||||||
|
setup();
|
||||||
|
long Min = calMin();
|
||||||
|
assertEquals(Min, spr.min("A1", "J10"));
|
||||||
|
}
|
||||||
|
@Test
|
||||||
|
public void testMax() {
|
||||||
|
setup();
|
||||||
|
long Max = calMax();
|
||||||
|
assertEquals(Max, spr.max("A1", "J10"));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
private double stabw() {
|
||||||
|
double midVal = cMV();
|
||||||
|
double sum = 0.0;
|
||||||
|
int numbers = 0;
|
||||||
|
for (String value : spreadsheetData) {
|
||||||
|
if (!value.isBlank()) {
|
||||||
|
long longValue = Long.parseLong(value);
|
||||||
|
sum += Math.pow(longValue - midVal, 2);
|
||||||
|
numbers++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return Math.sqrt(sum / numbers);
|
||||||
|
}
|
||||||
|
private double cMV() {
|
||||||
|
double midSum = 0.0;
|
||||||
|
int numbers = 0;
|
||||||
|
for (String value : spreadsheetData) {
|
||||||
|
if (!value.isBlank()) {
|
||||||
|
midSum += Long.parseLong(value);
|
||||||
|
numbers++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return midSum / numbers;
|
||||||
|
}
|
||||||
|
private long calMin() {
|
||||||
|
long min = Long.MAX_VALUE;
|
||||||
|
for (String value : spreadsheetData) {
|
||||||
|
if (!value.isBlank()) {
|
||||||
|
long longValue = Long.parseLong(value);
|
||||||
|
min = Math.min(min, longValue);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return min;
|
||||||
|
}
|
||||||
|
private long calMax() {
|
||||||
|
long max = Long.MIN_VALUE;
|
||||||
|
|
||||||
|
for (String value : spreadsheetData) {
|
||||||
|
if (!value.isBlank()) {
|
||||||
|
long longValue = Long.parseLong(value);
|
||||||
|
max = Math.max(max, longValue);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return max;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue