forked from IF-LK-2020/rechenmaschine
Aufgabe 2 Minus Terme
This commit is contained in:
parent
bdfd32c24a
commit
6a7344379a
|
@ -70,6 +70,9 @@ public class Rechenmaschine {
|
||||||
case '+':
|
case '+':
|
||||||
fehler = "Fehler im Wort " +pEingabe+ ":\nDas Wort darf nicht mit + beginnen!";
|
fehler = "Fehler im Wort " +pEingabe+ ":\nDas Wort darf nicht mit + beginnen!";
|
||||||
return false;
|
return false;
|
||||||
|
case '-':
|
||||||
|
fehler = "Fehler im Wort " +pEingabe+ ":\nDas Wort darf nicht mit - beginnen!";
|
||||||
|
return false;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
fehler = "Fehler im Wort " +pEingabe+ ":\nDer Buchstabe " + buchstabe + " gehört nicht zur Sprache!";
|
fehler = "Fehler im Wort " +pEingabe+ ":\nDer Buchstabe " + buchstabe + " gehört nicht zur Sprache!";
|
||||||
|
@ -81,9 +84,10 @@ public class Rechenmaschine {
|
||||||
case 1:
|
case 1:
|
||||||
switch( buchstabe ) {
|
switch( buchstabe ) {
|
||||||
case '+':
|
case '+':
|
||||||
|
case '-':
|
||||||
tokenlist.append(new Token("OPERAND", currentToken));
|
tokenlist.append(new Token("OPERAND", currentToken));
|
||||||
currentToken = "";
|
currentToken = "";
|
||||||
tokenlist.append(new Token("OPERATOR"));
|
tokenlist.append(new Token("OPERATOR",Character.toString(buchstabe)));
|
||||||
state = 0;
|
state = 0;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
@ -111,12 +115,13 @@ public class Rechenmaschine {
|
||||||
case 2:
|
case 2:
|
||||||
switch( buchstabe ) {
|
switch( buchstabe ) {
|
||||||
case '+':
|
case '+':
|
||||||
tokenlist.append(new Token("OPERATOR"));
|
case '-':
|
||||||
|
tokenlist.append(new Token("OPERATOR",Character.toString(buchstabe)));
|
||||||
state = 0;
|
state = 0;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
fehler = "Fehler im Wort " +pEingabe+ ":\nNach 0 am Anfang muss ein + folgen!";
|
fehler = "Fehler im Wort " +pEingabe+ ":\nNach 0 am Anfang muss ein + oder - folgen!";
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
@ -124,7 +129,7 @@ public class Rechenmaschine {
|
||||||
}
|
}
|
||||||
|
|
||||||
if( state == 0 ) {
|
if( state == 0 ) {
|
||||||
fehler = "Fehler im Wort " +pEingabe+ ":\nDas Wort darf nicht auf + enden!";
|
fehler = "Fehler im Wort " +pEingabe+ ":\nDas Wort darf nicht auf + oder - enden!";
|
||||||
return false;
|
return false;
|
||||||
} else {
|
} else {
|
||||||
tokenlist.append(new Token("OPERAND", currentToken));
|
tokenlist.append(new Token("OPERAND", currentToken));
|
||||||
|
@ -182,13 +187,19 @@ public class Rechenmaschine {
|
||||||
result = 0;
|
result = 0;
|
||||||
|
|
||||||
tokenlist.toFirst();
|
tokenlist.toFirst();
|
||||||
|
String previous="";
|
||||||
while( tokenlist.hasAccess() ) {
|
while( tokenlist.hasAccess() ) {
|
||||||
Token currentToken = tokenlist.getContent();
|
Token currentToken = tokenlist.getContent();
|
||||||
|
if(currentToken.getType().equals("OPERATOR")){
|
||||||
if( currentToken.getType().equals("OPERAND") ) {
|
previous=currentToken.getToken();
|
||||||
result += Integer.parseInt(currentToken.getToken());
|
}
|
||||||
|
if( currentToken.getType().equals("OPERAND") ) {
|
||||||
|
if(previous.equals("+")||previous.equals("")){
|
||||||
|
result += Integer.parseInt(currentToken.getToken());
|
||||||
|
} else {
|
||||||
|
result -= Integer.parseInt(currentToken.getToken());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
tokenlist.next();
|
tokenlist.next();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,16 +5,16 @@ dependency1.type=UsesDependency
|
||||||
dependency2.from=Rechenmaschine
|
dependency2.from=Rechenmaschine
|
||||||
dependency2.to=Token
|
dependency2.to=Token
|
||||||
dependency2.type=UsesDependency
|
dependency2.type=UsesDependency
|
||||||
editor.fx.0.height=738
|
editor.fx.0.height=1056
|
||||||
editor.fx.0.width=816
|
editor.fx.0.width=1936
|
||||||
editor.fx.0.x=466
|
editor.fx.0.x=-8
|
||||||
editor.fx.0.y=146
|
editor.fx.0.y=-8
|
||||||
objectbench.height=66
|
objectbench.height=111
|
||||||
objectbench.width=1256
|
objectbench.width=1256
|
||||||
package.divider.horizontal=0.6
|
package.divider.horizontal=0.6
|
||||||
package.divider.vertical=0.9099876695437731
|
package.divider.vertical=0.847741935483871
|
||||||
package.editor.height=731
|
package.editor.height=650
|
||||||
package.editor.width=1157
|
package.editor.width=1145
|
||||||
package.editor.x=100
|
package.editor.x=100
|
||||||
package.editor.y=25
|
package.editor.y=25
|
||||||
package.frame.height=875
|
package.frame.height=875
|
||||||
|
@ -24,18 +24,18 @@ package.numTargets=3
|
||||||
package.showExtends=true
|
package.showExtends=true
|
||||||
package.showUses=true
|
package.showUses=true
|
||||||
project.charset=UTF-8
|
project.charset=UTF-8
|
||||||
readme.height=58
|
readme.height=60
|
||||||
readme.name=@README
|
readme.name=@README
|
||||||
readme.width=47
|
readme.width=49
|
||||||
readme.x=10
|
readme.x=10
|
||||||
readme.y=10
|
readme.y=10
|
||||||
target1.height=50
|
target1.height=50
|
||||||
target1.name=List
|
target1.name=List
|
||||||
target1.showInterface=false
|
target1.showInterface=false
|
||||||
target1.type=ClassTarget
|
target1.type=ClassTarget
|
||||||
target1.width=130
|
target1.width=150
|
||||||
target1.x=300
|
target1.x=430
|
||||||
target1.y=70
|
target1.y=160
|
||||||
target2.height=50
|
target2.height=50
|
||||||
target2.name=Token
|
target2.name=Token
|
||||||
target2.showInterface=false
|
target2.showInterface=false
|
||||||
|
@ -47,6 +47,6 @@ target3.height=50
|
||||||
target3.name=Rechenmaschine
|
target3.name=Rechenmaschine
|
||||||
target3.showInterface=false
|
target3.showInterface=false
|
||||||
target3.type=ClassTarget
|
target3.type=ClassTarget
|
||||||
target3.width=120
|
target3.width=130
|
||||||
target3.x=170
|
target3.x=170
|
||||||
target3.y=210
|
target3.y=210
|
||||||
|
|
Loading…
Reference in New Issue