forked from IF-LK-2020/rechenmaschine
Punkt vor Strich Regel implementiert
This commit is contained in:
parent
cf554d7852
commit
fe5920fc36
|
@ -330,7 +330,7 @@ public class List<ContentType> {
|
|||
* pNode == null ist, pNode nicht in der Liste ist oder pNode der erste Knoten
|
||||
* der Liste ist
|
||||
*/
|
||||
private ListNode getPrevious(ListNode pNode) {
|
||||
public ListNode getPrevious(ListNode pNode) {
|
||||
if (pNode != null && pNode != first && !this.isEmpty()) {
|
||||
ListNode temp = first;
|
||||
while (temp != null && temp.getNextNode() != pNode) {
|
||||
|
@ -341,5 +341,4 @@ public class List<ContentType> {
|
|||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -19,7 +19,7 @@ public class Rechenmaschine {
|
|||
tokenlist = new List<>();
|
||||
|
||||
Scanner input = new Scanner(System.in);
|
||||
System.out.println("Gib einen Plusterm ein und bestätige mit ENTER: ");
|
||||
System.out.println("Gib einen Term ein und bestätige mit ENTER: ");
|
||||
String eingabe = input.nextLine();
|
||||
input.close();
|
||||
|
||||
|
@ -248,9 +248,40 @@ public class Rechenmaschine {
|
|||
return true;
|
||||
}
|
||||
|
||||
public void punktVorStrich(){
|
||||
tokenlist.toFirst();
|
||||
while(tokenlist.hasAccess()){
|
||||
if(tokenlist.getContent().getToken().equals("*")){
|
||||
tokenlist.remove();
|
||||
Double a = Double.parseDouble(tokenlist.getContent().getToken());
|
||||
|
||||
tokenlist.current=tokenlist.getPrevious(tokenlist.current);
|
||||
Double b = Double.parseDouble(tokenlist.getContent().getToken());
|
||||
|
||||
tokenlist.remove();
|
||||
|
||||
tokenlist.getContent().setToken(String.valueOf(a*b));
|
||||
} else if(tokenlist.getContent().getToken().equals("/")){
|
||||
tokenlist.remove();
|
||||
|
||||
Double b = Double.parseDouble(tokenlist.getContent().getToken());
|
||||
|
||||
//Previous muss gelöscht werden, tokenlist.getContent != null ist,
|
||||
// wenn der 2. Token am ende des Termes ist.
|
||||
tokenlist.current=tokenlist.getPrevious(tokenlist.current);
|
||||
Double a = Double.parseDouble(tokenlist.getContent().getToken());
|
||||
|
||||
tokenlist.remove();
|
||||
|
||||
tokenlist.getContent().setToken(String.valueOf(a/b));
|
||||
}
|
||||
tokenlist.next();
|
||||
}
|
||||
}
|
||||
|
||||
public void run() {
|
||||
result = 0;
|
||||
|
||||
punktVorStrich();
|
||||
tokenlist.toFirst();
|
||||
String previous="";
|
||||
while( tokenlist.hasAccess() ) {
|
||||
|
@ -262,8 +293,6 @@ public class Rechenmaschine {
|
|||
switch (previous) {
|
||||
case "+", "" -> result += Double.parseDouble(currentToken.getToken());
|
||||
case "-" -> result -= Double.parseDouble(currentToken.getToken());
|
||||
case "*" -> result *= Double.parseDouble(currentToken.getToken());
|
||||
default -> result /= Double.parseDouble(currentToken.getToken());
|
||||
}
|
||||
}
|
||||
tokenlist.next();
|
||||
|
|
Loading…
Reference in New Issue