Noch eine Version für Aufgabe 3
parent
c398b5b5f8
commit
0f85b4dc1a
|
|
@ -0,0 +1,52 @@
|
|||
package Uebung_03_Zehra;
|
||||
|
||||
public class BracketChecker {
|
||||
Stack stack;
|
||||
|
||||
public BracketChecker(Stack stack, String expression) {
|
||||
this.stack = stack;
|
||||
check(expression);
|
||||
}
|
||||
|
||||
/*
|
||||
Check whether the bracket is an opening one. if so: push onto the stack;
|
||||
if not: see whether the last bracket on the stack, which is an opening one fits with the one currently under observation from
|
||||
the expression
|
||||
*/
|
||||
public boolean check(String expression) {
|
||||
for (int i = 0; i < expression.length(); i++) {
|
||||
if (expression.charAt(i) == '(' || expression.charAt(i) == '{' || expression.charAt(i) == '[') {
|
||||
this.stack.push(expression.charAt(i));
|
||||
System.out.println(expression.charAt(i) + "auf den stack gelegt");
|
||||
} else if (expression.charAt(i) == ')' || expression.charAt(i) == '}' || expression.charAt(i) == ']') {
|
||||
char openingBracket = this.stack.pop();
|
||||
|
||||
//create a bracket pair from the opening bracket from the stack, and the closing bracket currently being observed from the expression
|
||||
String brackets = "" + openingBracket + expression.charAt(i);
|
||||
System.out.println("Passen die zusammen? " + brackets);
|
||||
|
||||
//check whether the bracket pair is harmonious
|
||||
if (!(brackets.equals("()") || brackets.equals("{}") || brackets.equals("[]"))) {
|
||||
System.out.println("nein tun sie nicht - Klammern ungültig");
|
||||
return false;
|
||||
} else {
|
||||
System.out.println("ja sieht gut aus");
|
||||
}
|
||||
}
|
||||
}
|
||||
/*if there are still unpopped chars on the stack, they must be opening brackets,
|
||||
that have no corresponding closing brackets in the expression. Hence the expression is not valid.
|
||||
*/
|
||||
System.out.println("Der Stackpointer zeigt aktuell auf den index" + stack.stackPointerTop);
|
||||
if (stack.stackPointerTop > 0) {
|
||||
System.out.println("Es gibt öffnende Klammern, zu denen es keine schließenden gibt. Ausdruck ungültig");
|
||||
return false;
|
||||
}
|
||||
|
||||
/* if we got here that means that no more opening brackets are on the stack to check for a corresponding closing bracket in the expression.
|
||||
hence the expression is valid
|
||||
*/
|
||||
System.out.println("Ausdruck ist gültig");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
package Uebung_03_Zehra;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Stack stack = new Stack();
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
String expression = scanner.nextLine();
|
||||
BracketChecker bracketChecker = new BracketChecker(stack, expression);
|
||||
System.out.println("Bitte geben Sie ihren Ausdruck ein");
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
package Uebung_03_Zehra;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
public class Stack{
|
||||
|
||||
public char[] stack;
|
||||
public int stackPointerTop=0;
|
||||
|
||||
public Stack() {
|
||||
this.stack = new char[100];
|
||||
this.stackPointerTop=0;
|
||||
}
|
||||
|
||||
public void push(char c){
|
||||
stack[stackPointerTop]=c;
|
||||
stackPointerTop++;
|
||||
|
||||
}
|
||||
public char pop(){
|
||||
if(stackPointerTop>=1){
|
||||
return stack[--stackPointerTop];
|
||||
}
|
||||
return '\0'; // '\0' stands for an empty character (null character)
|
||||
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue