forked from hummel/PR1-Spreadsheet
Fehlende Dateien hochladen
parent
e5190d7c9b
commit
beaf0b5b9e
|
@ -1,10 +1,10 @@
|
||||||
,,,,,,,,,
|
13,=69+420,,,,,,,,
|
||||||
1,,,,,,,,,
|
1,,,,,,,,,
|
||||||
123,,,,,,,,,
|
123,,,,,,,,,
|
||||||
,,,,,,,,,
|
,,,,,,,,,
|
||||||
,,,,,,,,,=7*6
|
|
||||||
,,,,,,,,,=3/2
|
|
||||||
,,,,,,,,,
|
,,,,,,,,,
|
||||||
,,,,,,,,,
|
,,,,,,,,,
|
||||||
,=41+A2,,,,,,,,
|
,,,,,,,,,
|
||||||
|
,,,,,,,,,
|
||||||
|
,,,,,,,,,
|
||||||
,,,,,,,,,
|
,,,,,,,,,
|
||||||
|
|
|
|
@ -3,6 +3,7 @@ package de.hs_mannheim.informatik.spreadsheet;
|
||||||
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.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
@ -39,8 +40,14 @@ public class Spreadsheet {
|
||||||
}
|
}
|
||||||
|
|
||||||
public String get(String cellName) {
|
public String get(String cellName) {
|
||||||
cellName = cellName.toUpperCase();
|
if(containsOnlyNumbers(cellName)) {
|
||||||
return get(getRow(cellName), getCol(cellName));
|
return cellName;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
cellName = cellName.toUpperCase();
|
||||||
|
return get(getRow(cellName), getCol(cellName));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void put(int row, int col, String value) {
|
private void put(int row, int col, String value) {
|
||||||
|
@ -128,7 +135,7 @@ public class Spreadsheet {
|
||||||
result = "TODO"; // TODO
|
result = "TODO"; // TODO
|
||||||
else if (!formula.isEmpty()) {
|
else if (!formula.isEmpty()) {
|
||||||
try {
|
try {
|
||||||
result = "" + calculate(formula);
|
result = "" + calculate(formula); //calculates the result of a "normal" algebraic expression.
|
||||||
} catch(ArithmeticException ae) {
|
} catch(ArithmeticException ae) {
|
||||||
result = "exc.";
|
result = "exc.";
|
||||||
}
|
}
|
||||||
|
@ -149,35 +156,75 @@ public class Spreadsheet {
|
||||||
return 0;
|
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
|
||||||
* expressions like =B4 or =2+A3-B2, i.e. only with int numbers and other cells and with plus,
|
* 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
|
* 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
|
* 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.
|
* cell name again. It is NOT required to implement dot before dash or parentheses in formulas.
|
||||||
* @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 {
|
private long calculate(String formula) throws ArithmeticException {
|
||||||
Matcher m = Pattern.compile("([A-Z][0-9]*)|[-\\+\\*/]|[0-9]*").matcher(formula);
|
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()) {
|
||||||
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()) {
|
zahlen.add(s);
|
||||||
System.out.println(s);
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return res;
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
// -----
|
// -----
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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() {
|
public String toString() {
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
|
|
||||||
|
@ -188,11 +235,11 @@ public class Spreadsheet {
|
||||||
|
|
||||||
int rc = 1;
|
int rc = 1;
|
||||||
for (Cell[] r : cells) {
|
for (Cell[] r : cells) {
|
||||||
sb.append(System.lineSeparator());
|
sb.append(System.lineSeparator()); //newLine
|
||||||
sb.append(String.format("%2s", rc++) + ": ");
|
sb.append(String.format("%2s", rc++) + ": "); //Number:
|
||||||
|
|
||||||
for (Cell c : r) {
|
for (Cell c : r) {
|
||||||
sb.append(c + " | ");
|
sb.append(c + " | "); //cell value
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue