diff --git a/Rechner/.classpath b/Rechner/.classpath index 57bca72..d54800d 100644 --- a/Rechner/.classpath +++ b/Rechner/.classpath @@ -1,6 +1,6 @@ - + diff --git a/Rechner/.settings/org.eclipse.jdt.core.prefs b/Rechner/.settings/org.eclipse.jdt.core.prefs index 8c9943d..f2525a8 100644 --- a/Rechner/.settings/org.eclipse.jdt.core.prefs +++ b/Rechner/.settings/org.eclipse.jdt.core.prefs @@ -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 diff --git a/Rechner/src/Calculator.java b/Rechner/src/Calculator.java index 582c5cb..8f2e2af 100644 --- a/Rechner/src/Calculator.java +++ b/Rechner/src/Calculator.java @@ -1,12 +1,46 @@ public class Calculator { -Node erzeugeBeispielBaum() { - Node root = new Node(new Symbol("+")); -root.left = new Node(new Symbol("3")); -root.right = new Node(new Symbol("*")); -root.right.left = new Node(new Symbol("8")); -return root; + Node erzeugeBeispielBaum() { + Node root = new Node(new Symbol("*")); + root.left = new Node(new Symbol("3")); + root.right = new Node(new Symbol("+")); + root.right.left = new Node(new Symbol("5")); + root.right.right = new Node(new Symbol("8")); + return root; + } + + public int berechne(Node 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 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); + + } + } } } diff --git a/Rechner/src/Symbol.java b/Rechner/src/Symbol.java index cd8a8c2..eef0ce3 100644 --- a/Rechner/src/Symbol.java +++ b/Rechner/src/Symbol.java @@ -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; +} } diff --git a/Rechner/src/Test.java b/Rechner/src/Test.java index ceb94cf..beb7ed2 100644 --- a/Rechner/src/Test.java +++ b/Rechner/src/Test.java @@ -4,7 +4,9 @@ public class Test { public static void main(String[] args) { Calculator c = new Calculator(); Node root = c.erzeugeBeispielBaum(); - + int ergebnis = c.berechne(root); + System.out.println( ergebnis); + c.infixAusgeben(root); } }