solved s2 ex2

This commit is contained in:
navid.sassan 2020-10-05 00:12:58 +02:00
parent 514e94147b
commit 4f31b86af4
6 changed files with 435 additions and 124 deletions

View File

@ -0,0 +1,52 @@
package ch.zhaw.ads;
import java.util.Map;
import static java.util.Map.entry;
public class BracketServer implements CommandExecutor {
@Override
public String execute(String command) {
StringBuffer result = new StringBuffer(100);
result.append("Die Eingabe war \"");
result.append(command);
result.append("\"\n");
return result.toString();
}
private Map<Character, Character> openingBrackets = Map.ofEntries(
entry('(', ')'),
entry('[', ']'),
entry('{', '}'),
entry('<', '>')
);
private Map<Character, Character> closingBrackets = Map.ofEntries(
entry(')', '('),
entry(']', '['),
entry('}', '{'),
entry('>', '<')
);
public boolean checkBrackets(String input) {
ListStack<Character> bracketStack = new ListStack<>();
for (int i = 0; i < input.length(); i++) {
char cur_char = input.charAt(i);
if (openingBrackets.containsKey(cur_char)) {
bracketStack.push(cur_char);
}
if (closingBrackets.containsKey(cur_char)) {
if (bracketStack.isEmpty()) {
return false;
}
Character last = (Character) bracketStack.pop();
if (last != closingBrackets.get(cur_char)) {
return false;
}
}
}
return bracketStack.isEmpty();
}
// search bracket from start, then remove matching bracket from the end of the string. then remove opening bracket.
}

View File

@ -0,0 +1,72 @@
/**
* AnyServer -- Praktikum Experimentierkasten -- ADS
*
* @author E. Mumprecht
* @version 1.0 -- Geruest fuer irgendeinen Server
*/
package ch.zhaw.ads;
public class ListStack<T> implements CommandExecutor {
@Override
public String execute(String command) {
StringBuffer result = new StringBuffer(100);
result.append("Die Eingabe war \"");
result.append(command);
result.append("\"\n");
return result.toString();
}
Object[] data;
private int top;
private int capacity;
private static final int DEFAULT_CAPACITY = 100;
public ListStack() {
this(DEFAULT_CAPACITY);
}
public ListStack(int capacity) {
this.capacity= capacity;
removeAll();
}
public void removeAll() {
data = new Object[capacity];
top = 0;
}
public void push(Object x) {
// public void push(Object x) throws StackOverflowException {
// if (isFull()){
// throw new StackOverflowException();
// }
data[top] = x;
top++;
}
public Object pop() {
if (isEmpty()){
return null;
}
top--;
Object topItem= data [top];
data [top] = null;
return topItem;
}
public Object peek() {
if (isEmpty()){
return null;
}
return data [top-1];
}
public boolean isEmpty() {
return (top == 0);
}
public boolean isFull() {
return top == data.length;
}
}

View File

@ -0,0 +1,117 @@
package ch.zhaw.ads;
import java.util.AbstractList;
public class MyList<T extends Object> extends AbstractList<T> {
private Object[] data;
private Node<T> head;
private Node<T> tail;
private int size;
public MyList() {
clear();
}
public boolean add(Object obj) {
try {
if (isEmpty()) {
head = tail = new Node<T>((T) obj);
size++;
return true;
}
Node<T> newNode = new Node<T>((T) obj, tail, null);
// the old tail now has a nextNode
tail.setNextNode(newNode);
// and move tail to newNode
tail = newNode;
size++;
return true;
} catch (ClassCastException e) {
return false;
}
}
public boolean remove(Object obj) {
if (isEmpty()) {
return false;
}
try {
Node<T> nodeToRemove = new Node<T>((T) obj);
for (Node<T> node = head; node.hasNextNode(); node = node.getNextNode()) {
if (node.equals(nodeToRemove)) {
unlink(node);
return true;
}
}
} catch (ClassCastException e) {
return false;
}
// reaching this means that no element was found
return false;
}
private void unlink(Node<T> node) {
Node<T> previousNode = node.getNextNode();
Node<T> nextNode = node.getNextNode();
if (previousNode == null) {
// this means the node which will be unlinked was the first element. the new first element is the nextNode of the unlinked node
head = nextNode;
} else {
previousNode.setNextNode(nextNode);
}
// same thing, other way around
if (nextNode == null) {
tail = previousNode;
} else {
nextNode.setPreviousNode(previousNode);
}
size--;
}
public T get(int index) {
if (index > 0 || index > size()) {
throw new IndexOutOfBoundsException();
}
// check if the index is closer to head or tail
// shifting the size to the right by one bit basically divides it by 2 (works because its binary)
if (index < (size >> 1)) {
Node<T> node = head;
for (int i = 0; i < index; i++) {
node = node.getNextNode();
}
return node.getValue();
} else {
Node<T> node = tail;
for (int i = size - 1; i > index; i--) {
node = node.getPreviousNode();
}
return node.getValue();
}
}
public boolean isEmpty() {
return size == 0;
}
public int size() {
return size;
}
public void clear() {
size = 0;
head = null;
tail = null;
}
void other() {
throw new UnsupportedOperationException();
}
}

View File

@ -0,0 +1,61 @@
package ch.zhaw.ads;
public class Node<E> {
private E value;
private Node<E> previousNode;
private Node<E> nextNode;
public Node(E object) {
this(object, null, null);
}
public Node(E object, Node<E> previousNode, Node<E> nextNode) {
this.value = object;
this.previousNode = previousNode;
this.nextNode = nextNode;
}
public boolean hasNextNode() {
return this.nextNode != null;
}
public boolean hasPreviousNode() {
return this.previousNode != null;
}
public boolean equals(Node<E> node) {
if (node == null) {
return false;
}
return value.equals(node.value);
}
public Node<E> clone(){
return new Node<E>(value, previousNode, nextNode);
}
public E getValue() {
return value;
}
public void setValue(E value) {
this.value = value;
}
public Node<E> getPreviousNode() {
return previousNode;
}
public void setPreviousNode(Node<E> previousNode ) {
this.previousNode = previousNode ;
}
public Node<E> getNextNode() {
return nextNode;
}
public void setNextNode(Node<E> nextNode ) {
this.nextNode = nextNode ;
}
}

View File

@ -1,8 +1,9 @@
package Praktikum_02_Code;
package ch.zhaw.ads;
import org.junit.Test;
import org.junit.Before;
import static org.junit.Assert.*;
public class BracketServerTest {
BracketServer bs;
@ -26,6 +27,14 @@ public class BracketServerTest {
test("[])",false);
test("[(3 +3)* 35 +3]* {3 +2}",true) ;
test("[({3 +3)* 35} +3]* {3 +2}",false);
// new tests
test("(", false);
test(")", false);
test("<(<>)>", true);
test("<(<)>>", false);
test("<*(<*<>*>)*>", true);
test("<(<**>)*>", false);
}
}