1
0
Fork 0

//implemented calculate method

main
Emelie Schneider 2024-01-05 16:44:25 +01:00
parent 23d6c57683
commit 75dec01910
1 changed files with 59 additions and 3 deletions

View File

@ -373,16 +373,72 @@ 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;
String cellName="";
String operand="";
long value=0;
int row;
int col;
int lock=0;
int cnt=0;
// TODO implement // TODO implement
// uncomment the following to see an example how the elements of a formula can be accessed // 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);
if(Character.isLetter(s.charAt(0))) {
cellName= s;
row=getRow(cellName);
col=getCol(cellName);
value= Integer.parseInt(cells[row][col].getValue());
lock=0;
}else if(Character.isDigit(s.charAt(0))) {
value= Integer.parseInt(s);
lock=0;
}else {
operand = s;
System.out.println(operand);
lock=1;
}
if(cnt==0)
res=value;
if(lock==0) {
switch(operand) {
case "+": res+= value;
break;
case "-": res-= value;
break;
case "*": res*= value;
break;
case "/": if(value==0) {
System.out.println("division by 0 is not possible");
}else res/= value;
break;
}
}
}
cnt++;
} }
}
return res; return res;
} }