1
0
Fork 0

Spreadsheet evaluateCell functions fixes.

pull/1/head
selim 2023-12-26 19:13:33 +01:00
parent 6e59545dc1
commit ac408c1f23
1 changed files with 32 additions and 32 deletions

View File

@ -74,13 +74,13 @@ public class Spreadsheet {
} }
private int getCol(String cellName) { private int getCol(String cellName) {
return cellName.charAt(0) - 'A'; return cellName.charAt(0) - 'A' + 1;
} }
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)); return Integer.parseInt(""+cellName.charAt(1) + cellName.charAt(2));
return cellName.charAt(1) - '1'; return cellName.charAt(1) - '1' + 1;
} }
// ----- // -----
@ -156,7 +156,7 @@ public class Spreadsheet {
String substringEndCell1 = ""; String substringEndCell1 = "";
String substringEndCell2 = ""; String substringEndCell2 = "";
if(formula.length()>10) { if(formula.length()>9) {
substringStartCell1 = formula.substring(formula.length() - 6, formula.length() - 4); substringStartCell1 = formula.substring(formula.length() - 6, formula.length() - 4);
substringStartCell2 = formula.substring(formula.length() - 7, formula.length() - 5); substringStartCell2 = formula.substring(formula.length() - 7, formula.length() - 5);
substringStartCell3 = formula.substring(formula.length() - 8, formula.length() - 5); substringStartCell3 = formula.substring(formula.length() - 8, formula.length() - 5);
@ -251,11 +251,10 @@ public class Spreadsheet {
private long sum(String startCellName, String endCellName) { private long sum(String startCellName, String endCellName) {
long result = 0; long result = 0;
for(int r = getRow(startCellName); r<getRow(endCellName)+1; r++) for(int r = getRow(startCellName)-1; r<getRow(endCellName); r++)
for(int c = getCol(startCellName); c<getCol(endCellName)+1; c++) for(int c = getCol(startCellName)-1; c<getCol(endCellName); c++)
try { if(!cells[r][c].isEmpty())
result += Long.parseLong(cells[r][c].getValue()); result += Long.parseLong(cells[r][c].getValue());
} catch (NumberFormatException | ArrayIndexOutOfBoundsException a ){}
return result; return result;
} }
@ -263,28 +262,27 @@ public class Spreadsheet {
long result = 0; long result = 0;
int counter = 0; int counter = 0;
for(int r = getRow(startCellName); r<getRow(endCellName)+1; r++) for(int r = getRow(startCellName)-1; r<getRow(endCellName); r++)
for(int c = getCol(startCellName); c<getCol(endCellName)+1; c++) for(int c = getCol(startCellName)-1; c<getCol(endCellName); c++)
try { if(!cells[r][c].isEmpty())
if(counter>0) if(counter>0)
result *= Long.parseLong(cells[r][c].getValue()); result *= Long.parseLong(cells[r][c].getValue());
else { else {
result = Long.parseLong(cells[r][c].getValue()); result = Long.parseLong(cells[r][c].getValue());
counter++; 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)-1; r<getRow(endCellName); r++)
for(int c = getCol(startCellName); c<getCol(endCellName)+1; c++) for(int c = getCol(startCellName)-1; c<getCol(endCellName); c++)
try { if(!cells[r][c].isEmpty()) {
result += Long.parseLong(cells[r][c].getValue()); result += Long.parseLong(cells[r][c].getValue());
counter ++; counter++;
} catch (NumberFormatException | ArrayIndexOutOfBoundsException a ){} }
result /= counter; result /= counter;
return result; return result;
} }
@ -293,16 +291,16 @@ public class Spreadsheet {
double 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)-1; r<getRow(endCellName); r++)
for(int c = getCol(startCellName); c<getCol(endCellName)+1; c++) for(int c = getCol(startCellName)-1; c<getCol(endCellName); c++)
try { if(!cells[r][c].isEmpty()){
avg += Double.parseDouble(cells[r][c].getValue()); avg += Double.parseDouble(cells[r][c].getValue());
counter++; counter++;
cellNames.add(getCellName(r, c)); cellNames.add(getCellName(r, c));
} catch (NumberFormatException | ArrayIndexOutOfBoundsException a ){} }
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);
@ -323,7 +321,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);
@ -345,32 +343,34 @@ public class Spreadsheet {
private long min(String startCellName, String endCellName){ private long min(String startCellName, String endCellName){
ArrayList<String> cellNames = new ArrayList<>(); ArrayList<String> cellNames = new ArrayList<>();
for(int r = getRow(startCellName); r<getRow(endCellName)+1; r++) for(int r = getRow(startCellName)-1; r<getRow(endCellName); r++)
for(int c = getCol(startCellName); c<getCol(endCellName)+1; c++) for(int c = getCol(startCellName)-1; c<getCol(endCellName); c++)
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))); long result = Long.parseLong(get(cellNames.get(0)));
for(int i = 0; i< cellNames.size(); i++) for(int i = 0; i< cellNames.size(); i++)
if(result>Long.parseLong(get(cellNames.get(i)))) try {
result = Long.parseLong(get(cellNames.get(i))); if (result > Long.parseLong(get(cellNames.get(i))))
result = Long.parseLong(get(cellNames.get(i)));
} catch(NumberFormatException n){}
return result; return result;
} }
private long max(String startCellName, String endCellName){ private long max(String startCellName, String endCellName){
ArrayList<String> cellNames = new ArrayList<>(); ArrayList<String> cellNames = new ArrayList<>();
for(int r = getRow(startCellName); r<getRow(endCellName)+1; r++) for(int r = getRow(startCellName)-1; r<getRow(endCellName); r++)
for(int c = getCol(startCellName); c<getCol(endCellName)+1; c++) for(int c = getCol(startCellName)-1; c<getCol(endCellName); c++)
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))); long result = Long.parseLong(get(cellNames.get(0)));
for(int i = 0; i< cellNames.size(); i++) for(int i = 0; i< cellNames.size(); i++)
if(result<Long.parseLong(get(cellNames.get(i)))) try {
result = Long.parseLong(get(cellNames.get(i))); if (result < Long.parseLong(get(cellNames.get(i))))
result = Long.parseLong(get(cellNames.get(i)));
} catch(NumberFormatException n){}
return result; return result;
} }