Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.57 KB | None | 0 0
  1. class Program {     private static StringBuilder wordPath = new StringBuilder();      static void Main(string[] args)     {         Console.WriteLine("* Automat: A = <{z0, z1, z2}, {0, 1}, b, {z1, z2}, z0>");         Console.WriteLine("** Make sure your word is recognized by the Automat\n");         Console.Write("# Enter a word: ");         string word = Console.ReadLine();          wordPath.Append("z0");         bool isValidWord = Automat(word, 0, 0);          if (isValidWord)         {             Console.WriteLine("True (your word is recognized)");             Console.WriteLine("\n# Path for your word");             Console.WriteLine(wordPath.ToString());         }         else         {             Console.WriteLine("False (your word is not recognized)");         }     }      public static bool Automat(string word, int index, int state)     {         if (!Check(word)) return false;          if (index == word.Length)         {             if (state == 1 || state == 2)             {                 return true;             }             else             {                 return false;             }         }         else         {             char ch = word[index];              switch (state)             {                 case 0:                     if (ch == '0')                     {                         state = 1;                         wordPath.Append(" -> z1");                     }                     else                     {                         state = 0;                         wordPath.Append(" -> z0");                     } break;                  case 1:                     if (ch == '0')                     {                         return false;                     }                     else                     {                         state = 2;                         wordPath.Append(" -> z2");                     }                     break;                  case 2:                     if (ch == '0')                     {                         state = 1;                         wordPath.Append(" -> z1");                     }                     else                     {                         state = 0;                         wordPath.Append(" -> z0");                     }                     break;             }         }          return Automat(word, index + 1, state);     }      public static bool Check(string word)     {         int n = word.Length;          for (int i = 0; i < n; i++)         {             if (word[i] != '0' && word[i] != '1')             {                 return false;             }         }          return true;     } }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement