1
0
Fork 0

Minor fixes and changes.

pull/1/head
selim 2023-12-26 06:34:14 +01:00
parent cc351b4395
commit 4ba9ff2bb7
2 changed files with 129 additions and 71 deletions

View File

@ -1,6 +1,7 @@
package de.hs_mannheim.informatik.spreadsheet;
import java.io.FileNotFoundException;
import java.util.Scanner;
/**
* Part of a simplified spreadsheet system for the PR1 programming lab at Hochschule Mannheim.
@ -10,9 +11,13 @@ import java.io.FileNotFoundException;
public class Axel {
public static void main(String[] args) throws FileNotFoundException {
Spreadsheet spr = new Spreadsheet(10,10);
Spreadsheet spr = new Spreadsheet(10, 10);
Scanner sc = new Scanner(System.in);
spr.cells[2][7].setFormula("=5+3");
while (true) {
spr.cells[sc.nextInt()-1][5].setFormula(sc.next());
spr.updateSpreadsheet();
@ -21,5 +26,5 @@ public class Axel {
// TODO: You might want to put "UI loop" for entering value and formulas here resp. in some UI methods.
}
}
}

View File

@ -151,67 +151,89 @@ public class Spreadsheet {
String formula = cells[row][col].getFormula();
String result = "";
if(formula.length()>10) {
String substringStartCell1 = formula.substring(formula.length() - 6, formula.length() - 4);
String substringStart2 = formula.substring(formula.length() - 6, formula.length() - 3);
String substringEndCell = formula.substring(formula.length() - 3, formula.length() - 1);
if (formula.startsWith("SUMME(")) // e.g. SUMME(A3:A8)
if(formula.length()<13)
if (formula.length() < 13)
result = "" + sum(substringStartCell1, substringEndCell);
else if(formula.length()<14&&substringStart2.contains(":"))
else if (formula.length() < 14 && substringStart2.contains(":"))
result = "" + sum(substringStartCell1, substringEndCell);
else if(formula.length()<14&&!substringStart2.contains(":"))
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);
else
result = "0";
else if (formula.startsWith("PRODUKT(")) // e.g. PRODUKT(A3:B9)
if(formula.length()<15)
if (formula.length() < 15)
result = "" + product(substringStartCell1, substringEndCell);
else if(formula.length()<16&&substringStart2.contains(":"))
else if (formula.length() < 16 && substringStart2.contains(":"))
result = "" + product(substringStartCell1, substringEndCell);
else if(formula.length()<16&&!substringStart2.contains(":"))
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);
else
result = "0";
else if (formula.startsWith("MITTELWERT(")) // e.g. MITTELWERT(A3:A5)
if(formula.length()<17)
if (formula.length() < 17)
result = "" + average(substringStartCell1, substringEndCell);
else if(formula.length()<18&&substringStart2.contains(":"))
else if (formula.length() < 18 && substringStart2.contains(":"))
result = "" + average(substringStartCell1, substringEndCell);
else if(formula.length()<18&&!substringStart2.contains(":"))
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);
else
result = "0";
else if (formula.startsWith("STABW(")) // e.g. STABW(C6:D8) -> Standardabweichung
if(formula.length()<13)
if (formula.length() < 13)
result = "" + standardDeviation(substringStartCell1, substringEndCell);
else if(formula.length()<14&&substringStart2.contains(":"))
else if (formula.length() < 14 && substringStart2.contains(":"))
result = "" + standardDeviation(substringStartCell1, substringEndCell);
else if(formula.length()<14&&!substringStart2.contains(":"))
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);
else
result = "0";
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.startsWith("MIN(")) // e.g. MIN(C13:H13) -> kleinster Wert
if (formula.length() < 11)
result = "" + min(substringStartCell1, substringEndCell);
else if (formula.length() < 12 && substringStart2.contains(":"))
result = "" + min(substringStartCell1, substringEndCell);
else if (formula.length() < 12 && !substringStart2.contains(":"))
result = "" + min(substringStart2, substringEndCell);
else if (formula.length() < 13)
result = "" + min(substringStart2, substringEndCell);
else
result = "0";
else if (formula.startsWith("MAX(")) // e.g. MAX(A1:A10) -> größter Wert
if (formula.length() < 11)
result = "" + max(substringStartCell1, substringEndCell);
else if (formula.length() < 12 && substringStart2.contains(":"))
result = "" + max(substringStartCell1, substringEndCell);
else if (formula.length() < 12 && !substringStart2.contains(":"))
result = "" + max(substringStart2, substringEndCell);
else if (formula.length() < 13)
result = "" + max(substringStart2, substringEndCell);
else
result = "0";
}
else if (!formula.isEmpty()) {
try {
result = "" + calculate(formula);
@ -252,8 +274,8 @@ public class Spreadsheet {
return result;
}
private long average(String startCellName, String endCellName){
long result = 0;
private double average(String startCellName, String endCellName){
double result = 0;
int counter = 0;
for(int r = getRow(startCellName); r<getRow(endCellName)+1; r++)
for(int c = getCol(startCellName); c<getCol(endCellName)+1; c++)
@ -270,14 +292,13 @@ public class Spreadsheet {
long avg = 0;
int counter = 0;
for(int r = getRow(startCellName); r<getRow(endCellName)+1; r++)
for(int c = getCol(startCellName); c<getCol(endCellName)+1; c++)
try {
for(int c = getCol(startCellName); c<getCol(endCellName)+1; c++) {
avg += Long.parseLong(cells[r][c].getValue());
counter ++;
counter++;
cellNames.add(getCellName(r, c));
} catch (NumberFormatException n){}
}
avg /= counter;
//average
//average/ add cell names to list
ArrayList<String> copyCellNames = new ArrayList<>(cellNames);
double[] frequency = new double[cellNames.size()];
@ -330,6 +351,38 @@ public class Spreadsheet {
return Math.sqrt(mem);
//standardDeviation formula
}
private long min(String startCellName, String endCellName){
ArrayList<String> cellNames = new ArrayList<>();
for(int r = getRow(startCellName); r<getRow(endCellName)+1; r++)
for(int c = getCol(startCellName); c<getCol(endCellName)+1; c++)
try {
cellNames.add(getCellName(r, c));
} catch (NumberFormatException n){}
long result = Long.parseLong(get(cellNames.get(0)));
for(int i = 0; i< cellNames.size(); i++)
if(result>Long.parseLong(get(cellNames.get(i))))
result = Long.parseLong(get(cellNames.get(i)));
return result;
}
private long max(String startCellName, String endCellName){
ArrayList<String> cellNames = new ArrayList<>();
for(int r = getRow(startCellName); r<getRow(endCellName)+1; r++)
for(int c = getCol(startCellName); c<getCol(endCellName)+1; c++)
try {
cellNames.add(getCellName(r, c));
} catch (NumberFormatException n){}
long result = Long.parseLong(get(cellNames.get(0)));
for(int i = 0; i< cellNames.size(); i++)
if(result<Long.parseLong(get(cellNames.get(i))))
result = Long.parseLong(get(cellNames.get(i)));
return result;
}
/**
* This method calculates the result of a "normal" algebraic expression. It only needs to support
@ -406,7 +459,7 @@ public class Spreadsheet {
sb.append(" " + (char) ('A' + i) + " |");
}
int rc = 1;
for (Cell[] r : cells) {
for (int r = 0; r < cells.length; r++) {
sb.append(System.lineSeparator());
for (int i = 0; i < cells[0].length; i++) {
sb.append("----------------");
@ -415,8 +468,8 @@ public class Spreadsheet {
sb.append(System.lineSeparator());
sb.append(String.format(" " + "%2s", rc++) + " |");
for (Cell c : r) {
sb.append(" " + String.format("%13s", c) + " |");
for (int c = 0; c < cells[r].length; c++) {
sb.append(" " + String.format("%13s", cells[r][c].getValue()) + " |");
}
}