Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- #include <vector>
- #include <string>
- #include <iostream>
- using namespace std;
- enum Direction{L, R, S, Q,};
- struct State
- {
- string name;
- char input_symbol;
- string next_state;
- char output_symbol;
- Direction dir;
- void Print()
- {
- cout<<name<<" "<<input_symbol<<" "<<next_state<<" "<<output_symbol<<" ";
- switch(dir)
- {
- case L: cout<<"L"<<endl; break;
- case R: cout<<"R"<<endl; break;
- case S: cout<<"S"<<endl; break;
- case Q: cout<<"Q"<<endl; break;
- }
- }
- };
- class TuringMachine
- {
- public:
- void AddState(State &state)
- {
- states.push_back(state);
- state.Print();
- }
- void Run()
- {
- cout<<"Input:";
- cin>>input;
- string tmp = input;
- bool end = false;
- unsigned current_index = 0;
- State st = states[0];
- string name_needed = st.name;
- cout<<endl;
- while(!end)
- {
- unsigned i = 0;
- bool fail = true;
- while(i < states.size())
- {
- if(states[i].name == name_needed)
- {
- if(states[i].input_symbol == tmp[current_index])
- {
- st = states[i];
- fail = false;
- break;
- }
- }
- ++i;
- }
- if(fail){cout<<"Error!"<<endl;
- st.Print();
- cout<<"Current symbol: "<<tmp[current_index]<<endl;break;}
- tmp[current_index] = st.output_symbol;
- name_needed = st.next_state;
- switch(st.dir)
- {
- case L: --current_index; break;
- case R: ++current_index; break;
- case S: break;
- case Q: end = true; break;
- }
- cout<<tmp<<endl;
- }
- }
- private:
- vector<State>states;
- string input;
- };
- void Skip(char c, vector<unsigned char>& text, unsigned& i)
- {
- while(i < text.size())
- {
- if(text[i] != c) { break; }
- ++i;
- }
- }
- string GetWord(vector<unsigned char>& text, unsigned& from)
- {
- string buffer;
- Skip(' ', text, from);
- Skip('\n', text, from);
- while(from < text.size())
- {
- if(text[from] != ' ' && text[from] != '\n')
- { buffer += text[from]; ++from; }
- else break;
- }
- return buffer;
- }
- int main()
- {
- TuringMachine machine;
- string filename;
- cout<<"Source:";
- cin>>filename;
- fstream file(filename);
- vector<unsigned char>source;
- while (!file.eof())
- {
- char ch = 0;
- file.get(ch);
- source.push_back(ch);
- }
- file.close();
- for(unsigned i = 0; i < static_cast<unsigned>(source.size()); )
- {
- State st;
- string tmp;
- st.name = GetWord(source, i);
- tmp = GetWord(source, i);
- if(tmp.size() > 1) {cout<<1<<endl;break;}
- st.input_symbol = tmp[0];
- st.next_state = GetWord(source, i);
- tmp = GetWord(source, i);
- if(tmp.size() > 1) {cout<<2<<endl;break;}
- st.output_symbol = tmp[0];
- tmp = GetWord(source, i);
- if(tmp.size() > 2) {cout<<3<<endl;break;}
- bool fail = false;
- switch(tmp[0])
- {
- case 'L': st.dir = L; break;
- case 'R': st.dir = R; break;
- case 'S': st.dir = S; break;
- case 'Q': st.dir = Q; break;
- default: fail = true; break;
- }
- if(fail){break;}
- machine.AddState(st);
- //if(source[i] != '\n') {break;}
- }
- machine.Run();
- system("PAUSE");
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement