forked from hummel/PR1-Spreadsheet
Fehlende Dateien hochladen
parent
e5190d7c9b
commit
beaf0b5b9e
|
@ -1,10 +1,10 @@
|
|||
,,,,,,,,,
|
||||
13,=69+420,,,,,,,,
|
||||
1,,,,,,,,,
|
||||
123,,,,,,,,,
|
||||
,,,,,,,,,
|
||||
,,,,,,,,,=7*6
|
||||
,,,,,,,,,=3/2
|
||||
,,,,,,,,,
|
||||
,,,,,,,,,
|
||||
,=41+A2,,,,,,,,
|
||||
,,,,,,,,,
|
||||
,,,,,,,,,
|
||||
,,,,,,,,,
|
||||
,,,,,,,,,
|
||||
|
|
|
|
@ -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,10 +40,16 @@ public class Spreadsheet {
|
|||
}
|
||||
|
||||
public String get(String 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) {
|
||||
if (!value.startsWith("="))
|
||||
cells[row][col].setValue(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.";
|
||||
}
|
||||
|
@ -159,25 +166,65 @@ public class Spreadsheet {
|
|||
* @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()
|
||||
while(m.find()) {
|
||||
String s = m.group();
|
||||
if (!s.isEmpty()) {
|
||||
System.out.println(s);
|
||||
zahlen.add(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() {
|
||||
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
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue