update 21.3.2022

master
mittelni 2022-03-21 12:49:54 +01:00
parent 8a11dbf1b8
commit de725401ff
5 changed files with 59 additions and 11 deletions

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-17">
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-11">
<attributes>
<attribute name="module" value="true"/>
</attributes>

View File

@ -1,8 +1,8 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=17
org.eclipse.jdt.core.compiler.codegen.targetPlatform=11
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=17
org.eclipse.jdt.core.compiler.compliance=11
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
@ -11,4 +11,4 @@ org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
org.eclipse.jdt.core.compiler.release=enabled
org.eclipse.jdt.core.compiler.source=17
org.eclipse.jdt.core.compiler.source=11

View File

@ -3,17 +3,58 @@ public class Calculator {
Node<Symbol> erzeugeBeispielBaum() {
Node<Symbol> root = new Node<Symbol>(new Symbol("+"));
Node<Symbol> root = new Node<Symbol>(new Symbol("*"));
root.left = new Node<Symbol>(new Symbol("3"));
root.right = new Node<Symbol>(new Symbol("*"));
root.right = new Node<Symbol>(new Symbol("+"));
root.right.left = new Node<Symbol>(new Symbol("5"));
root.right.right = new Node<Symbol>(new Symbol("8"));
return root;
}
public int berechne(Node<Symbol> n) {
if(n.left == null) {
return n.content.getInt();
} else {
int links = berechne(n.left);
int rechts = berechne(n.right);
if(n.content.istMal()) {
return links * rechts;
} else {
return links + rechts;
}
}
}
public void infixAusgeben(Node<Symbol> n) {
//wenn Blatt
if(n.left == null) {
System.out.print(n.content);
} else {
//links ausgeben
//wenn aktuell ein * und links ein +, dann mit klammern
if(n.content.istMal() && n.left.content.istPlus()) {
System.out.print("(");
infixAusgeben(n.left);
System.out.print(")");
} else {
infixAusgeben(n.left);
}
//Operator ausgeben
System.out.print(n.content);
//rechts ausgeben
//wenn aktuell ein * und rechts ein +; dann mit klammern
if(n.content.istMal() && n.right.content.istPlus()) {
System.out.print("(");
infixAusgeben(n.right);
System.out.print(")");
} else {
infixAusgeben(n.right);
}
}
}
}

View File

@ -27,4 +27,8 @@ public class Symbol {
public int getInt() {
return Integer.parseInt(s);
}
public String toString() {
return s;
}
}

View File

@ -4,8 +4,11 @@ public class Test {
public static void main(String[] args) {
Calculator c = new Calculator();
Node<Symbol> root = c.erzeugeBeispielBaum();
int ergebnis = c.berechne(root);
System.out.println(ergebnis);
c.infixAusgeben(root);
}
}