Compare commits
7 Commits
Author | SHA1 | Date |
---|---|---|
Sebastian Tews | 95913e847f | |
Sebastian Tews | 9b12aa90e1 | |
Sebastian Tews | 091418cbde | |
Sebastian Tews | 98942ba56d | |
Sebastian Tews | 0c46be39b4 | |
Sebastian Tews | df6ff99a0c | |
Sebastian Tews | 49d70f71c3 |
|
@ -1,4 +1,20 @@
|
|||
1,2
|
||||
3,4
|
||||
5,6
|
||||
7,8
|
||||
Hallo,,,,,,,,,,,,,,,,,,,
|
||||
1,,7,,,,,,,,,,,,,,,,,
|
||||
55,7,,4,,,,,,,,,,,,,,,,
|
||||
9,,,,,,,,,,,,,,,,,,,
|
||||
1,,,,,,,,=7*6,=7*6,,,,,,,,,,
|
||||
,,,,,,,,,=3/2,,,,,,,,,,
|
||||
,,,,,,,,,,,,,,,,,,,
|
||||
,,,,,,,,,,,,,,,,,,,
|
||||
,=41+A2,,,,,,,,,,,,,,,,,,
|
||||
,,,,,,,,,,,,,,,,,,,
|
||||
,,,,,,,,,,,,,,,,,,,
|
||||
,,,,,,,,,LL,,,,,,,,,,
|
||||
,,,,,,,,,,,,,,,,,,,
|
||||
,,,,,,,,,,,,,,,,,,,
|
||||
,,,,,,,Z1,,,,,,,,,,,,
|
||||
,,,,,,,,,,,,,,,,,,,
|
||||
,,,,,,,,,,,,,,,,,,,
|
||||
,,,,,,,,,,,,,,,,,,,
|
||||
,,,,,,,,,,,,,,,,,,,
|
||||
20,,,,,,,,,,,,,,,,,,,
|
||||
|
|
|
|
@ -1,5 +1,5 @@
|
|||
package de.hs_mannheim.informatik.spreadsheet;
|
||||
|
||||
import java.util.Scanner;
|
||||
import java.io.FileNotFoundException;
|
||||
|
||||
/**
|
||||
|
@ -10,9 +10,34 @@ import java.io.FileNotFoundException;
|
|||
public class Axel {
|
||||
|
||||
public static void main(String[] args) throws FileNotFoundException {
|
||||
Spreadsheet spr = new Spreadsheet(10,10);
|
||||
Scanner sc = new Scanner(System.in);
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
System.out.println("Wie viele Zeilen soll das Spreadsheet haben? (1-99)");
|
||||
|
||||
spr.put("A3", "123");
|
||||
int eingabeR = scanner.nextInt();
|
||||
if (eingabeR > 99) {
|
||||
System.out.println("Eingabe überschreitet das Maximum. Eingabe wird zu 99 gesetzt");
|
||||
eingabeR = 99;
|
||||
} else {
|
||||
System.out.println("Eingabe ist möglich");
|
||||
}
|
||||
System.out.println("Wie viele Spalten soll das Spreadsheet haben? (1-26)");
|
||||
int eingabeC = scanner.nextInt();
|
||||
if (eingabeC > 26) {
|
||||
System.out.println("Eingabe überschreitet das Maximum. Eingabe wird zu 26 gesetzt");
|
||||
eingabeC = 26;
|
||||
} else {
|
||||
System.out.println("Eingabe ist möglich");
|
||||
}
|
||||
//Begrenzung der Zeilen und Spalten größe durch if Abfrage.
|
||||
// Wert wird automatisch bei Überschreitung zum Maximum
|
||||
|
||||
//Tabellengröße anpassen nach "Wahl"
|
||||
Spreadsheet spr = new Spreadsheet(eingabeR, eingabeC);
|
||||
spr.readCsv("C:\\Users\\sebas\\IdeaProjects\\PR1-Spreadsheet\\Axel\\resources\\zahlen.csv",',', "A1");
|
||||
// csv auslesen
|
||||
|
||||
//spr.put("A1", "123");
|
||||
spr.put("A2", "1");
|
||||
|
||||
spr.put("B9", "=41+A2");
|
||||
|
@ -21,9 +46,54 @@ public class Axel {
|
|||
|
||||
System.out.println(spr);
|
||||
|
||||
spr.saveCsv("/tmp/test.csv");
|
||||
//spr.saveCsv("C:\\Users\\sebas\\IdeaProjects\\PR1-Spreadsheet\\Axel\\resources\\zahlen.csv");
|
||||
|
||||
// TODO: You might want to put "UI loop" for entering value and formulas here resp. in some UI methods.
|
||||
|
||||
System.out.println("Möchten Sie, die Tabelle bearbeiten (j/n)");
|
||||
|
||||
String eingabe = "";
|
||||
String eingabeCR = "";
|
||||
String eingabeValue = "";
|
||||
|
||||
|
||||
do {
|
||||
eingabe = sc.nextLine();
|
||||
if (eingabe.equals("j")) {
|
||||
System.out.println("Geben Sie die Spalte (A-Z) und die Zeile (1-99)");
|
||||
eingabeCR = sc.nextLine();
|
||||
System.out.println("Weisen Sie jetzt einen Wert zu");
|
||||
eingabeValue = sc.nextLine();
|
||||
spr.put(eingabeCR, eingabeValue);
|
||||
System.out.println();
|
||||
System.out.println(spr);
|
||||
System.out.println("Möchten Sie weiter machen(j/n)?");
|
||||
|
||||
} else if (eingabe.equals("n")) {
|
||||
//System.out.println("Das Programm wird beendet");
|
||||
break;
|
||||
|
||||
} else {
|
||||
System.out.println("Falsche eingabe bitte erneut versuchen");
|
||||
}
|
||||
} while (true);
|
||||
System.out.println("Möchten Sie die Änderungen an der Tabelle speichern? (j/n)");
|
||||
eingabe = sc.nextLine();
|
||||
if(eingabe.equals("j"))
|
||||
{
|
||||
spr.saveCsv("C:\\Users\\sebas\\IdeaProjects\\PR1-Spreadsheet\\Axel\\resources\\zahlen.csv");
|
||||
System.out.println("Das Programm wird beendet");
|
||||
} else{
|
||||
System.out.println("Tabelle wird nicht gespeichert. Das Programm wird beendet");
|
||||
}
|
||||
|
||||
// Speichern der Tabelle bzw Inhalt der Textdatei(Änderungen)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -1,8 +1,13 @@
|
|||
package de.hs_mannheim.informatik.spreadsheet;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Scanner;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
|
@ -29,6 +34,8 @@ public class Spreadsheet {
|
|||
for (int r = 0; r < rows; r++)
|
||||
for (int c = 0; c < cols; c++)
|
||||
cells[r][c] = new Cell();
|
||||
//Fehlermeldung wenn es über 99 Zeilen und 26 Spalten sind
|
||||
//Oben genanntes bringt nichts. Bugfix durch zurücksetzen auf vorherigen Wert. Fehlermeldung unnötig durch if Abfrage.
|
||||
}
|
||||
|
||||
// -----
|
||||
|
@ -62,7 +69,14 @@ public class Spreadsheet {
|
|||
}
|
||||
|
||||
private int getRow(String cellName) {
|
||||
return cellName.charAt(1) - '1';
|
||||
int ergebnis = 0;
|
||||
for (int i = 1; i < cellName.length() ; i++) {
|
||||
ergebnis = (ergebnis * 10) + cellName.charAt(i) - '0';
|
||||
|
||||
|
||||
}
|
||||
return ergebnis-1;
|
||||
|
||||
}
|
||||
|
||||
// -----
|
||||
|
@ -72,13 +86,46 @@ public class Spreadsheet {
|
|||
* A method for reading in data from a CSV file.
|
||||
* @param path The file to read.
|
||||
* @param separator The char used to split up the input, e.g. a comma or a semicolon.
|
||||
* @param starCellName The upper left cell where data from the CSV file should be inserted.
|
||||
* @param startCellName The upper left cell where data from the CSV file should be inserted.
|
||||
* @return Nothing.
|
||||
* @exception IOException If path does not exist.
|
||||
*/
|
||||
public void readCsv(String path, char separator, String startCellName) throws FileNotFoundException {
|
||||
// TODO: implement this
|
||||
ArrayList<String> lines = readFile(path);
|
||||
String[] seperateLines = new String[lines.size()];
|
||||
int startingCellRow = getRow(startCellName);
|
||||
int startingCellCol = getCol(startCellName);
|
||||
String[][] quickSave = new String[cells.length - startingCellRow][cells[0].length - startingCellCol];
|
||||
for (int i = 0; i < seperateLines.length; i++) {
|
||||
quickSave[i] = lines.get(i).split(separator+""); //Seperator erstellt
|
||||
for (int j = 0; j < quickSave[i].length; j++) {
|
||||
put(startingCellRow+i,startingCellCol+j,quickSave[i][j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param path Dateipfad der Datei die von der Methode ausgelesen werden soll
|
||||
* @return lines
|
||||
* @throws FileNotFoundException
|
||||
*/
|
||||
public static ArrayList<String> readFile(String path) throws FileNotFoundException {
|
||||
ArrayList<String> lines = new ArrayList<>();
|
||||
Scanner sc = new Scanner(new File(path));
|
||||
|
||||
while (sc.hasNextLine()) {
|
||||
lines.add(sc.nextLine());
|
||||
}
|
||||
|
||||
sc.close();
|
||||
|
||||
return lines;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* A method for saving data to a CSV file.
|
||||
|
@ -106,9 +153,10 @@ public class Spreadsheet {
|
|||
}
|
||||
|
||||
/**
|
||||
* This method does the actual evaluation/calcluation of a specific cell
|
||||
* @param cellName the name of the cell to be evaluated
|
||||
* @return Nothing.
|
||||
* Methode welche die Eingabe überprüft und anhand dessen, dann auf abgebildete Methoden zugreift
|
||||
* und das Ergebnis wiedergibt
|
||||
* @param row steht für die Zeile
|
||||
* @param col steht für die Spalte
|
||||
*/
|
||||
private void evaluateCell(int row, int col) {
|
||||
String formula = cells[row][col].getFormula();
|
||||
|
@ -117,15 +165,15 @@ public class Spreadsheet {
|
|||
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
|
||||
else if (formula.startsWith("PRODUKT(")) // e.g. PRODUKT(A3:B9)
|
||||
result = "TODO"; // TODO
|
||||
result = "" + multiplier(formula.substring(8, 10), formula.substring(11, 13)); // TODO
|
||||
else if (formula.startsWith("MITTELWERT(")) // e.g. MITTELWERT(A3:A5)
|
||||
result = "TODO"; // TODO
|
||||
result = "" + mittelwert(formula.substring(11, 13), formula.substring(14, 16)); // TODO
|
||||
else if (formula.startsWith("STABW(")) // e.g. STABW(C6:D8) -> Standardabweichung
|
||||
result = "TODO"; // TODO
|
||||
else if (formula.startsWith("MIN(")) // e.g. MIN(C13:H13) -> größter Wert
|
||||
result = "TODO"; // TODO
|
||||
result = "" + min(formula.substring(4, 6), formula.substring(7, 9)); // TODO
|
||||
else if (formula.startsWith("MAX(")) // e.g. MAX(A1:A10) -> Standardabweichung
|
||||
result = "TODO"; // TODO
|
||||
result = "" + max(formula.substring(4, 6), formula.substring(7, 9)); // TODO
|
||||
else if (!formula.isEmpty()) {
|
||||
try {
|
||||
result = "" + calculate(formula);
|
||||
|
@ -136,7 +184,7 @@ public class Spreadsheet {
|
|||
|
||||
cells[row][col].setValue("" + result);
|
||||
}
|
||||
|
||||
//substring start und ende auf die entsprechende Wortlänge bei der Bedingung anpassen
|
||||
/**
|
||||
* Method for calculating the sum of a rectangular block of cells, such as from A1 to B3.
|
||||
* @param startCellName The name of the cell in the upper left corner of the rectangle.
|
||||
|
@ -144,10 +192,128 @@ public class Spreadsheet {
|
|||
* @return The sum calculated.
|
||||
*/
|
||||
private long sum(String startCellName, String endCellName) {
|
||||
|
||||
int startRow = getRow(startCellName);
|
||||
int startCol = getCol(startCellName);
|
||||
int endRow = getRow(endCellName);
|
||||
int endCol = getCol(endCellName);
|
||||
|
||||
long result = 0;
|
||||
|
||||
for (int i = startRow; i <= endRow; i++) {
|
||||
for (int j = startCol; j <= endCol; j++) {
|
||||
result += (Integer.parseInt(cells[i][j].getValue()));
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Methode zum berechnen des Produkts eines viereckigen Blocks aus Zellen. Beispiel A2 bis B2
|
||||
* @param startCellName The name of the cell in the upper left corner of the rectangle.
|
||||
* @param endCellName The name of the cell in the lower right corner of the rectangle.
|
||||
* @return das Produkt. Hier result
|
||||
*/
|
||||
private long multiplier(String startCellName, String endCellName) {
|
||||
|
||||
int startRow = getRow(startCellName);
|
||||
int startCol = getCol(startCellName);
|
||||
int endRow = getRow(endCellName);
|
||||
int endCol = getCol(endCellName);
|
||||
|
||||
long result = 1; // 1 da hier Multiplikation startet
|
||||
|
||||
for (int i = startRow; i <= endRow; i++) {
|
||||
for (int j = startCol; j <= endCol; j++) {
|
||||
result *= (Integer.parseInt(cells[i][j].getValue()));
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
/**
|
||||
* Methode zum berechnen des Mittelwerts eines viereckigen Blocks aus Zellen. Beispiel A2 bis B2
|
||||
* @param startCellName The name of the cell in the upper left corner of the rectangle.
|
||||
* @param endCellName The name of the cell in the lower right corner of the rectangle.
|
||||
* @return der Mittelwert. Hier result
|
||||
*/
|
||||
private long mittelwert(String startCellName, String endCellName) {
|
||||
|
||||
int startRow = getRow(startCellName);
|
||||
int startCol = getCol(startCellName);
|
||||
int endRow = getRow(endCellName);
|
||||
int endCol = getCol(endCellName);
|
||||
int counter = 0;
|
||||
long summe = 0;
|
||||
|
||||
long result = 0;
|
||||
|
||||
for (int i = startRow; i <= endRow; i++) {
|
||||
for (int j = startCol; j <= endCol; j++) {
|
||||
counter++;
|
||||
summe += (Integer.parseInt(cells[i][j].getValue()));
|
||||
}
|
||||
}
|
||||
result = summe / counter;
|
||||
|
||||
return result;
|
||||
}
|
||||
/**
|
||||
* Methode zum berechnen des Minimums eines viereckigen Blocks aus Zellen. Beispiel A2 bis B2
|
||||
* @param startCellName The name of the cell in the upper left corner of the rectangle.
|
||||
* @param endCellName The name of the cell in the lower right corner of the rectangle.
|
||||
* @return die kleinste Zahl. Hier result
|
||||
*/
|
||||
private long min(String startCellName, String endCellName) {
|
||||
int startRow = getRow(startCellName);
|
||||
int startCol = getCol(startCellName);
|
||||
int endRow = getRow(endCellName);
|
||||
int endCol = getCol(endCellName);
|
||||
long result = 0;
|
||||
|
||||
for (int i = startRow; i <= endRow; i++) {
|
||||
for (int j = startCol; j <= endCol; j++) {
|
||||
|
||||
result = (Integer.parseInt(cells[i][j].getValue()));
|
||||
if(result > (Integer.parseInt(cells[i][j].getValue()))) //größer result für min Wert
|
||||
{
|
||||
result = (Integer.parseInt(cells[i][j].getValue()));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
/**
|
||||
* Methode zum berechnen des Maximums eines viereckigen Blocks aus Zellen. Beispiel A2 bis B2
|
||||
* @param startCellName The name of the cell in the upper left corner of the rectangle.
|
||||
* @param endCellName The name of the cell in the lower right corner of the rectangle.
|
||||
* @return das Maximum. Hier result
|
||||
*/
|
||||
private long max(String startCellName, String endCellName) {
|
||||
int startRow = getRow(startCellName);
|
||||
int startCol = getCol(startCellName);
|
||||
int endRow = getRow(endCellName);
|
||||
int endCol = getCol(endCellName);
|
||||
long result = 0;
|
||||
|
||||
for (int i = startRow; i <= endRow; i++) {
|
||||
for (int j = startCol; j <= endCol; j++) {
|
||||
|
||||
if(result < (Integer.parseInt(cells[i][j].getValue()))) // kleiner result für max Wert
|
||||
{
|
||||
result = (Integer.parseInt(cells[i][j].getValue()));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
// TODO implement
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method calculates the result of a "normal" algebraic expression. It only needs to support
|
||||
|
@ -158,24 +324,94 @@ public class Spreadsheet {
|
|||
* @param formula The expression to be evaluated.
|
||||
* @return The result calculated.
|
||||
*/
|
||||
private long calculate(String formula) throws ArithmeticException {
|
||||
|
||||
public long calculate(String formula) throws ArithmeticException {
|
||||
Matcher m = Pattern.compile("([A-Z][0-9]*)|[-\\+\\*/]|[0-9]*").matcher(formula);
|
||||
|
||||
long res = 0;
|
||||
long result = 0;
|
||||
int zwischenspeicher = 0;
|
||||
int counter = 0;
|
||||
int newNumber = 0;
|
||||
|
||||
// TODO implement
|
||||
|
||||
// uncomment the following to see an example how the elements of a formula can be accessed
|
||||
|
||||
while (m.find()) { // m.find() must always be used before m.group()
|
||||
String s = m.group();
|
||||
if (!s.isEmpty()) {
|
||||
System.out.println(s);
|
||||
//System.out.println(s);
|
||||
counter++;
|
||||
|
||||
switch (counter){
|
||||
case 1:
|
||||
try {
|
||||
result = Integer.parseInt(s);
|
||||
}catch (Exception ignored){
|
||||
result = Integer.parseInt(get(s));
|
||||
}
|
||||
break;
|
||||
case 2:
|
||||
switch(s) {
|
||||
case "+":
|
||||
zwischenspeicher = 1;
|
||||
break;
|
||||
case "-":
|
||||
zwischenspeicher = 2;
|
||||
break;
|
||||
case "*":
|
||||
zwischenspeicher = 3;
|
||||
break;
|
||||
case "/":
|
||||
zwischenspeicher = 4;
|
||||
break;
|
||||
default:
|
||||
System.out.println("ERROR");
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case 3:
|
||||
try {
|
||||
newNumber = Integer.parseInt(s);
|
||||
}catch (Exception ignored){
|
||||
newNumber = Integer.parseInt(get(s));
|
||||
}
|
||||
result = rechner(result, zwischenspeicher, newNumber);
|
||||
|
||||
counter = 1;
|
||||
break;
|
||||
default:
|
||||
System.out.println("ERROR");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return res;
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param a steht für die Zahl "a"
|
||||
* @param auswahl steht für die entsprechende Rechnung
|
||||
* @param b steht für die Zahl "b"
|
||||
* @return die Rechnung
|
||||
*/
|
||||
public static long rechner(long a, int auswahl, int b) {
|
||||
switch(auswahl){
|
||||
case 1:
|
||||
return a + b;
|
||||
case 2:
|
||||
return a - b;
|
||||
case 3:
|
||||
return a * b;
|
||||
case 4:
|
||||
return a / b;
|
||||
default:
|
||||
System.out.println("ERROR");
|
||||
break;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
// -----
|
||||
|
||||
public String toString() {
|
||||
|
|
|
@ -0,0 +1,26 @@
|
|||
package de.hs_mannheim.informatik.spreadsheet;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class SpreadsheetTest {
|
||||
|
||||
@org.junit.jupiter.api.Test
|
||||
void testCalculate() {
|
||||
Spreadsheet spr = new Spreadsheet(25,25); // Irgendeine Größe einstellen des Spreadsheets
|
||||
|
||||
assertEquals(spr.calculate("2+2"), 4);
|
||||
assertEquals(spr.calculate("3*3"), 9);
|
||||
assertEquals(spr.calculate("6/2"), 3);
|
||||
|
||||
|
||||
|
||||
spr.put("A1", "6");
|
||||
spr.put("A2", "2");
|
||||
|
||||
assertEquals(spr.calculate("A1+A2"), 8);
|
||||
assertEquals(spr.calculate("A1*A2"), 12);
|
||||
assertEquals(spr.calculate("A1/A2"), 3);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue