Spreadsheet evaluateCell functions fixes.
parent
6e59545dc1
commit
ac408c1f23
|
@ -74,13 +74,13 @@ public class Spreadsheet {
|
|||
}
|
||||
|
||||
private int getCol(String cellName) {
|
||||
return cellName.charAt(0) - 'A';
|
||||
return cellName.charAt(0) - 'A' + 1;
|
||||
}
|
||||
|
||||
private int getRow(String cellName) {
|
||||
if(cellName.length()>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 substringEndCell2 = "";
|
||||
|
||||
if(formula.length()>10) {
|
||||
if(formula.length()>9) {
|
||||
substringStartCell1 = formula.substring(formula.length() - 6, formula.length() - 4);
|
||||
substringStartCell2 = formula.substring(formula.length() - 7, 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) {
|
||||
long result = 0;
|
||||
|
||||
for(int r = getRow(startCellName); r<getRow(endCellName)+1; r++)
|
||||
for(int c = getCol(startCellName); c<getCol(endCellName)+1; c++)
|
||||
try {
|
||||
for(int r = getRow(startCellName)-1; r<getRow(endCellName); r++)
|
||||
for(int c = getCol(startCellName)-1; c<getCol(endCellName); c++)
|
||||
if(!cells[r][c].isEmpty())
|
||||
result += Long.parseLong(cells[r][c].getValue());
|
||||
} catch (NumberFormatException | ArrayIndexOutOfBoundsException a ){}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
@ -263,28 +262,27 @@ public class Spreadsheet {
|
|||
long 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++)
|
||||
try {
|
||||
for(int r = getRow(startCellName)-1; r<getRow(endCellName); r++)
|
||||
for(int c = getCol(startCellName)-1; c<getCol(endCellName); c++)
|
||||
if(!cells[r][c].isEmpty())
|
||||
if(counter>0)
|
||||
result *= Long.parseLong(cells[r][c].getValue());
|
||||
else {
|
||||
result = Long.parseLong(cells[r][c].getValue());
|
||||
counter++;
|
||||
}
|
||||
} catch (NumberFormatException | ArrayIndexOutOfBoundsException a ){}
|
||||
|
||||
return result;
|
||||
}
|
||||
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++)
|
||||
try {
|
||||
for(int r = getRow(startCellName)-1; r<getRow(endCellName); r++)
|
||||
for(int c = getCol(startCellName)-1; c<getCol(endCellName); c++)
|
||||
if(!cells[r][c].isEmpty()) {
|
||||
result += Long.parseLong(cells[r][c].getValue());
|
||||
counter ++;
|
||||
} catch (NumberFormatException | ArrayIndexOutOfBoundsException a ){}
|
||||
counter++;
|
||||
}
|
||||
result /= counter;
|
||||
return result;
|
||||
}
|
||||
|
@ -293,16 +291,16 @@ public class Spreadsheet {
|
|||
|
||||
double 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 r = getRow(startCellName)-1; r<getRow(endCellName); r++)
|
||||
for(int c = getCol(startCellName)-1; c<getCol(endCellName); c++)
|
||||
if(!cells[r][c].isEmpty()){
|
||||
avg += Double.parseDouble(cells[r][c].getValue());
|
||||
counter++;
|
||||
cellNames.add(getCellName(r, c));
|
||||
} catch (NumberFormatException | ArrayIndexOutOfBoundsException a ){}
|
||||
}
|
||||
avg /= counter;
|
||||
//average/ add cell names to list
|
||||
System.out.println(cellNames);
|
||||
|
||||
ArrayList<String> copyCellNames = new ArrayList<>(cellNames);
|
||||
double[] frequency = new double[cellNames.size()];
|
||||
Arrays.fill(frequency,1);
|
||||
|
@ -323,7 +321,7 @@ public class Spreadsheet {
|
|||
for(int i = 0; i< cellNames.size(); i++)
|
||||
relativeFrequency.add(i,frequency[i]/copyCellNames.size());
|
||||
|
||||
System.out.println("CellNames: "+cellNames);
|
||||
|
||||
if(get(cellNames.get(0)).isEmpty())
|
||||
mem = ((0 - avg)*(0 - avg))
|
||||
* relativeFrequency.get(0);
|
||||
|
@ -345,32 +343,34 @@ public class Spreadsheet {
|
|||
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++)
|
||||
for(int r = getRow(startCellName)-1; r<getRow(endCellName); r++)
|
||||
for(int c = getCol(startCellName)-1; c<getCol(endCellName); c++)
|
||||
if(!cells[r][c].isEmpty())
|
||||
cellNames.add(getCellName(r, c));
|
||||
|
||||
|
||||
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)));
|
||||
|
||||
try {
|
||||
if (result > Long.parseLong(get(cellNames.get(i))))
|
||||
result = Long.parseLong(get(cellNames.get(i)));
|
||||
} catch(NumberFormatException n){}
|
||||
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++)
|
||||
for(int r = getRow(startCellName)-1; r<getRow(endCellName); r++)
|
||||
for(int c = getCol(startCellName)-1; c<getCol(endCellName); c++)
|
||||
if(!cells[r][c].isEmpty())
|
||||
cellNames.add(getCellName(r, c));
|
||||
|
||||
|
||||
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)));
|
||||
try {
|
||||
if (result < Long.parseLong(get(cellNames.get(i))))
|
||||
result = Long.parseLong(get(cellNames.get(i)));
|
||||
} catch(NumberFormatException n){}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue