|
|
|
@ -1,10 +1,8 @@
|
|
|
|
|
package de.hs_mannheim.informatik.spreadsheet;
|
|
|
|
|
|
|
|
|
|
import java.io.FileNotFoundException;
|
|
|
|
|
import java.io.File;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.io.PrintWriter;
|
|
|
|
|
import java.util.Scanner;
|
|
|
|
|
import java.util.regex.Matcher;
|
|
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
|
|
|
|
@ -23,12 +21,8 @@ public class Spreadsheet {
|
|
|
|
|
* @param cols number of columns
|
|
|
|
|
*/
|
|
|
|
|
public Spreadsheet(int rows, int cols) {
|
|
|
|
|
if (rows>99){
|
|
|
|
|
rows=99;
|
|
|
|
|
}
|
|
|
|
|
if (cols>26) {
|
|
|
|
|
cols=26;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO limit the maximum size on 99 (1..99) rows and 26 (A..Z) columns
|
|
|
|
|
|
|
|
|
|
cells = new Cell[rows][cols];
|
|
|
|
|
|
|
|
|
@ -68,7 +62,7 @@ public class Spreadsheet {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private int getRow(String cellName) {
|
|
|
|
|
return Integer.parseInt(cellName.substring(1))-1;
|
|
|
|
|
return cellName.charAt(1) - '1';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// -----
|
|
|
|
@ -82,18 +76,8 @@ public class Spreadsheet {
|
|
|
|
|
* @return Nothing.
|
|
|
|
|
* @exception IOException If path does not exist.
|
|
|
|
|
*/
|
|
|
|
|
public void readCsv(String path) throws FileNotFoundException {
|
|
|
|
|
Scanner file=new Scanner(new File(path));
|
|
|
|
|
String[] rowRead;
|
|
|
|
|
int row=0;
|
|
|
|
|
while(file.hasNextLine()) {
|
|
|
|
|
rowRead=file.nextLine().split(",");
|
|
|
|
|
for(int col=0;col<=rowRead.length-1;col++) {
|
|
|
|
|
put(row,col,rowRead[col]);
|
|
|
|
|
}
|
|
|
|
|
row++;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
public void readCsv(String path, char separator, String startCellName) throws FileNotFoundException {
|
|
|
|
|
// TODO: implement this
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -107,8 +91,12 @@ public class Spreadsheet {
|
|
|
|
|
|
|
|
|
|
for (Cell[] row : cells) {
|
|
|
|
|
for (Cell cell : row) {
|
|
|
|
|
if (!cell.getFormula().isEmpty())
|
|
|
|
|
out.print("=" + cell.getFormula());
|
|
|
|
|
else
|
|
|
|
|
out.print(cell.getValue());
|
|
|
|
|
if (cell != row[cells.length-1])
|
|
|
|
|
|
|
|
|
|
if (cell != row[cells[0].length-1])
|
|
|
|
|
out.print(",");
|
|
|
|
|
}
|
|
|
|
|
out.println();
|
|
|
|
@ -126,48 +114,18 @@ public class Spreadsheet {
|
|
|
|
|
String formula = cells[row][col].getFormula();
|
|
|
|
|
String result = "";
|
|
|
|
|
|
|
|
|
|
if (formula.startsWith("SUMME("))
|
|
|
|
|
if(formula.length()==14)
|
|
|
|
|
result = "" + sum(formula.substring(6, 9), formula.substring(10, 13));
|
|
|
|
|
else if(formula.length()==13)
|
|
|
|
|
result = "" + sum(formula.substring(6, 8), formula.substring(9, 12));
|
|
|
|
|
else
|
|
|
|
|
result = "" + sum(formula.substring(6, 8), formula.substring(9, 11));
|
|
|
|
|
else if (formula.startsWith("PRODUKT("))
|
|
|
|
|
if(formula.length()==16)
|
|
|
|
|
result = "" + product(formula.substring(8, 11), formula.substring(12, 15));
|
|
|
|
|
else if(formula.length()==15)
|
|
|
|
|
result = "" + product(formula.substring(8, 10), formula.substring(11, 14));
|
|
|
|
|
else
|
|
|
|
|
result = "" + product(formula.substring(8, 10), formula.substring(11, 13));
|
|
|
|
|
else if (formula.startsWith("MITTELWERT("))
|
|
|
|
|
if(formula.length()==19)
|
|
|
|
|
result = "" + average(formula.substring(11, 14), formula.substring(15, 18));
|
|
|
|
|
else if(formula.length()==18)
|
|
|
|
|
result = "" + average(formula.substring(11, 13), formula.substring(14, 17));
|
|
|
|
|
else
|
|
|
|
|
result = "" + average(formula.substring(11, 13), formula.substring(14, 16));
|
|
|
|
|
else if (formula.startsWith("STABW("))
|
|
|
|
|
if(formula.length()==14)
|
|
|
|
|
result = "" + standardDeviation(formula.substring(6, 9), formula.substring(10, 13));
|
|
|
|
|
else if(formula.length()==13)
|
|
|
|
|
result = "" + standardDeviation(formula.substring(6, 8), formula.substring(9, 12));
|
|
|
|
|
else
|
|
|
|
|
result = "" + standardDeviation(formula.substring(6, 8), formula.substring(9, 11));
|
|
|
|
|
else if (formula.startsWith("MAX("))
|
|
|
|
|
if(formula.length()==12)
|
|
|
|
|
result = "" + max(formula.substring(4, 7), formula.substring(8, 11));
|
|
|
|
|
else if(formula.length()==11)
|
|
|
|
|
result = "" + max(formula.substring(4, 6), formula.substring(7, 10));
|
|
|
|
|
else
|
|
|
|
|
result = "" + max(formula.substring(4, 6), formula.substring(7, 9));
|
|
|
|
|
else if (formula.startsWith("MIN("))
|
|
|
|
|
if(formula.length()==12)
|
|
|
|
|
result = "" + min(formula.substring(4, 7), formula.substring(8, 11));
|
|
|
|
|
else if(formula.length()==11)
|
|
|
|
|
result = "" + min(formula.substring(4, 6), formula.substring(7, 10));
|
|
|
|
|
else
|
|
|
|
|
result = "" + min(formula.substring(4, 6), formula.substring(7, 9));
|
|
|
|
|
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()) {
|
|
|
|
|
try {
|
|
|
|
|
result = "" + calculate(formula);
|
|
|
|
@ -178,79 +136,17 @@ public class Spreadsheet {
|
|
|
|
|
|
|
|
|
|
cells[row][col].setValue("" + result);
|
|
|
|
|
}
|
|
|
|
|
//calculations for cell blocks
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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 sum=0;
|
|
|
|
|
for(int i=startCellName.charAt(0)-'A'; i<=endCellName.charAt(0)-'A'; i++) {
|
|
|
|
|
for(int j=Integer.parseInt(startCellName.substring(1))-1; j<=Integer.parseInt(endCellName.substring(1))-1; j++) {
|
|
|
|
|
if(cells[j][i].isEmpty())
|
|
|
|
|
continue;
|
|
|
|
|
sum+=Integer.parseInt(cells[j][i].getValue());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return sum;
|
|
|
|
|
}
|
|
|
|
|
private long product(String startCellName, String endCellName) {
|
|
|
|
|
long product=1;
|
|
|
|
|
for(int i=startCellName.charAt(0)-'A'; i<=endCellName.charAt(0)-'A'; i++) {
|
|
|
|
|
for(int j=Integer.parseInt(startCellName.substring(1))-1; j<=Integer.parseInt(endCellName.substring(1))-1; j++) {
|
|
|
|
|
if(cells[j][i].isEmpty())
|
|
|
|
|
// TODO implement
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
product=product*Integer.parseInt(cells[j][i].getValue());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return product;
|
|
|
|
|
}
|
|
|
|
|
private long average(String startCellName, String endCellName) {
|
|
|
|
|
long average=0;
|
|
|
|
|
long counter=0;
|
|
|
|
|
for(int i=startCellName.charAt(0)-'A'; i<=endCellName.charAt(0)-'A'; i++) {
|
|
|
|
|
for(int j=Integer.parseInt(startCellName.substring(1))-1; j<=Integer.parseInt(endCellName.substring(1))-1; j++) {
|
|
|
|
|
if(cells[j][i].isEmpty())
|
|
|
|
|
continue;
|
|
|
|
|
average+=Integer.parseInt(cells[j][i].getValue());
|
|
|
|
|
counter++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return (long) average/counter;
|
|
|
|
|
}
|
|
|
|
|
private long standardDeviation(String startCellName, String endCellName) {
|
|
|
|
|
long average=average(startCellName,endCellName);
|
|
|
|
|
long counter=0;
|
|
|
|
|
long value=0;
|
|
|
|
|
for(int i=startCellName.charAt(0)-'A'; i<=endCellName.charAt(0)-'A'; i++) {
|
|
|
|
|
for(int j=Integer.parseInt(startCellName.substring(1))-1; j<=Integer.parseInt(endCellName.substring(1))-1; j++) {
|
|
|
|
|
if(cells[j][i].isEmpty())
|
|
|
|
|
continue;
|
|
|
|
|
value+=Math.pow(Integer.parseInt(cells[j][i].getValue())-average,2);
|
|
|
|
|
counter++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return (long) Math.sqrt(value/(counter-1));
|
|
|
|
|
}
|
|
|
|
|
private int max(String startCellName,String endCellName) {
|
|
|
|
|
int max=Integer.MIN_VALUE;
|
|
|
|
|
for(int i=startCellName.charAt(0)-'A'; i<=endCellName.charAt(0)-'A'; i++) {
|
|
|
|
|
for(int j=Integer.parseInt(startCellName.substring(1))-1; j<=Integer.parseInt(endCellName.substring(1))-1; j++) {
|
|
|
|
|
if(cells[j][i].isEmpty())
|
|
|
|
|
continue;
|
|
|
|
|
else if(max<Integer.parseInt(cells[j][i].getValue()))
|
|
|
|
|
max=Integer.parseInt(cells[j][i].getValue());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return max;
|
|
|
|
|
}
|
|
|
|
|
private int min(String startCellName,String endCellName) {
|
|
|
|
|
int min=Integer.MAX_VALUE;
|
|
|
|
|
for(int i=startCellName.charAt(0)-'A'; i<=endCellName.charAt(0)-'A'; i++) {
|
|
|
|
|
for(int j=Integer.parseInt(startCellName.substring(1))-1; j<=Integer.parseInt(endCellName.substring(1))-1; j++) {
|
|
|
|
|
if(cells[j][i].isEmpty())
|
|
|
|
|
continue;
|
|
|
|
|
else if(min>Integer.parseInt(cells[j][i].getValue()))
|
|
|
|
|
min=Integer.parseInt(cells[j][i].getValue());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return min;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -266,61 +162,14 @@ public class Spreadsheet {
|
|
|
|
|
Matcher m = Pattern.compile("([A-Z][0-9]*)|[-\\+\\*/]|[0-9]*").matcher(formula);
|
|
|
|
|
|
|
|
|
|
long res = 0;
|
|
|
|
|
boolean first=true;
|
|
|
|
|
String operation="";
|
|
|
|
|
|
|
|
|
|
// 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()) {
|
|
|
|
|
if((int)s.charAt(0)>=(int)'A'&&(int)s.charAt(0)<=(int)'Z' && get(s).isEmpty())
|
|
|
|
|
continue;
|
|
|
|
|
if(first && (int)s.charAt(0)>=(int)'A'&&(int)s.charAt(0)<=(int)'Z') {
|
|
|
|
|
res=Integer.parseInt(get(s));
|
|
|
|
|
first=false;
|
|
|
|
|
}else if(first) {
|
|
|
|
|
res=Integer.parseInt(s);
|
|
|
|
|
first=false;
|
|
|
|
|
}else if(s.equals("+")){
|
|
|
|
|
operation="+";
|
|
|
|
|
}else if(s.equals("-")){
|
|
|
|
|
operation="-";
|
|
|
|
|
}else if(s.equals("*")){
|
|
|
|
|
operation="*";
|
|
|
|
|
}else if(s.equals("/")){
|
|
|
|
|
operation="/";
|
|
|
|
|
}else if((int)s.charAt(0)>=(int)'A'&&(int)s.charAt(0)<=(int)'Z'){
|
|
|
|
|
switch(operation) {
|
|
|
|
|
case("+"):
|
|
|
|
|
res+=Integer.parseInt(get(s));
|
|
|
|
|
break;
|
|
|
|
|
case("-"):
|
|
|
|
|
res-=Integer.parseInt(get(s));
|
|
|
|
|
break;
|
|
|
|
|
case("*"):
|
|
|
|
|
res*=Integer.parseInt(get(s));
|
|
|
|
|
break;
|
|
|
|
|
case("/"):
|
|
|
|
|
res/=Integer.parseInt(get(s));
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}else {
|
|
|
|
|
switch(operation) {
|
|
|
|
|
case("+"):
|
|
|
|
|
res+=Integer.parseInt(s);
|
|
|
|
|
break;
|
|
|
|
|
case("-"):
|
|
|
|
|
res-=Integer.parseInt(s);
|
|
|
|
|
break;
|
|
|
|
|
case("*"):
|
|
|
|
|
res*=Integer.parseInt(s);
|
|
|
|
|
break;
|
|
|
|
|
case("/"):
|
|
|
|
|
res/=Integer.parseInt(s);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!s.isEmpty()) {
|
|
|
|
|
System.out.println(s);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|