Tomki

PushDownAutomata

Mar 22nd, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.19 KB | None | 0 0
  1. // MPAuto.cpp: определяет точку входа для консольного приложения.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <vector>
  6. #include <string>
  7. #include <iostream>
  8. #include <deque>
  9.  
  10. using namespace std;
  11.  
  12. class pushDownAuto
  13. {
  14. private:
  15.     vector<char> memory;
  16.     int activeMatrix;
  17. public:
  18.     pushDownAuto()
  19.     {
  20.         memory.empty();
  21.         activeMatrix = 1;
  22.     }
  23.     void pushA()
  24.     {
  25.         memory.push_back('A');
  26.     }
  27.     void pop()
  28.     {
  29.         memory.pop_back();
  30.     }
  31.     void pushB()
  32.     {
  33.         memory.push_back('B');
  34.     }
  35.     void pushC()
  36.     {
  37.         memory.push_back('C');
  38.     }
  39.     int parse(deque<char>& txt)
  40.     {
  41.         while (txt.size() != 0)
  42.         {
  43.            
  44.             if (activeMatrix == 1)
  45.             {
  46.                 if (memory.empty())
  47.                 {
  48.                     switch (txt[0])
  49.                     {
  50.                     default:
  51.                         break;
  52.                     case '0':
  53.                         return 0;
  54.                     case '1':
  55.                     {
  56.                         pushA(), txt.pop_front();
  57.                         break;
  58.                     }
  59.                     case '\0':
  60.                         return 0;
  61.                     }
  62.                 }
  63.                 else if (memory.back() == 'A')
  64.                 {
  65.                     switch (txt[0])
  66.                     {
  67.                     default:
  68.                         break;
  69.                     case '0':
  70.                         return 0;
  71.                     case '1':
  72.                     {
  73.                         pop(), pushB(), txt.pop_front();
  74.                         break;
  75.                     }
  76.                     case '\0':
  77.                         return 0;
  78.                     }
  79.                 }
  80.                 else if (memory.back() == 'B')
  81.                 {
  82.                     switch (txt[0])
  83.                     {
  84.                     default:
  85.                         break;
  86.                     case '0':
  87.                         activeMatrix = 2; break;
  88.                     case '1':
  89.                         pushA(), txt.pop_front(); break;
  90.                     case '\0':
  91.                         return 0;
  92.                     }
  93.                 }
  94.             }
  95.             else if (activeMatrix == 2)
  96.             {
  97.                 if (memory.back() == 'B')
  98.                 {
  99.                     switch (txt[0])
  100.                     {
  101.                     default:
  102.                         break;
  103.                     case '0':
  104.                         pushC(), txt.pop_front(); break;
  105.                     case '1':
  106.                         return 0;
  107.                     case '\0':
  108.                         return 0;
  109.                     }
  110.                 }
  111.                 else if (memory.back() == 'C')
  112.                 {
  113.                     switch (txt[0])
  114.                     {
  115.                     default:
  116.                         break;
  117.                     case '0':
  118.                         pushC(), txt.pop_front(); break;
  119.                     case '1':
  120.                         activeMatrix = 3; break;
  121.                     case '\0':
  122.                         return 0;
  123.                     }
  124.                 }
  125.             }
  126.             else if (activeMatrix == 3)
  127.             {
  128.                 if (memory.back() == 'C')
  129.                 {
  130.                     switch (txt[0])
  131.                     {
  132.                     default:
  133.                         break;
  134.                     case '0':
  135.                         return 0;
  136.                     case '1':
  137.                         pop(), txt.pop_front(); break;
  138.                     case '\0':
  139.                         return 0;
  140.                     }
  141.                 }
  142.                 else if (memory.back() == 'B')
  143.                 {
  144.                     switch (txt[0])
  145.                     {
  146.                     default:
  147.                         break;
  148.                     case '0':
  149.                         activeMatrix = 4; break;
  150.                     case '1':
  151.                         return 0;
  152.                     case '\0':
  153.                         return 0;
  154.                     }
  155.                 }
  156.             }
  157.             else if (activeMatrix == 4)
  158.             {
  159.                 if (memory.empty())
  160.                 {
  161.                     if (txt[0] == '\0')
  162.                         return 1;
  163.                     else
  164.                         return 0;
  165.                 }
  166.                 else
  167.                 {
  168.                     switch (txt[0])
  169.                     {
  170.                     default:
  171.                         break;
  172.                     case '0':
  173.                         pop(), txt.pop_front(); break;
  174.                     case '1':
  175.                         return 0;
  176.                     case '\0':
  177.                         return 0;
  178.                     }
  179.                 }
  180.             }
  181.         }
  182.     }
  183.     void getInfo()
  184.     {
  185.         cout << memory.size();
  186.     }
  187. };
  188.  
  189. int main()
  190. {
  191.     deque<char> input;
  192.     string txt;
  193.     cin >> txt;
  194.     int i = 0;
  195.     while (txt[i] != '\0')
  196.     {
  197.         input.push_back(txt[i]);
  198.         i++;
  199.     }
  200.     input.push_back(txt[i]);
  201.  
  202.     pushDownAuto *obj = new pushDownAuto();
  203.     if (obj->parse(input))
  204.         cout << "correct\n";
  205.     else cout << "incorrect\n";
  206.     return 0;
  207. }
Advertisement
Add Comment
Please, Sign In to add comment