master
haeslelu 2022-03-21 12:45:38 +01:00
commit 494b70579c
8 changed files with 177 additions and 0 deletions

10
Rechner/.classpath Normal file
View File

@ -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>

1
Rechner/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/bin/

17
Rechner/.project Normal file
View File

@ -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>

View File

@ -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

View File

@ -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);
}
}
}
}

12
Rechner/src/Node.java Normal file
View File

@ -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;
}
}

38
Rechner/src/Symbol.java Normal file
View File

@ -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;
}
}

14
Rechner/src/Test.java Normal file
View File

@ -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);
}
}