1
0
Fork 0

calculate() Methode ergänzt

main
Anastasia Kisner 2024-01-05 19:18:41 +01:00
parent 7472ee85e8
commit a3da7cf436
1 changed files with 43 additions and 8 deletions

View File

@ -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.sql.SQLOutput;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -110,7 +111,8 @@ public class Spreadsheet {
/** /**
* This method does the actual evaluation/calcluation of a specific cell * This method does the actual evaluation/calcluation of a specific cell
* @param cellName the name of the cell to be evaluated * @param row the row of the cell to be evaluated
* @param col the col of the cell to be evaluated
* @return Nothing. * @return Nothing.
*/ */
private void evaluateCell(int row, int col) { private void evaluateCell(int row, int col) {
@ -165,17 +167,50 @@ public class Spreadsheet {
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 res = 0; long res = 0;
char operator = '+';
// TODO implement while (m.find()) {
// 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(); String s = m.group();
if (!s.isEmpty()) { long value;
System.out.println(s);
} if(s.isEmpty()){
continue;
} }
System.out.println("Beginning of loop " + s);
if (!(s.matches("[-\\+\\*/]"))) {
if(s.matches("[A-Z][0-9]*")) {
int col = this.getCol(s);
int row = this.getRow(s);
String cellValue = cells[row][col].getValue();
value = Long.parseLong(cells[row][col].getValue());
}else{
System.out.println("in number in try");
value = Long.parseLong(s);
}
switch(operator) {
case '+':
res += value;
break;
case '-':
res -= value;
break;
case '*':
res *= value;
break;
case '/':
if (res != 0) {
res /= value;
break;
}
}
}else{
operator = s.charAt(0);
}
}
return res; return res;
} }