commit 095612675036f513192b7cb210f72adbc1babcd6 Author: Simon Date: Tue Apr 30 16:53:08 2019 +0200 Inital commit of exercise 1 diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..51a8bba --- /dev/null +++ b/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/.project b/.project new file mode 100644 index 0000000..6d69175 --- /dev/null +++ b/.project @@ -0,0 +1,17 @@ + + + ALGUE2 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + \ No newline at end of file diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..0c68a61 --- /dev/null +++ b/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,7 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 +org.eclipse.jdt.core.compiler.compliance=1.8 +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.8 diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..6cfdc21 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,21 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "type": "java", + "name": "Debug (Launch) - Current File", + "request": "launch", + "mainClass": "${file}" + }, + { + "type": "java", + "name": "Debug (Launch)-App", + "request": "launch", + "mainClass": "app.App", + "projectName": "ALGUE2" + } + ] +} \ No newline at end of file diff --git a/Uebungsblatt2.pdf b/Uebungsblatt2.pdf new file mode 100644 index 0000000..7b9adfc Binary files /dev/null and b/Uebungsblatt2.pdf differ diff --git a/bin/app/App.class b/bin/app/App.class new file mode 100644 index 0000000..02d8341 Binary files /dev/null and b/bin/app/App.class differ diff --git a/bin/app/MyStack.class b/bin/app/MyStack.class new file mode 100644 index 0000000..75d5145 Binary files /dev/null and b/bin/app/MyStack.class differ diff --git a/src/app/App.java b/src/app/App.java new file mode 100644 index 0000000..49c5f22 --- /dev/null +++ b/src/app/App.java @@ -0,0 +1,72 @@ +package app; + +public class App { + public static void main(String[] args) throws Exception { + /** Aufgabe 1.1 - 1.3 */ + System.out.println("++++ Aufgabe 1.1 - 1.3 ++++"); + MyStack stack = new MyStack(); + stack.push("hello"); + stack.push("world"); + stack.push("123"); + System.out.println(stack.pop()); + System.out.println(stack.pop()); + System.out.println(stack.pop()); + + /** Aufgabe 1.4 */ + System.out.println("++++ Aufgabe 1.4 ++++"); + tokenizeAndCalc("( 2 + 3 )"); + tokenizeAndCalc("( 3 - ( 2 * 3 ) )"); + tokenizeAndCalc("( ( 5 * 4 ) - ( 2 * ( 3 / 4 ) ) )"); + } + + private static void tokenizeAndCalc(String in) { + MyStack stack = new MyStack(); + int i = 0; + while (i < in.length()) { + String a = ""; + // Catch last case if space is missing + if (i + 1 == in.length()) + a = "" + in.charAt(i); + else + while (in.charAt(i) != ' ') { + a = a + in.charAt(i); + i++; + } + stack.push(a); + i++; + } + stack.reverse(); // Reverse stack due to limitations + + MyStack opStack = new MyStack(); + MyStack numStack = new MyStack(); + while (!stack.isEmpty()) { + String curr = stack.pop(); + if (curr.matches("-?\\d+")) + numStack.push(Float.parseFloat(curr)); + else if (curr.matches("\\*|\\+|\\-|\\/")) + opStack.push(curr); + else if (curr.equals(")")) { + String currOp = opStack.pop(); + float num1 = numStack.pop(); + float num2 = numStack.pop(); + switch (currOp) { + case "+": + numStack.push(num2 + num1); + break; + case "-": + numStack.push(num2 - num1); + break; + case "*": + numStack.push(num2 * num1); + break; + case "/": + numStack.push(num2 / num1); + break; + } + } + } + System.out.println(in); + System.out.println("Ergebnis: " + numStack.pop()); + } + +} diff --git a/src/app/MyStack.java b/src/app/MyStack.java new file mode 100644 index 0000000..1b6fbd4 --- /dev/null +++ b/src/app/MyStack.java @@ -0,0 +1,52 @@ +package app; + +import java.util.Arrays; + +@SuppressWarnings("unchecked") +public class MyStack { + private E[] array; + private int pointer = 0; + + public MyStack() { + this.array = (E[]) new Object[1]; + } + + public void push(E el) { + if (array.length == pointer) + resizeArray(1); + array[pointer++] = el; + } + + public E pop() { + pointer--; + E ret = array[pointer]; + if (array.length >= pointer) + resizeArray(0); + return ret; + } + + private void resizeArray(int factor) { + E[] old = this.array.clone(); + this.array = (E[]) new Object[pointer + factor]; + int i = 0; + for (E e : old) + if (pointer + factor > i) + this.array[i++] = e; + } + + public boolean isEmpty() { + return this.array.length == 0; + } + + @Override + public String toString() { + return Arrays.toString(this.array); + } + + public void reverse() { + E[] old = this.array.clone(); + int ii = 0; + for (int i = this.array.length - 1; i > -1; i--) + this.array[i] = old[ii++]; + } +} \ No newline at end of file