sum of a cell block with two digit rows

ERANZER 2024-01-05 12:31:59 +01:00
parent 9c5b38d9db
commit 859be3633e
2 changed files with 24 additions and 5 deletions

View File

@ -11,7 +11,7 @@ import java.util.Scanner;
public class Axel { public class Axel {
public static void main(String[] args) throws FileNotFoundException { public static void main(String[] args) throws FileNotFoundException {
Spreadsheet spr = new Spreadsheet(10,10); Spreadsheet spr = new Spreadsheet(20,10);
String cell, in; String cell, in;
Scanner kb = new Scanner(System.in); Scanner kb = new Scanner(System.in);

View File

@ -68,7 +68,10 @@ public class Spreadsheet {
} }
private int getRow(String cellName) { private int getRow(String cellName) {
return cellName.charAt(1) - '1'; if(cellName.length()==3)
return Integer.parseInt((cellName.charAt(1)+"")+(cellName.charAt(2)+""))-1;
else
return cellName.charAt(1) - '1';
} }
// ----- // -----
@ -121,7 +124,13 @@ public class Spreadsheet {
String result = ""; String result = "";
if (formula.startsWith("SUMME(")) // e.g. SUMME(A3:A8) if (formula.startsWith("SUMME(")) // e.g. SUMME(A3:A8)
result = "" + sum(formula.substring(6, 8), formula.substring(9, 11)); // TODO adapt to cells with two digits if(formula.length()==14)
result = "" + sum(formula.substring(6, 9), formula.substring(10, 13));
else if(formula.length()==13)
result = "" + sum(formula.substring(6, 8), formula.substring(9, 12));
else
result = "" + sum(formula.substring(6, 8), formula.substring(9, 11));
else if (formula.startsWith("PRODUKT(")) // e.g. PRODUKT(A3:B9) else if (formula.startsWith("PRODUKT(")) // e.g. PRODUKT(A3:B9)
result = "TODO"; // TODO result = "TODO"; // TODO
else if (formula.startsWith("MITTELWERT(")) // e.g. MITTELWERT(A3:A5) else if (formula.startsWith("MITTELWERT(")) // e.g. MITTELWERT(A3:A5)
@ -151,9 +160,19 @@ public class Spreadsheet {
*/ */
private long sum(String startCellName, String endCellName) { private long sum(String startCellName, String endCellName) {
int sum=0; int sum=0;
int startCellRow;
int endCellRow;
if(startCellName.length()==3)
startCellRow=Integer.parseInt((startCellName.charAt(1)+"")+(startCellName.charAt(2)+""))-1;
else
startCellRow=startCellName.charAt(1)-'1';
if(endCellName.length()==3)
endCellRow=Integer.parseInt((endCellName.charAt(1)+"")+(endCellName.charAt(2)+""))-1;
else
endCellRow=endCellName.charAt(1)-'1';
for(int i=startCellName.charAt(0)-'A'; i<=endCellName.charAt(0)-'A'; i++) { for(int i=startCellName.charAt(0)-'A'; i<=endCellName.charAt(0)-'A'; i++) {
for(int j=startCellName.charAt(1)-'1'; j<=endCellName.charAt(1)-'1'; j++) { for(int j=startCellRow; j<=endCellRow; j++) {
if(cells[i][j].isEmpty()) { if(cells[j][i].isEmpty()) {
continue; continue;
} }
sum+=Integer.parseInt(cells[j][i].getValue()); sum+=Integer.parseInt(cells[j][i].getValue());