Advertisement
Guest User

Untitled

a guest
Jun 14th, 2015
608
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.03 KB | None | 0 0
  1. #include <fstream>
  2. #include <vector>
  3. #include <string>
  4. #include <iostream>
  5. using namespace std;
  6.  
  7. enum Direction{L, R, S, Q,};
  8.  
  9. struct State
  10. {
  11.     string name;
  12.     char input_symbol;
  13.     string next_state;
  14.     char output_symbol;
  15.     Direction dir;
  16.  
  17.     void Print()
  18.     {
  19.         cout<<name<<" "<<input_symbol<<" "<<next_state<<" "<<output_symbol<<" ";
  20.         switch(dir)
  21.         {
  22.             case L: cout<<"L"<<endl; break;
  23.             case R: cout<<"R"<<endl; break;
  24.             case S: cout<<"S"<<endl; break;
  25.             case Q: cout<<"Q"<<endl; break;
  26.         }
  27.     }
  28. };
  29.  
  30. class TuringMachine
  31. {
  32. public:
  33.     void AddState(State &state)
  34.     {
  35.         states.push_back(state);
  36.         state.Print();
  37.     }
  38.  
  39.     void Run()
  40.     {
  41.         cout<<"Input:";
  42.         cin>>input;
  43.  
  44.         string tmp = input;
  45.  
  46.         bool end = false;
  47.         unsigned current_index = 0;
  48.         State st = states[0];
  49.  
  50.         string name_needed = st.name;
  51.  
  52.         cout<<endl;
  53.  
  54.         while(!end)
  55.         {
  56.  
  57.             unsigned i = 0;
  58.             bool fail = true;
  59.             while(i < states.size())
  60.             {
  61.                 if(states[i].name == name_needed)
  62.                 {
  63.                     if(states[i].input_symbol == tmp[current_index])
  64.                     {
  65.                         st = states[i];
  66.                         fail = false;
  67.                         break;
  68.                     }
  69.                 }
  70.                 ++i;
  71.             }
  72.             if(fail){cout<<"Error!"<<endl;
  73.             st.Print();
  74.             cout<<"Current symbol: "<<tmp[current_index]<<endl;break;}
  75.            
  76.             tmp[current_index] = st.output_symbol;
  77.             name_needed = st.next_state;
  78.             switch(st.dir)
  79.             {
  80.                 case L: --current_index; break;
  81.                 case R: ++current_index; break;
  82.                 case S: break;
  83.                 case Q: end = true; break;
  84.             }
  85.             cout<<tmp<<endl;
  86.         }
  87.     }
  88. private:
  89.     vector<State>states;
  90.     string input;
  91. };
  92.  
  93. void Skip(char c, vector<unsigned char>& text, unsigned& i)
  94. {
  95.     while(i < text.size())
  96.     {
  97.         if(text[i] != c) { break; }
  98.         ++i;
  99.     }
  100. }
  101.  
  102. string GetWord(vector<unsigned char>& text, unsigned& from)
  103. {
  104.     string buffer;
  105.     Skip(' ', text, from);
  106.     Skip('\n', text, from);
  107.     while(from < text.size())
  108.     {
  109.         if(text[from] != ' ' && text[from] != '\n')
  110.         { buffer += text[from]; ++from; }
  111.         else break;
  112.     }
  113.     return buffer;
  114. }
  115.  
  116. int main()
  117. {
  118.     TuringMachine machine;
  119.  
  120.     string filename;
  121.     cout<<"Source:";
  122.     cin>>filename;
  123.     fstream file(filename);
  124.  
  125.     vector<unsigned char>source;
  126.     while (!file.eof())
  127.     {
  128.         char ch = 0;
  129.         file.get(ch);
  130.         source.push_back(ch);
  131.     }
  132.  
  133.     file.close();
  134.  
  135.     for(unsigned i = 0; i < static_cast<unsigned>(source.size()); )
  136.     {
  137.         State st;
  138.         string tmp;
  139.         st.name = GetWord(source, i);
  140.         tmp = GetWord(source, i);
  141.         if(tmp.size() > 1) {cout<<1<<endl;break;}
  142.         st.input_symbol = tmp[0];
  143.         st.next_state = GetWord(source, i);
  144.         tmp = GetWord(source, i);
  145.         if(tmp.size() > 1) {cout<<2<<endl;break;}
  146.         st.output_symbol = tmp[0];
  147.         tmp = GetWord(source, i);
  148.         if(tmp.size() > 2) {cout<<3<<endl;break;}
  149.         bool fail = false;
  150.         switch(tmp[0])
  151.         {
  152.             case 'L': st.dir = L; break;
  153.             case 'R': st.dir = R; break;
  154.             case 'S': st.dir = S; break;
  155.             case 'Q': st.dir = Q; break;
  156.             default: fail = true; break;
  157.         }
  158.         if(fail){break;}
  159.         machine.AddState(st);
  160.         //if(source[i] != '\n') {break;}
  161.     }
  162.  
  163.     machine.Run();
  164.  
  165.     system("PAUSE");
  166.     return EXIT_SUCCESS;
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement