1
0
Fork 0

Many fixes until excluding average method.

pull/1/head
selim 2023-12-26 09:59:16 +01:00
parent 4ba9ff2bb7
commit 138275e3e3
2 changed files with 103 additions and 78 deletions

View File

@ -20,8 +20,9 @@ public class Axel {
spr.cells[sc.nextInt()-1][5].setFormula(sc.next()); spr.cells[sc.nextInt()-1][5].setFormula(sc.next());
spr.updateSpreadsheet(); spr.updateSpreadsheet();
System.out.println(spr.toStringShowFormula());
System.out.println(spr); System.out.println(spr);
System.out.println(spr.cells[0][5]);
// TODO: You might want to put "UI loop" for entering value and formulas here resp. in some UI methods. // TODO: You might want to put "UI loop" for entering value and formulas here resp. in some UI methods.

View File

@ -56,7 +56,7 @@ public class Spreadsheet {
} }
public String getCellName(int row, int col) { public String getCellName(int row, int col) {
return "" + ((char) ('A' + col)) + (row+1); return ("" + ((char) ('A' + col))) + (row+1);
} }
private void put(int row, int col, String value) { private void put(int row, int col, String value) {
@ -79,7 +79,7 @@ public class Spreadsheet {
private int getRow(String cellName) { private int getRow(String cellName) {
if(cellName.length()>2) if(cellName.length()>2)
return Integer.parseInt(""+cellName.charAt(1)+cellName.charAt(2)) - 1; return Integer.parseInt(""+cellName.charAt(1) + cellName.charAt(2));
return cellName.charAt(1) - '1'; return cellName.charAt(1) - '1';
} }
@ -91,7 +91,7 @@ public class Spreadsheet {
* *
* @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 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. * @return Nothing.
* @throws IOException If path does not exist. * @throws IOException If path does not exist.
*/ */
@ -144,7 +144,7 @@ public class Spreadsheet {
/** /**
* This method does the actual evaluation/calcluation of a specific cell * This method does the actual evaluation/calcluation of a specific cell
* *
* @param cellName the name of the cell to be evaluated * @param row,col the name of row and column of the cell
* @return Nothing. * @return Nothing.
*/ */
private void evaluateCell(int row, int col) { private void evaluateCell(int row, int col) {
@ -153,83 +153,73 @@ public class Spreadsheet {
if(formula.length()>10) { if(formula.length()>10) {
String substringStartCell1 = formula.substring(formula.length() - 6, formula.length() - 4); String substringStartCell1 = formula.substring(formula.length() - 6, formula.length() - 4);
String substringStart2 = formula.substring(formula.length() - 6, formula.length() - 3); String substringStartCell2 = formula.substring(formula.length() - 7, formula.length() - 5);
String substringEndCell = formula.substring(formula.length() - 3, formula.length() - 1); String substringStartCell3 = formula.substring(formula.length() - 8, formula.length() - 5);
String substringEndCell1 = formula.substring(formula.length() - 3, formula.length() - 1);
String substringEndCell2 = formula.substring(formula.length() - 4, formula.length() - 1);
if (formula.startsWith("SUMME(")) // e.g. SUMME(A3:A8) if (formula.startsWith("SUMME("))// e.g. SUMME(A3:A8)
if (formula.length() < 13) if (formula.length() < 13)
result = "" + sum(substringStartCell1, substringEndCell); result = "" + sum(substringStartCell1, substringEndCell1);
else if (formula.length() < 14 && substringStart2.contains(":")) else if (formula.length() < 14)
result = "" + sum(substringStartCell1, substringEndCell); result = "" + sum(substringStartCell2, substringEndCell2);
else if (formula.length() < 14 && !substringStart2.contains(":"))
result = "" + sum(substringStart2, substringEndCell);
else if (formula.length() < 15) else if (formula.length() < 15)
result = "" + sum(substringStart2, substringEndCell); result = "" + sum(substringStartCell3, substringEndCell2);
else else
result = "0"; result = "0";
else if (formula.startsWith("PRODUKT(")) // e.g. PRODUKT(A3:B9) else if (formula.startsWith("PRODUKT(")) // e.g. PRODUKT(A3:B9)
if (formula.length() < 15) if (formula.length() < 15)
result = "" + product(substringStartCell1, substringEndCell); result = "" + product(substringStartCell1, substringEndCell1);
else if (formula.length() < 16 && substringStart2.contains(":")) else if (formula.length() < 16 && !substringEndCell2.contains(":"))
result = "" + product(substringStartCell1, substringEndCell); result = "" + product(substringStartCell2, substringEndCell2);
else if (formula.length() < 16 && !substringStart2.contains(":"))
result = "" + product(substringStart2, substringEndCell);
else if (formula.length() < 17) else if (formula.length() < 17)
result = "" + product(substringStart2, substringEndCell); result = "" + product(substringStartCell3, substringEndCell2);
else else
result = "0"; result = "0";
else if (formula.startsWith("MITTELWERT(")) // e.g. MITTELWERT(A3:A5) else if (formula.startsWith("MITTELWERT(")) // e.g. MITTELWERT(A3:A5)
if (formula.length() < 17) if (formula.length() < 17)
result = "" + average(substringStartCell1, substringEndCell); result = "" + average(substringStartCell1, substringEndCell1);
else if (formula.length() < 18 && substringStart2.contains(":")) else if (formula.length() < 18)
result = "" + average(substringStartCell1, substringEndCell); result = "" + average(substringStartCell2, substringEndCell2);
else if (formula.length() < 18 && !substringStart2.contains(":"))
result = "" + average(substringStart2, substringEndCell);
else if (formula.length() < 19) else if (formula.length() < 19)
result = "" + average(substringStart2, substringEndCell); result = "" + average(substringStartCell3, substringEndCell2);
else else
result = "0"; result = "0";
else if (formula.startsWith("STABW(")) // e.g. STABW(C6:D8) -> Standardabweichung else if (formula.startsWith("STABW(")) // e.g. STABW(C6:D8) -> Standardabweichung
if (formula.length() < 13) if (formula.length() < 13)
result = "" + standardDeviation(substringStartCell1, substringEndCell); result = "" + standardDeviation(substringStartCell1, substringEndCell1);
else if (formula.length() < 14 && substringStart2.contains(":")) else if (formula.length() < 14)
result = "" + standardDeviation(substringStartCell1, substringEndCell); result = "" + standardDeviation(substringStartCell2, substringEndCell2);
else if (formula.length() < 14 && !substringStart2.contains(":"))
result = "" + standardDeviation(substringStart2, substringEndCell);
else if (formula.length() < 15) else if (formula.length() < 15)
result = "" + standardDeviation(substringStart2, substringEndCell); result = "" + standardDeviation(substringStartCell3, substringEndCell2);
else else
result = "0"; result = "0";
else if (formula.startsWith("MIN(")) // e.g. MIN(C13:H13) -> kleinster Wert else if (formula.startsWith("MIN(")) // e.g. MIN(C1:H13) -> kleinster Wert
if (formula.length() < 11) if (formula.length() < 11)
result = "" + min(substringStartCell1, substringEndCell); result = "" + min(substringStartCell1, substringEndCell1);
else if (formula.length() < 12 && substringStart2.contains(":")) else if (formula.length() < 12)
result = "" + min(substringStartCell1, substringEndCell); result = "" + min(substringStartCell2, substringEndCell2);
else if (formula.length() < 12 && !substringStart2.contains(":"))
result = "" + min(substringStart2, substringEndCell);
else if (formula.length() < 13) else if (formula.length() < 13)
result = "" + min(substringStart2, substringEndCell); result = "" + min(substringStartCell3, substringEndCell2);
else else
result = "0"; result = "0";
else if (formula.startsWith("MAX(")) // e.g. MAX(A1:A10) -> größter Wert else if (formula.startsWith("MAX(")) // e.g. MAX(A1:A10) -> größter Wert
if (formula.length() < 11) if (formula.length() < 11)
result = "" + max(substringStartCell1, substringEndCell); result = "" + max(substringStartCell1, substringEndCell1);
else if (formula.length() < 12 && substringStart2.contains(":")) else if (formula.length() < 12)
result = "" + max(substringStartCell1, substringEndCell); result = "" + max(substringStartCell2, substringEndCell2);
else if (formula.length() < 12 && !substringStart2.contains(":"))
result = "" + max(substringStart2, substringEndCell);
else if (formula.length() < 13) else if (formula.length() < 13)
result = "" + max(substringStart2, substringEndCell); result = "" + max(substringStartCell3, substringEndCell2);
else else
result = "0"; result = "0";
} }
@ -259,22 +249,28 @@ public class Spreadsheet {
for(int c = getCol(startCellName); c<getCol(endCellName)+1; c++) for(int c = getCol(startCellName); c<getCol(endCellName)+1; c++)
try { try {
result += Long.parseLong(cells[r][c].getValue()); result += Long.parseLong(cells[r][c].getValue());
} catch (NumberFormatException n){} } catch (NumberFormatException | ArrayIndexOutOfBoundsException a ){}
return result; return result;
} }
private long product(String startCellName, String endCellName) { private long product(String startCellName, String endCellName) {
long result = Long.parseLong(cells[0][0].getValue());; long result = 0;
int counter = 0;
for(int r = getRow(startCellName); r<getRow(endCellName)+1; r++) for(int r = getRow(startCellName); r<getRow(endCellName)+1; r++)
for(int c = getCol(startCellName); c<getCol(endCellName)+1; c++) for(int c = getCol(startCellName); c<getCol(endCellName)+1; c++)
try { try {
result *= Long.parseLong(cells[r+1][c+1].getValue()); if(counter>0)
} catch (NumberFormatException n){} result *= Long.parseLong(cells[r][c].getValue());
else {
result = Long.parseLong(cells[r][c].getValue());
counter++;
}
} catch (NumberFormatException | ArrayIndexOutOfBoundsException a ){}
return result; return result;
} }
private double average(String startCellName, String endCellName){ private double average(String startCellName, String endCellName) {
double result = 0; double result = 0;
int counter = 0; int counter = 0;
for(int r = getRow(startCellName); r<getRow(endCellName)+1; r++) for(int r = getRow(startCellName); r<getRow(endCellName)+1; r++)
@ -282,24 +278,25 @@ public class Spreadsheet {
try { try {
result += Long.parseLong(cells[r][c].getValue()); result += Long.parseLong(cells[r][c].getValue());
counter ++; counter ++;
} catch (NumberFormatException n){} } catch (NumberFormatException | ArrayIndexOutOfBoundsException a ){}
result /= counter; result /= counter;
return result; return result;
} }
private double standardDeviation(String startCellName, String endCellName){ private double standardDeviation(String startCellName, String endCellName){
ArrayList<String> cellNames = new ArrayList<>(); ArrayList<String> cellNames = new ArrayList<>();
long avg = 0; double avg = 0;
int counter = 0; int counter = 0;
for(int r = getRow(startCellName); r<getRow(endCellName)+1; r++) for(int r = getRow(startCellName); r<getRow(endCellName); r++)
for(int c = getCol(startCellName); c<getCol(endCellName)+1; c++) { for(int c = getCol(startCellName); c<getCol(endCellName); c++)
avg += Long.parseLong(cells[r][c].getValue()); if(!(cells[r][c].isEmpty())) {
avg += Double.parseDouble(cells[r][c].getValue());
counter++; counter++;
cellNames.add(getCellName(r, c)); cellNames.add(getCellName(r, c));
} }
avg /= counter; avg /= counter;
//average/ add cell names to list //average/ add cell names to list
System.out.println(cellNames);
ArrayList<String> copyCellNames = new ArrayList<>(cellNames); ArrayList<String> copyCellNames = new ArrayList<>(cellNames);
double[] frequency = new double[cellNames.size()]; double[] frequency = new double[cellNames.size()];
Arrays.fill(frequency,1); Arrays.fill(frequency,1);
@ -308,19 +305,7 @@ public class Spreadsheet {
for(int i = 0; i< cellNames.size(); i++) { for(int i = 0; i< cellNames.size(); i++) {
for(int t = 0; t<cellNames.size(); t++){ for(int t = 0; t<cellNames.size(); t++){
if(((get(cellNames.get(i)).isEmpty())&&!(get(cellNames.get(t)).isEmpty()))&&(Double.parseDouble(get(cellNames.get(t)))==0)&&(t!=i)){ if(!((get(cellNames.get(i)).isEmpty())&&(get(cellNames.get(t)).isEmpty()))&&(t!=i)&&(Double.parseDouble(get(cellNames.get(i))))==(Double.parseDouble(get(cellNames.get(t))))){
cellNames.remove(t);
frequency[i]++;
t = 0;
}else if((!(get(cellNames.get(i)).isEmpty())&&(get(cellNames.get(t)).isEmpty()))&&(Double.parseDouble(get(cellNames.get(i)))==0)&&(t!=i)){
cellNames.remove(t);
frequency[i]++;
t = 0;
}else if((get(cellNames.get(i)).isEmpty())&&(get(cellNames.get(t)).isEmpty())&&(t!=i)){
cellNames.remove(t);
frequency[i]++;
t = 0;
}else if((t!=i)&&(Double.parseDouble(get(cellNames.get(i))))==(Double.parseDouble(get(cellNames.get(t))))){
cellNames.remove(t); cellNames.remove(t);
frequency[i] ++; frequency[i] ++;
t = 0; t = 0;
@ -332,7 +317,7 @@ public class Spreadsheet {
for(int i = 0; i< cellNames.size(); i++) for(int i = 0; i< cellNames.size(); i++)
relativeFrequency.add(i,frequency[i]/copyCellNames.size()); relativeFrequency.add(i,frequency[i]/copyCellNames.size());
System.out.println("CellNames: "+cellNames);
if(get(cellNames.get(0)).isEmpty()) if(get(cellNames.get(0)).isEmpty())
mem = ((0 - avg)*(0 - avg)) mem = ((0 - avg)*(0 - avg))
* relativeFrequency.get(0); * relativeFrequency.get(0);
@ -356,9 +341,9 @@ public class Spreadsheet {
for(int r = getRow(startCellName); r<getRow(endCellName)+1; r++) for(int r = getRow(startCellName); r<getRow(endCellName)+1; r++)
for(int c = getCol(startCellName); c<getCol(endCellName)+1; c++) for(int c = getCol(startCellName); c<getCol(endCellName)+1; c++)
try { if(!cells[r][c].isEmpty())
cellNames.add(getCellName(r, c)); cellNames.add(getCellName(r, c));
} catch (NumberFormatException n){}
long result = Long.parseLong(get(cellNames.get(0))); long result = Long.parseLong(get(cellNames.get(0)));
for(int i = 0; i< cellNames.size(); i++) for(int i = 0; i< cellNames.size(); i++)
@ -372,9 +357,9 @@ public class Spreadsheet {
for(int r = getRow(startCellName); r<getRow(endCellName)+1; r++) for(int r = getRow(startCellName); r<getRow(endCellName)+1; r++)
for(int c = getCol(startCellName); c<getCol(endCellName)+1; c++) for(int c = getCol(startCellName); c<getCol(endCellName)+1; c++)
try { if(!cells[r][c].isEmpty())
cellNames.add(getCellName(r, c)); cellNames.add(getCellName(r, c));
} catch (NumberFormatException n){}
long result = Long.parseLong(get(cellNames.get(0))); long result = Long.parseLong(get(cellNames.get(0)));
for(int i = 0; i< cellNames.size(); i++) for(int i = 0; i< cellNames.size(); i++)
@ -469,6 +454,9 @@ public class Spreadsheet {
sb.append(String.format(" " + "%2s", rc++) + " |"); sb.append(String.format(" " + "%2s", rc++) + " |");
for (int c = 0; c < cells[r].length; c++) { for (int c = 0; c < cells[r].length; c++) {
if((cells[r][c].getValue()).length()>13)
sb.append(" " + String.format("%13s", cells[r][c].getValue()).substring(0,13) + " |");
else
sb.append(" " + String.format("%13s", cells[r][c].getValue()) + " |"); sb.append(" " + String.format("%13s", cells[r][c].getValue()) + " |");
} }
@ -482,5 +470,41 @@ public class Spreadsheet {
return sb.toString(); return sb.toString();
}
public String toStringShowFormula() {
StringBuilder sb = new StringBuilder();
sb.append(System.lineSeparator());
sb.append(" |");
for (int i = 0; i < cells[0].length; i++) {
sb.append(" " + (char) ('A' + i) + " |");
}
int rc = 1;
for (int r = 0; r < cells.length; r++) {
sb.append(System.lineSeparator());
for (int i = 0; i < cells[0].length; i++) {
sb.append("----------------");
}
sb.append("------");
sb.append(System.lineSeparator());
sb.append(String.format(" " + "%2s", rc++) + " |");
for (int c = 0; c < cells[r].length; c++) {
if((cells[r][c].getFormula()).length()>13)
sb.append(" " + String.format("%13s", cells[r][c].getFormula()).substring(0,13) + " |");
else
sb.append(" " + String.format("%13s", cells[r][c].getFormula()) + " |");
}
}
sb.append(System.lineSeparator());
for (int i = 0; i < cells[0].length; i++) {
sb.append("----------------");
}
sb.append("------");
sb.append(System.lineSeparator());
return sb.toString();
} }
} }