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
|
||||
3,4
|
||||
5,6
|
||||
7,8
|
||||
,24,,,,
|
||||
,28,,,,
|
||||
123,24,,,,
|
||||
,24,,,,
|
||||
,5,,,,
|
||||
,5,,,,
|
||||
,200,,,,
|
||||
,200,,,,
|
||||
,=41+A2+10-2/3,,,,
|
||||
,,,,,
|
||||
|
|
|
|
@ -1,29 +1,118 @@
|
|||
package de.hs_mannheim.informatik.spreadsheet;
|
||||
|
||||
import java.io.File;
|
||||
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.
|
||||
*
|
||||
*
|
||||
* @author Oliver Hummel
|
||||
*/
|
||||
public class Axel {
|
||||
|
||||
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");
|
||||
spr.put("A2", "1");
|
||||
System.out.println("Was möchten sie machen?");
|
||||
|
||||
spr.put("B9", "=41+A2");
|
||||
spr.put("J5", "=7*6");
|
||||
spr.put("J6", "=3/2");
|
||||
System.out.println("\n 1. Dokument öffnen");
|
||||
System.out.println(" 2. Neues Dokument erstellen");
|
||||
System.out.println(" 3. Programm schließen");
|
||||
System.out.print("-> ");
|
||||
String docSlct = input.nextLine();
|
||||
|
||||
System.out.println(spr);
|
||||
|
||||
spr.saveCsv("/tmp/test.csv");
|
||||
|
||||
// TODO: You might want to put "UI loop" for entering value and formulas here resp. in some UI methods.
|
||||
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("Befehl (z.B. D4_=7*6): ");
|
||||
command = input.nextLine();
|
||||
if(command.matches("[A-Za-z][1-9][0-9]?_="
|
||||
+ "((((([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;
|
||||
|
||||
//test
|
||||
/**
|
||||
* 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
|
||||
|
|
|
@ -1,11 +1,14 @@
|
|||
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.regex.Matcher;
|
||||
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.
|
||||
* 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 {
|
||||
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) {
|
||||
|
||||
// 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 c = 0; c < cols; c++)
|
||||
for (int r = 0; r < rowWithMax99; r++)
|
||||
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();
|
||||
}
|
||||
|
||||
// -----
|
||||
/*
|
||||
* @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
|
||||
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
private void put(int row, int col, String value) {
|
||||
void put(int row, int col, String value) {
|
||||
if (!value.startsWith("="))
|
||||
cells[row][col].setValue(value);
|
||||
else {
|
||||
|
@ -51,33 +79,57 @@ public class Spreadsheet {
|
|||
evaluateCell(row, col);
|
||||
}
|
||||
}
|
||||
|
||||
public void put(String cellName, String value) {
|
||||
cellName = cellName.toUpperCase();
|
||||
put(getRow(cellName), getCol(cellName), value);
|
||||
}
|
||||
|
||||
private int getCol(String cellName) {
|
||||
public int getCol(String cellName) {
|
||||
return cellName.charAt(0) - 'A';
|
||||
}
|
||||
|
||||
private int getRow(String cellName) {
|
||||
return cellName.charAt(1) - '1';
|
||||
public 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';
|
||||
}
|
||||
}
|
||||
|
||||
// -----
|
||||
// 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.
|
||||
* @param startCellName The upper left cell where data from the CSV file should be inserted.
|
||||
* @return Nothing.
|
||||
* @exception IOException If path does not exist.
|
||||
*/
|
||||
public void readCsv(String path, char separator, String startCellName) throws FileNotFoundException {
|
||||
// TODO: implement this
|
||||
public void readCsv(String path, String startCellName) throws FileNotFoundException {
|
||||
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.
|
||||
* @exception IOException If path does not exist.
|
||||
*/
|
||||
public void saveCsv(String path) throws FileNotFoundException {
|
||||
PrintWriter out = new PrintWriter(path);
|
||||
public void saveCsv(PrintWriter out) throws FileNotFoundException {
|
||||
|
||||
for (Cell[] row : cells) {
|
||||
for (Cell cell : row) {
|
||||
|
@ -113,20 +164,32 @@ public class Spreadsheet {
|
|||
private void evaluateCell(int row, int col) {
|
||||
String formula = cells[row][col].getFormula();
|
||||
String result = "";
|
||||
|
||||
if (formula.startsWith("SUMME(")) // e.g. SUMME(A3:A8)
|
||||
result = "" + sum(formula.substring(6, 8), formula.substring(9, 11)); // TODO adapt to cells with two digits
|
||||
else if (formula.startsWith("PRODUKT(")) // e.g. PRODUKT(A3:B9)
|
||||
result = "TODO"; // TODO
|
||||
else if (formula.startsWith("MITTELWERT(")) // e.g. MITTELWERT(A3:A5)
|
||||
result = "TODO"; // TODO
|
||||
else if (formula.startsWith("STABW(")) // e.g. STABW(C6:D8) -> 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()) {
|
||||
int offset = 0;
|
||||
int offsetEnd = 0;
|
||||
int diff=0;
|
||||
for(int i = 0; i<formula.length();i++) {
|
||||
if(formula.charAt(i)=='(') {
|
||||
offset=i+1;
|
||||
}else if(formula.charAt(i)==')') {
|
||||
offsetEnd=i;
|
||||
}
|
||||
else if(formula.charAt(i)==':') {
|
||||
diff=i;
|
||||
}
|
||||
}
|
||||
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 {
|
||||
result = "" + calculate(formula);
|
||||
} 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.
|
||||
* @return The sum calculated.
|
||||
*/
|
||||
private long sum(String startCellName, String endCellName) {
|
||||
// TODO implement
|
||||
|
||||
return 0;
|
||||
public long sum(String startCellName, String endCellName) {
|
||||
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;
|
||||
}
|
||||
/**
|
||||
* 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
|
||||
* 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 {
|
||||
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);
|
||||
List<String> args = new ArrayList<>();
|
||||
|
||||
while (m.find()) {
|
||||
if(!m.group().isEmpty()) {
|
||||
if(isNumber(m.group())||isCar(m.group())) {
|
||||
args.add(m.group());
|
||||
}else {
|
||||
if(cells[getRow(m.group())][getCol(m.group())].getValue().isEmpty())
|
||||
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