1
0
Fork 0

Fehlende Dateien hochladen

master
devra 2024-01-06 15:58:12 +01:00
parent e5190d7c9b
commit beaf0b5b9e
2 changed files with 81 additions and 34 deletions

View File

@ -1,10 +1,10 @@
,,,,,,,,,
13,=69+420,,,,,,,,
1,,,,,,,,,
123,,,,,,,,,
,,,,,,,,,
,,,,,,,,,=7*6
,,,,,,,,,=3/2
,,,,,,,,,
,,,,,,,,,
,=41+A2,,,,,,,,
,,,,,,,,,
,,,,,,,,,
,,,,,,,,,
,,,,,,,,,

1 13 =69+420
2 1 1
3 123 123
4
=7*6
=3/2
5
6
7 =41+A2
8
9
10

View File

@ -3,6 +3,7 @@ package de.hs_mannheim.informatik.spreadsheet;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@ -39,8 +40,14 @@ public class Spreadsheet {
}
public String get(String cellName) {
cellName = cellName.toUpperCase();
return get(getRow(cellName), getCol(cellName));
if(containsOnlyNumbers(cellName)) {
return cellName;
}
else {
cellName = cellName.toUpperCase();
return get(getRow(cellName), getCol(cellName));
}
}
private void put(int row, int col, String value) {
@ -128,7 +135,7 @@ public class Spreadsheet {
result = "TODO"; // TODO
else if (!formula.isEmpty()) {
try {
result = "" + calculate(formula);
result = "" + calculate(formula); //calculates the result of a "normal" algebraic expression.
} catch(ArithmeticException ae) {
result = "exc.";
}
@ -149,35 +156,75 @@ public class Spreadsheet {
return 0;
}
/**
* This method calculates the result of a "normal" algebraic expression. It only needs to support
* expressions like =B4 or =2+A3-B2, i.e. only with int numbers and other cells and with plus,
* minus, times, split only. An expression always starts with either a number or a cell name. If it
* continues, it is guaranteed that this is followed by an operator and either a number or a
* cell name again. It is NOT required to implement dot before dash or parentheses in formulas.
* @param formula The expression to be evaluated.
* @return The result calculated.
*/
private long calculate(String formula) throws ArithmeticException {
Matcher m = Pattern.compile("([A-Z][0-9]*)|[-\\+\\*/]|[0-9]*").matcher(formula);
/**
* This method calculates the result of a "normal" algebraic expression. It only needs to support
* expressions like =B4 or =2+A3-B2, i.e. only with int numbers and other cells and with plus,
* minus, times, split only. An expression always starts with either a number or a cell name. If it
* continues, it is guaranteed that this is followed by an operator and either a number or a
* cell name again. It is NOT required to implement dot before dash or parentheses in formulas.
* @param formula The expression to be evaluated.
* @return The result calculated.
*/
private long calculate(String formula) throws ArithmeticException {
System.out.println(formula);
Matcher m = Pattern.compile("([A-Z][0-9]*)|[-\\+\\*/]|[0-9]*").matcher(formula);
long res = 0;
// TODO implement
ArrayList<String> zahlen = new ArrayList<>();
// 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);
while(m.find()) {
String s = m.group();
if (!s.isEmpty()) {
zahlen.add(s);
}
}
int ersteZahl = Integer.parseInt(get(zahlen.get(0)));
int zweiteZahl=0;
for (int i = 0; i < zahlen.size(); i+=2) {
if(i<zahlen.size()-2) {
String operant=zahlen.get(i+1);
zweiteZahl=Integer.parseInt(get(zahlen.get(i+2)));
switch(operant) {
case "+":
ersteZahl=ersteZahl+zweiteZahl;
break;
case "-":
ersteZahl=ersteZahl-zweiteZahl;
break;
case "*":
ersteZahl=ersteZahl*zweiteZahl;
break;
case "/":
ersteZahl=ersteZahl/zweiteZahl;
break;
}
}
}
System.out.print(ersteZahl);
System.out.println(" ");
return ersteZahl;
}
return res;
}
// -----
/**
* This method checks if a given string contains one or more digits
* to make sure that the string consists of only numbers,
*/
public boolean containsOnlyNumbers(String input) {
return input.matches("[0-9]+");
}
public String toString() {
StringBuilder sb = new StringBuilder();
@ -188,11 +235,11 @@ public class Spreadsheet {
int rc = 1;
for (Cell[] r : cells) {
sb.append(System.lineSeparator());
sb.append(String.format("%2s", rc++) + ": ");
sb.append(System.lineSeparator()); //newLine
sb.append(String.format("%2s", rc++) + ": "); //Number:
for (Cell c : r) {
sb.append(c + " | ");
sb.append(c + " | "); //cell value
}
}