1
0
Fork 0

Methode "calculate" geht jetzt

main
Florian Hörner 2024-01-05 22:26:43 +01:00
parent c4f0a994c9
commit 3f4f1d978a
1 changed files with 61 additions and 2 deletions

View File

@ -198,7 +198,7 @@ public class Spreadsheet {
result = "TODO"; // TODO
else if (!formula.isEmpty()) {
try {
result = "" + calculate(formula);
result = "" + calculate(formula); //geht nicht?
} catch(ArithmeticException ae) {
result = "exc.";
}
@ -234,16 +234,75 @@ public class Spreadsheet {
long res = 0;
// TODO implement
int merker = 0;
int zaehler = 0;
int neue_zahl = 0;
// 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);
zaehler++;
switch (zaehler){
case 1:
try {
res = Integer.parseInt(s);
}catch (Exception ignored){
res = Integer.parseInt(get(s));
}
break;
case 2:
switch(s) {
case "+":
merker = 1;
break;
case "-":
merker = 2;
break;
case "*":
merker = 3;
break;
case "/":
merker = 4;
break;
default:
System.out.println("ERROR");
break;
}
break;
case 3:
try {
neue_zahl = Integer.parseInt(s);
}catch (Exception ignored){
neue_zahl = Integer.parseInt(get(s));
}
res = rechne(res, merker, neue_zahl);
zaehler = 1;
break;
}
}
}
return res;
}
return res;
private long rechne(long a, int merker, int b) {
switch(merker){
case 1:
return a + b;
case 2:
return a - b;
case 3:
return a * b;
case 4:
return a / b;
default:
System.out.println("ERROR");
break;
}
return 0;
}
// -----