Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class PDA
- {
- private Stack<Integer> keller = new Stack<Integer>();
- private ArrayList<Character> alphabet = new ArrayList<Character>();
- private int aktZustand;
- private int endzustand;
- String wort;
- public PDA(String wort,ArrayList<Character> alphabet,int startzustand,int endzustand)
- {
- // Stack und Konstruktor initialisieren
- keller.push(0);
- this.alphabet = alphabet;
- this.endzustand = endzustand;
- this.wort = wort;
- this.aktZustand = startzustand;
- }
- public boolean pruefeWort()
- {
- char aktZeichen;
- for (int i = 0; i < wort.length(); i++)
- {
- //Abfrage ob Wort länger 0 ist
- if (wort.length() == 0)
- {
- System.out.println( "Bitte ein Wort eingeben");
- return false;
- }
- //abfrage ob alle Zeichen des Wortes überhaupt Teil des Alphabets sind.
- aktZeichen = wort.charAt(i);
- if (!(alphabet.contains(aktZeichen)))
- {
- System.out.println("Ungültiges Zeichen");
- return false;
- }
- for (int j = 0; j< alphabet.size();j++)
- {
- if (aktZeichen == alphabet.get(j))
- {
- if (!transZustand(j+1))
- return false;
- }
- }
- }
- //Start der Regelüberprüfung
- transZustand(0);
- if (endzustand == aktZustand)
- {
- System.out.println(wort + " ist in der Sprache enthalten");
- return true;
- }
- else
- {
- System.out.println(wort + " ist in der Sprache nicht enthalten");
- return false;
- }
- }
- private boolean transZustand(int zeichen)
- {
- int kZeichen = keller.peek();
- if (aktZustand == 0 && zeichen == 0 && kZeichen == 0)
- {
- aktZustand = 2;
- keller.pop();
- System.out.println("Regel1");
- return false;
- }
- if (aktZustand == 0 && zeichen == 1 && kZeichen == 0)
- {
- aktZustand = 0;
- keller.push(1);
- System.out.println("Regel2");
- return true;
- }
- if (aktZustand == 0 && zeichen == 1 && kZeichen == 1)
- {
- aktZustand = 0;
- keller.push(1);
- System.out.println("Regel3");
- return true;
- }
- if (aktZustand == 0 && zeichen == 2 && kZeichen == 1)
- {
- aktZustand = 1;
- keller.pop();
- System.out.println("Regel4");
- return true;
- }
- if (aktZustand == 1 && zeichen == 2 && kZeichen == 1)
- {
- aktZustand = 1;
- keller.pop();
- System.out.println("Regel5");
- return true;
- }
- if (aktZustand == 1 && zeichen == 0 && kZeichen == 0)
- {
- aktZustand = 2;
- keller.pop();
- System.out.println("Regel6");
- return true;
- }
- System.out.println(wort + " ist in der Sprache nicht enthalten");
- return false;
- }
- }
- //Main Class
- import java.util.ArrayList;
- public class Start
- {
- public static void main(String[] args)
- {
- String wort = "aabb";
- ArrayList<Character> alphabet = new ArrayList<Character>();
- int endzustand = 2;
- int startzustand = 0;
- alphabet.add('a');
- alphabet.add('b');
- PDA pda = new PDA(wort,alphabet,startzustand,endzustand);
- pda.pruefeWort();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement