1
0
Fork 0

Spreadsheet changed every function supporting double.

pull/1/head
selim 2023-12-27 03:29:44 +01:00
parent 1719f8db84
commit 82406c871d
1 changed files with 50 additions and 49 deletions

View File

@ -149,7 +149,7 @@ 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 = ""; double result = 0;
String substringStartCell1 = ""; String substringStartCell1 = "";
String substringStartCell2 = ""; String substringStartCell2 = "";
String substringStartCell3 = ""; String substringStartCell3 = "";
@ -166,79 +166,80 @@ public class Spreadsheet {
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, substringEndCell1); result = sum(substringStartCell1, substringEndCell1);
else if (formula.length() < 14) else if (formula.length() < 14)
result = "" + sum(substringStartCell2, substringEndCell2); result = sum(substringStartCell2, substringEndCell2);
else if (formula.length() < 15) else if (formula.length() < 15)
result = "" + sum(substringStartCell3, substringEndCell2); 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, substringEndCell1); result = product(substringStartCell1, substringEndCell1);
else if (formula.length() < 16 && !substringEndCell2.contains(":")) else if (formula.length() < 16 && !substringEndCell2.contains(":"))
result = "" + product(substringStartCell2, substringEndCell2); result = product(substringStartCell2, substringEndCell2);
else if (formula.length() < 17) else if (formula.length() < 17)
result = "" + product(substringStartCell3, substringEndCell2); 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() < 18) if (formula.length() < 18)
result = "" + average(substringStartCell1, substringEndCell1); result = average(substringStartCell1, substringEndCell1);
else if (formula.length() < 19) else if (formula.length() < 19)
result = "" + average(substringStartCell2, substringEndCell2); result = average(substringStartCell2, substringEndCell2);
else if (formula.length() < 20) else if (formula.length() < 20)
result = "" + average(substringStartCell3, substringEndCell2); 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, substringEndCell1); result = standardDeviation(substringStartCell1, substringEndCell1);
else if (formula.length() < 14) else if (formula.length() < 14)
result = "" + standardDeviation(substringStartCell2, substringEndCell2); result = standardDeviation(substringStartCell2, substringEndCell2);
else if (formula.length() < 15) else if (formula.length() < 15)
result = "" + standardDeviation(substringStartCell3, substringEndCell2); result = standardDeviation(substringStartCell3, substringEndCell2);
else else
result = "0"; result = 0;
else if (formula.startsWith("MIN(")) // e.g. MIN(C1: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, substringEndCell1); result = min(substringStartCell1, substringEndCell1);
else if (formula.length() < 12) else if (formula.length() < 12)
result = "" + min(substringStartCell2, substringEndCell2); result = min(substringStartCell2, substringEndCell2);
else if (formula.length() < 13) else if (formula.length() < 13)
result = "" + min(substringStartCell3, substringEndCell2); 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, substringEndCell1); result = max(substringStartCell1, substringEndCell1);
else if (formula.length() < 12) else if (formula.length() < 12)
result = "" + max(substringStartCell2, substringEndCell2); result = max(substringStartCell2, substringEndCell2);
else if (formula.length() < 13) else if (formula.length() < 13)
result = "" + max(substringStartCell3, substringEndCell2); result = max(substringStartCell3, substringEndCell2);
else else
result = "0"; result = 0;
else if (!formula.isEmpty()) { else if (!formula.isEmpty()) {
try { try {
result = "" + calculate(formula); result = calculate(formula);
} catch (ArithmeticException ae) { } catch (ArithmeticException ae) {
result = "exc."; result = 0;
} }
} }
cells[row][col].setValue(result); if(!cells[row][col].getFormula().isEmpty())
cells[row][col].setValue(String.format("%.1f",result));
} }
/** /**
@ -248,27 +249,27 @@ 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) { private double sum(String startCellName, String endCellName) {
long result = 0; double result = 0;
for(int r = getRow(startCellName)-1; r<getRow(endCellName); r++) for(int r = getRow(startCellName)-1; r<getRow(endCellName); r++)
for(int c = getCol(startCellName)-1; c<getCol(endCellName); c++) for(int c = getCol(startCellName)-1; c<getCol(endCellName); c++)
if(!cells[r][c].isEmpty()) if(!cells[r][c].isEmpty())
result += Long.parseLong(cells[r][c].getValue()); result += Double.parseDouble(cells[r][c].getValue());
return result; return result;
} }
private long product(String startCellName, String endCellName) { private double product(String startCellName, String endCellName) {
long result = 0; double result = 0;
int counter = 0; int counter = 0;
for(int r = getRow(startCellName)-1; r<getRow(endCellName); r++) for(int r = getRow(startCellName)-1; r<getRow(endCellName); r++)
for(int c = getCol(startCellName)-1; c<getCol(endCellName); c++) for(int c = getCol(startCellName)-1; c<getCol(endCellName); c++)
if(!cells[r][c].isEmpty()) if(!cells[r][c].isEmpty())
if(counter>0) if(counter>0)
result *= Long.parseLong(cells[r][c].getValue()); result *= Double.parseDouble(cells[r][c].getValue());
else { else {
result = Long.parseLong(cells[r][c].getValue()); result = Double.parseDouble(cells[r][c].getValue());
counter++; counter++;
} }
@ -341,7 +342,7 @@ public class Spreadsheet {
return Math.sqrt(mem); return Math.sqrt(mem);
//standardDeviation formula //standardDeviation formula
} }
private long min(String startCellName, String endCellName){ private double min(String startCellName, String endCellName){
ArrayList<String> cellNames = new ArrayList<>(); ArrayList<String> cellNames = new ArrayList<>();
for(int r = getRow(startCellName)-1; r<getRow(endCellName); r++) for(int r = getRow(startCellName)-1; r<getRow(endCellName); r++)
@ -349,15 +350,15 @@ public class Spreadsheet {
if(!cells[r][c].isEmpty()) if(!cells[r][c].isEmpty())
cellNames.add(getCellName(r, c)); cellNames.add(getCellName(r, c));
long result = Long.parseLong(get(cellNames.get(0))); double result = Double.parseDouble(get(cellNames.get(0)));
for(int i = 0; i< cellNames.size(); i++) for(int i = 0; i< cellNames.size(); i++)
try { try {
if (result > Long.parseLong(get(cellNames.get(i)))) if (result > Double.parseDouble(get(cellNames.get(i))))
result = Long.parseLong(get(cellNames.get(i))); result = Double.parseDouble(get(cellNames.get(i)));
} catch(NumberFormatException n){} } catch(NumberFormatException n){}
return result; return result;
} }
private long max(String startCellName, String endCellName){ private double max(String startCellName, String endCellName){
ArrayList<String> cellNames = new ArrayList<>(); ArrayList<String> cellNames = new ArrayList<>();
for(int r = getRow(startCellName)-1; r<getRow(endCellName); r++) for(int r = getRow(startCellName)-1; r<getRow(endCellName); r++)
@ -366,11 +367,11 @@ public class Spreadsheet {
cellNames.add(getCellName(r, c)); cellNames.add(getCellName(r, c));
long result = Long.parseLong(get(cellNames.get(0))); double result = Double.parseDouble(get(cellNames.get(0)));
for(int i = 0; i< cellNames.size(); i++) for(int i = 0; i< cellNames.size(); i++)
try { try {
if (result < Long.parseLong(get(cellNames.get(i)))) if (result < Double.parseDouble(get(cellNames.get(i))))
result = Long.parseLong(get(cellNames.get(i))); result = Double.parseDouble(get(cellNames.get(i)));
} catch(NumberFormatException n){} } catch(NumberFormatException n){}
return result; return result;
@ -386,11 +387,11 @@ public class Spreadsheet {
* @param formula The expression to be evaluated. * @param formula The expression to be evaluated.
* @return The result calculated. * @return The result calculated.
*/ */
private long calculate(String formula) throws ArithmeticException { private double calculate(String formula) throws ArithmeticException {
Matcher m = Pattern.compile("([A-Z][1-9][0-9]*)|[-+*/]|[0-9]+").matcher(formula); Matcher m = Pattern.compile("([A-Z][1-9][0-9]*)|[-+*/]|[0-9]+").matcher(formula);
long result = 0; double result = 0;
long currentOperand = 0; double currentOperand = 0;
String currentOperator = "+"; String currentOperator = "+";
boolean firstOperator = true; boolean firstOperator = true;
@ -398,11 +399,11 @@ public class Spreadsheet {
String s = m.group(); String s = m.group();
if (s.matches(("[0-9]+"))) if (s.matches(("[0-9]+")))
currentOperand = Long.parseLong(s); currentOperand = Double.parseDouble(s);
else if (s.matches("[A-Z][1-9][0-9]*") && get(s).isEmpty()) else if (s.matches("[A-Z][1-9][0-9]*") && get(s).isEmpty())
currentOperand = 0; currentOperand = 0;
else if (s.matches("[A-Z][1-9][0-9]*")) { else if (s.matches("[A-Z][1-9][0-9]*")) {
currentOperand = Long.parseLong(get(s)); currentOperand = Double.parseDouble(get(s));
} else { } else {
if (!firstOperator) { if (!firstOperator) {
result = evaluateOperator(result, currentOperand, currentOperator); result = evaluateOperator(result, currentOperand, currentOperator);
@ -421,7 +422,7 @@ public class Spreadsheet {
return result; return result;
} }
private long evaluateOperator(long result, long currentOperand, String currentOperator) { private double evaluateOperator(double result, double currentOperand, String currentOperator) {
switch (currentOperator) { switch (currentOperator) {
case "+": case "+":
return (result + currentOperand); return (result + currentOperand);