From 859be3633e6bc6033191ca134030b2619c6b1d6e Mon Sep 17 00:00:00 2001 From: ERANZER Date: Fri, 5 Jan 2024 12:31:59 +0100 Subject: [PATCH] sum of a cell block with two digit rows --- .../informatik/spreadsheet/Axel.java | 2 +- .../informatik/spreadsheet/Spreadsheet.java | 27 ++++++++++++++++--- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/Axel/src/de/hs_mannheim/informatik/spreadsheet/Axel.java b/Axel/src/de/hs_mannheim/informatik/spreadsheet/Axel.java index ce1f56e..ab96d88 100644 --- a/Axel/src/de/hs_mannheim/informatik/spreadsheet/Axel.java +++ b/Axel/src/de/hs_mannheim/informatik/spreadsheet/Axel.java @@ -11,7 +11,7 @@ import java.util.Scanner; public class Axel { public static void main(String[] args) throws FileNotFoundException { - Spreadsheet spr = new Spreadsheet(10,10); + Spreadsheet spr = new Spreadsheet(20,10); String cell, in; Scanner kb = new Scanner(System.in); diff --git a/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java b/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java index f83c5f5..f650f37 100644 --- a/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java +++ b/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java @@ -68,7 +68,10 @@ public class Spreadsheet { } 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 = ""; 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) result = "TODO"; // TODO else if (formula.startsWith("MITTELWERT(")) // e.g. MITTELWERT(A3:A5) @@ -151,9 +160,19 @@ public class Spreadsheet { */ private long sum(String startCellName, String endCellName) { 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 j=startCellName.charAt(1)-'1'; j<=endCellName.charAt(1)-'1'; j++) { - if(cells[i][j].isEmpty()) { + for(int j=startCellRow; j<=endCellRow; j++) { + if(cells[j][i].isEmpty()) { continue; } sum+=Integer.parseInt(cells[j][i].getValue());