forked from hummel/PR1-Spreadsheet
Compare commits
No commits in common. "main" and "main" have entirely different histories.
|
@ -1,20 +1,4 @@
|
||||||
Hallo,,,,,,,,,,,,,,,,,,,
|
1,2
|
||||||
1,,7,,,,,,,,,,,,,,,,,
|
3,4
|
||||||
55,7,,4,,,,,,,,,,,,,,,,
|
5,6
|
||||||
9,,,,,,,,,,,,,,,,,,,
|
7,8
|
||||||
1,,,,,,,,=7*6,=7*6,,,,,,,,,,
|
|
||||||
,,,,,,,,,=3/2,,,,,,,,,,
|
|
||||||
,,,,,,,,,,,,,,,,,,,
|
|
||||||
,,,,,,,,,,,,,,,,,,,
|
|
||||||
,=41+A2,,,,,,,,,,,,,,,,,,
|
|
||||||
,,,,,,,,,,,,,,,,,,,
|
|
||||||
,,,,,,,,,,,,,,,,,,,
|
|
||||||
,,,,,,,,,LL,,,,,,,,,,
|
|
||||||
,,,,,,,,,,,,,,,,,,,
|
|
||||||
,,,,,,,,,,,,,,,,,,,
|
|
||||||
,,,,,,,Z1,,,,,,,,,,,,
|
|
||||||
,,,,,,,,,,,,,,,,,,,
|
|
||||||
,,,,,,,,,,,,,,,,,,,
|
|
||||||
,,,,,,,,,,,,,,,,,,,
|
|
||||||
,,,,,,,,,,,,,,,,,,,
|
|
||||||
20,,,,,,,,,,,,,,,,,,,
|
|
||||||
|
|
|
|
@ -1,5 +1,5 @@
|
||||||
package de.hs_mannheim.informatik.spreadsheet;
|
package de.hs_mannheim.informatik.spreadsheet;
|
||||||
import java.util.Scanner;
|
|
||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -10,34 +10,9 @@ import java.io.FileNotFoundException;
|
||||||
public class Axel {
|
public class Axel {
|
||||||
|
|
||||||
public static void main(String[] args) throws FileNotFoundException {
|
public static void main(String[] args) throws FileNotFoundException {
|
||||||
Scanner sc = new Scanner(System.in);
|
Spreadsheet spr = new Spreadsheet(10,10);
|
||||||
Scanner scanner = new Scanner(System.in);
|
|
||||||
System.out.println("Wie viele Zeilen soll das Spreadsheet haben? (1-99)");
|
|
||||||
|
|
||||||
int eingabeR = scanner.nextInt();
|
spr.put("A3", "123");
|
||||||
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("A2", "1");
|
||||||
|
|
||||||
spr.put("B9", "=41+A2");
|
spr.put("B9", "=41+A2");
|
||||||
|
@ -46,54 +21,9 @@ public class Axel {
|
||||||
|
|
||||||
System.out.println(spr);
|
System.out.println(spr);
|
||||||
|
|
||||||
//spr.saveCsv("C:\\Users\\sebas\\IdeaProjects\\PR1-Spreadsheet\\Axel\\resources\\zahlen.csv");
|
spr.saveCsv("/tmp/test.csv");
|
||||||
|
|
||||||
// TODO: You might want to put "UI loop" for entering value and formulas here resp. in some UI methods.
|
// 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,13 +1,8 @@
|
||||||
package de.hs_mannheim.informatik.spreadsheet;
|
package de.hs_mannheim.informatik.spreadsheet;
|
||||||
|
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.PrintWriter;
|
import java.io.PrintWriter;
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Scanner;
|
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
@ -34,8 +29,6 @@ public class Spreadsheet {
|
||||||
for (int r = 0; r < rows; r++)
|
for (int r = 0; r < rows; r++)
|
||||||
for (int c = 0; c < cols; c++)
|
for (int c = 0; c < cols; c++)
|
||||||
cells[r][c] = new Cell();
|
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.
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// -----
|
// -----
|
||||||
|
@ -69,14 +62,7 @@ public class Spreadsheet {
|
||||||
}
|
}
|
||||||
|
|
||||||
private int getRow(String cellName) {
|
private int getRow(String cellName) {
|
||||||
int ergebnis = 0;
|
return cellName.charAt(1) - '1';
|
||||||
for (int i = 1; i < cellName.length() ; i++) {
|
|
||||||
ergebnis = (ergebnis * 10) + cellName.charAt(i) - '0';
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
return ergebnis-1;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// -----
|
// -----
|
||||||
|
@ -86,47 +72,14 @@ public class Spreadsheet {
|
||||||
* A method for reading in data from a CSV file.
|
* A method for reading in data from a CSV file.
|
||||||
* @param path The file to read.
|
* @param path The file to read.
|
||||||
* @param separator The char used to split up the input, e.g. a comma or a semicolon.
|
* @param separator The char used to split up the input, e.g. a comma or a semicolon.
|
||||||
* @param startCellName The upper left cell where data from the CSV file should be inserted.
|
* @param starCellName The upper left cell where data from the CSV file should be inserted.
|
||||||
* @return Nothing.
|
* @return Nothing.
|
||||||
* @exception IOException If path does not exist.
|
* @exception IOException If path does not exist.
|
||||||
*/
|
*/
|
||||||
public void readCsv(String path, char separator, String startCellName) throws FileNotFoundException {
|
public void readCsv(String path, char separator, String startCellName) throws FileNotFoundException {
|
||||||
// TODO: implement this
|
// 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.
|
* A method for saving data to a CSV file.
|
||||||
* @param path The file to write.
|
* @param path The file to write.
|
||||||
|
@ -153,27 +106,26 @@ public class Spreadsheet {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Methode welche die Eingabe überprüft und anhand dessen, dann auf abgebildete Methoden zugreift
|
* This method does the actual evaluation/calcluation of a specific cell
|
||||||
* und das Ergebnis wiedergibt
|
* @param cellName the name of the cell to be evaluated
|
||||||
* @param row steht für die Zeile
|
* @return Nothing.
|
||||||
* @param col steht für die Spalte
|
|
||||||
*/
|
*/
|
||||||
private void evaluateCell(int row, int col) {
|
private void evaluateCell(int row, int col) {
|
||||||
String formula = cells[row][col].getFormula();
|
String formula = cells[row][col].getFormula();
|
||||||
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
|
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)
|
else if (formula.startsWith("PRODUKT(")) // e.g. PRODUKT(A3:B9)
|
||||||
result = "" + multiplier(formula.substring(8, 10), formula.substring(11, 13)); // TODO
|
result = "TODO"; // TODO
|
||||||
else if (formula.startsWith("MITTELWERT(")) // e.g. MITTELWERT(A3:A5)
|
else if (formula.startsWith("MITTELWERT(")) // e.g. MITTELWERT(A3:A5)
|
||||||
result = "" + mittelwert(formula.substring(11, 13), formula.substring(14, 16)); // TODO
|
result = "TODO"; // TODO
|
||||||
else if (formula.startsWith("STABW(")) // e.g. STABW(C6:D8) -> Standardabweichung
|
else if (formula.startsWith("STABW(")) // e.g. STABW(C6:D8) -> Standardabweichung
|
||||||
result = "TODO"; // TODO
|
result = "TODO"; // TODO
|
||||||
else if (formula.startsWith("MIN(")) // e.g. MIN(C13:H13) -> größter Wert
|
else if (formula.startsWith("MIN(")) // e.g. MIN(C13:H13) -> größter Wert
|
||||||
result = "" + min(formula.substring(4, 6), formula.substring(7, 9)); // TODO
|
result = "TODO"; // TODO
|
||||||
else if (formula.startsWith("MAX(")) // e.g. MAX(A1:A10) -> Standardabweichung
|
else if (formula.startsWith("MAX(")) // e.g. MAX(A1:A10) -> Standardabweichung
|
||||||
result = "" + max(formula.substring(4, 6), formula.substring(7, 9)); // TODO
|
result = "TODO"; // TODO
|
||||||
else if (!formula.isEmpty()) {
|
else if (!formula.isEmpty()) {
|
||||||
try {
|
try {
|
||||||
result = "" + calculate(formula);
|
result = "" + calculate(formula);
|
||||||
|
@ -184,7 +136,7 @@ public class Spreadsheet {
|
||||||
|
|
||||||
cells[row][col].setValue("" + result);
|
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.
|
* 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.
|
* @param startCellName The name of the cell in the upper left corner of the rectangle.
|
||||||
|
@ -192,128 +144,10 @@ public class Spreadsheet {
|
||||||
* @return The sum calculated.
|
* @return The sum calculated.
|
||||||
*/
|
*/
|
||||||
private long sum(String startCellName, String endCellName) {
|
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
|
// TODO implement
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method calculates the result of a "normal" algebraic expression. It only needs to support
|
* This method calculates the result of a "normal" algebraic expression. It only needs to support
|
||||||
|
@ -324,94 +158,24 @@ public class Spreadsheet {
|
||||||
* @param formula The expression to be evaluated.
|
* @param formula The expression to be evaluated.
|
||||||
* @return The result calculated.
|
* @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);
|
Matcher m = Pattern.compile("([A-Z][0-9]*)|[-\\+\\*/]|[0-9]*").matcher(formula);
|
||||||
|
|
||||||
long result = 0;
|
long res = 0;
|
||||||
int zwischenspeicher = 0;
|
|
||||||
int counter = 0;
|
|
||||||
int newNumber = 0;
|
|
||||||
|
|
||||||
// TODO implement
|
// 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()
|
while (m.find()) { // m.find() must always be used before m.group()
|
||||||
String s = m.group();
|
String s = m.group();
|
||||||
if (!s.isEmpty()) {
|
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 result;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
* @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() {
|
public String toString() {
|
||||||
|
|
|
@ -1,26 +0,0 @@
|
||||||
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