Inital commit of exercise 1

This commit is contained in:
Simon Giesel 2019-04-30 16:53:08 +02:00
commit 0956126750
9 changed files with 175 additions and 0 deletions

6
.classpath Normal file
View file

@ -0,0 +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-1.8"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="output" path="bin"/>
</classpath>

17
.project Normal file
View file

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<projectDescription>
<name>ALGUE2</name>
<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,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

21
.vscode/launch.json vendored Normal file
View file

@ -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<ALGUE2>",
"request": "launch",
"mainClass": "app.App",
"projectName": "ALGUE2"
}
]
}

BIN
Uebungsblatt2.pdf Normal file

Binary file not shown.

BIN
bin/app/App.class Normal file

Binary file not shown.

BIN
bin/app/MyStack.class Normal file

Binary file not shown.

72
src/app/App.java Normal file
View file

@ -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<String> stack = new MyStack<String>();
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<String> stack = new MyStack<String>();
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<String> opStack = new MyStack<String>();
MyStack<Float> numStack = new MyStack<Float>();
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());
}
}

52
src/app/MyStack.java Normal file
View file

@ -0,0 +1,52 @@
package app;
import java.util.Arrays;
@SuppressWarnings("unchecked")
public class MyStack<E> {
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++];
}
}