Baum
commit
494b70579c
|
@ -0,0 +1,10 @@
|
|||
<?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-11">
|
||||
<attributes>
|
||||
<attribute name="module" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="src" path="src"/>
|
||||
<classpathentry kind="output" path="bin"/>
|
||||
</classpath>
|
|
@ -0,0 +1 @@
|
|||
/bin/
|
|
@ -0,0 +1,17 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>Rechner</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
|
@ -0,0 +1,14 @@
|
|||
eclipse.preferences.version=1
|
||||
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
|
||||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=11
|
||||
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
|
||||
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
|
||||
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
|
||||
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=11
|
|
@ -0,0 +1,71 @@
|
|||
|
||||
public class Calculator {
|
||||
public 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) {
|
||||
|
||||
// wenn Blatt(also letztes)
|
||||
if( n.left == null) {
|
||||
System.out.print( n.content); // wenn keine toString-Methode in Symbo Klasse dann n.content.getInt();
|
||||
}
|
||||
else {
|
||||
// links ausgeben
|
||||
if(n.content.istMal() && n.left.content.istPlus() ) {
|
||||
System.out.print("(");
|
||||
infixAusgeben(n.left);
|
||||
System.out.print(")");
|
||||
}
|
||||
else {
|
||||
infixAusgeben(n.left);
|
||||
}
|
||||
|
||||
// Operator ausgeben
|
||||
// if( n.content.istMal() ) {
|
||||
System.out.print(n.content); // System.out.println("*");
|
||||
// } else {
|
||||
// System.out.println("+"); }
|
||||
|
||||
// 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);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
|
||||
public class Node <T> {
|
||||
|
||||
public T content;
|
||||
public Node<T> left , right;
|
||||
|
||||
public Node(T zahl) {
|
||||
this.content = zahl;
|
||||
left = null;
|
||||
right = null;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,38 @@
|
|||
|
||||
public class Symbol {
|
||||
private String s;
|
||||
|
||||
public Symbol(String _s) {
|
||||
this.s = _s;
|
||||
}
|
||||
|
||||
public boolean istMal() {
|
||||
if(this.s == "*") return true; // oder return s == "*" ;
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean istPlus() {
|
||||
return s == "+";
|
||||
}
|
||||
|
||||
public boolean istOperator() {
|
||||
return istPlus() || istMal() ;
|
||||
}
|
||||
|
||||
public boolean istZahl() {
|
||||
|
||||
if(!istOperator()) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
public int getInt() {
|
||||
|
||||
if( istZahl() == true) return Integer.parseInt(s);
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return s;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
|
||||
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