Advertisement
Soulseller

Kellerautomat

May 30th, 2016
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.99 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class PDA
  4. {
  5.  
  6.     private Stack<Integer> keller = new Stack<Integer>();
  7.     private ArrayList<Character> alphabet = new ArrayList<Character>();
  8.     private int aktZustand;
  9.     private int endzustand;
  10.     String wort;
  11.  
  12.     public PDA(String wort,ArrayList<Character> alphabet,int startzustand,int endzustand)
  13.     {
  14.         // Stack und Konstruktor initialisieren
  15.         keller.push(0);
  16.         this.alphabet = alphabet;
  17.         this.endzustand = endzustand;
  18.         this.wort = wort;
  19.         this.aktZustand = startzustand;
  20.     }
  21.  
  22.  
  23.     public boolean pruefeWort()
  24.     {
  25.         char aktZeichen;
  26.  
  27.         for (int i = 0; i < wort.length(); i++)
  28.         {
  29.             //Abfrage ob Wort länger 0 ist
  30.             if (wort.length() == 0)
  31.             {
  32.                 System.out.println( "Bitte ein Wort eingeben");
  33.                 return false;
  34.             }
  35.            
  36.             //abfrage ob alle Zeichen des Wortes überhaupt Teil des Alphabets sind.
  37.             aktZeichen = wort.charAt(i);
  38.  
  39.             if (!(alphabet.contains(aktZeichen)))
  40.             {
  41.                 System.out.println("Ungültiges Zeichen");
  42.                 return false;
  43.             }
  44.            
  45.            
  46.             for (int j = 0; j< alphabet.size();j++)
  47.             {
  48.                 if (aktZeichen == alphabet.get(j))
  49.                 {
  50.                     if (!transZustand(j+1))        
  51.                         return false;
  52.                 }
  53.             }
  54.            
  55.         }
  56.        
  57.         //Start der Regelüberprüfung
  58.         transZustand(0);
  59.        
  60.         if (endzustand == aktZustand)
  61.         {          
  62.             System.out.println(wort + " ist in der Sprache enthalten");
  63.             return true;
  64.         }
  65.        
  66.         else
  67.         {          
  68.             System.out.println(wort + " ist in der Sprache nicht enthalten");
  69.             return false;
  70.         }
  71.     }
  72.  
  73.    
  74.    
  75.     private boolean transZustand(int zeichen)
  76.     {
  77.         int kZeichen = keller.peek();
  78.  
  79.         if (aktZustand == 0 && zeichen == 0 && kZeichen == 0)
  80.         {
  81.             aktZustand = 2;
  82.             keller.pop();
  83.             System.out.println("Regel1");
  84.             return false;
  85.         }
  86.  
  87.         if (aktZustand == 0 && zeichen == 1 && kZeichen == 0)
  88.         {
  89.             aktZustand = 0;
  90.             keller.push(1);
  91.             System.out.println("Regel2");
  92.             return true;
  93.         }
  94.  
  95.         if (aktZustand == 0 && zeichen == 1 && kZeichen == 1)
  96.         {
  97.             aktZustand = 0;
  98.             keller.push(1);
  99.             System.out.println("Regel3");
  100.             return true;
  101.         }
  102.  
  103.         if (aktZustand == 0 && zeichen == 2 && kZeichen == 1)
  104.         {
  105.             aktZustand = 1;
  106.             keller.pop();
  107.             System.out.println("Regel4");
  108.             return true;
  109.         }
  110.  
  111.         if (aktZustand == 1 && zeichen == 2 && kZeichen == 1)
  112.         {
  113.             aktZustand = 1;
  114.             keller.pop();
  115.             System.out.println("Regel5");
  116.             return true;
  117.         }
  118.  
  119.         if (aktZustand == 1 && zeichen == 0 && kZeichen == 0)
  120.         {
  121.             aktZustand = 2;
  122.             keller.pop();
  123.             System.out.println("Regel6");
  124.             return true;
  125.         }
  126.        
  127.        
  128.         System.out.println(wort + " ist in der Sprache nicht enthalten");
  129.         return false;
  130.     }
  131. }
  132.  
  133.  
  134. //Main Class
  135.  
  136. import java.util.ArrayList;
  137.  
  138. public class Start
  139. {
  140.     public static void main(String[] args)
  141.     {  
  142.         String wort = "aabb";
  143.         ArrayList<Character> alphabet = new ArrayList<Character>();
  144.         int endzustand = 2;
  145.         int startzustand = 0;
  146.        
  147.         alphabet.add('a');
  148.         alphabet.add('b');
  149.  
  150.    
  151.          PDA pda = new PDA(wort,alphabet,startzustand,endzustand);
  152.          pda.pruefeWort();
  153.     }
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement