neu
parent
5a8baa7343
commit
89049944be
|
@ -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>
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -1,12 +1,46 @@
|
|||
|
||||
public class Calculator {
|
||||
Node<Symbol> erzeugeBeispielBaum() {
|
||||
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.left = new Node<Symbol>(new Symbol("8"));
|
||||
return root;
|
||||
Node<Symbol> erzeugeBeispielBaum() {
|
||||
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.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) {
|
||||
if (n.left == null) {
|
||||
System.out.print(n.content);
|
||||
} else {
|
||||
infixAusgeben(n.left);
|
||||
System.out.print(n.content);
|
||||
if(n.content.istMal() && n.right.content.istPlus()) {
|
||||
System.out.print("(");
|
||||
infixAusgeben(n.right);
|
||||
System.out.print(")");
|
||||
}
|
||||
else {
|
||||
infixAusgeben(n.right);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,7 +5,7 @@ public Symbol(String _s) {
|
|||
this.s = _s;
|
||||
}
|
||||
public boolean istMal() {
|
||||
return s == "x";
|
||||
return s == "*";
|
||||
}
|
||||
public boolean istPlus() {
|
||||
return s == "+";
|
||||
|
@ -19,5 +19,7 @@ public boolean istZhal() {
|
|||
public int getInt() {
|
||||
return Integer.parseInt(s);
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return s;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,7 +4,9 @@ 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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue