Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int main()
- {
- vector<state> StateVector;
- ifstream example_file;
- ofstream output;
- output.open("file_out.txt");
- example_file.open("file._");
- string line,command;
- char transition;
- int id1,id2;
- if(example_file.is_open())
- {
- List automates_list;
- while (getline(example_file,line))
- {
- if(line=="Command")
- {
- string command;
- while(example_file>>command && command!=";")
- {
- if(command=="Print")
- {
- example_file>>id1;
- output<<"print"<<endl;
- automates_list.getAutomat(id1).print(output);
- }
- if(command=="List")
- {
- output<<"list"<<endl;
- Listfunction(automates_list,output);
- }
- if(command=="Union")
- {
- example_file>>id1>>id2;
- Union(automates_list,id1,id2,output);
- }
- if(command=="Un")
- {
- example_file>>id1>>id2;
- Un(automates_list,id1,output);
- }
- if(command=="Concat")
- {
- example_file>>id1>>id2;
- Concat(automates_list,id1,id2,output);
- }
- if(command=="Empty")
- {
- example_file>>id1;
- Empty(automates_list,id1,output);
- }
- if(command=="Final")
- {
- example_file>>id1;
- Final(automates_list,id1,output);
- }
- if(command=="Recognise")
- {
- string word;
- example_file>>id1;
- example_file>>word;
- int n = word.length();
- char* _word= new char[n+1];
- strcpy(_word, word.c_str());
- Recognise(automates_list,id1,_word,output);
- }
- }
- }
- else
- {
- if(line=="Automat")//reading Automate
- {
- state * final_states;
- state start_state;
- int number_final_states=0;
- char state_name;
- string automate_part;
- //reading the states
- while(example_file>>state_name && state_name!=';')
- {
- Transition new_transition;
- state new_s{state_name,new_transition};
- StateVector.push_back(new_s);
- }
- for(int index=0; index<StateVector.size(); index++)
- {
- output<<StateVector[index].getState_name()<<endl;
- }
- //reading transitions
- char transition_symbol;
- char _to_state;
- while(example_file>>state_name && state_name!=';' && _to_state!=';')
- {
- if(StateVector.empty())
- {
- output<<"Error"<<endl;
- break;
- }
- else
- {
- int size=StateVector.size();
- int index=0;
- while(StateVector[index].getState_name()!=state_name && index<size)
- index++;
- if(index==size)
- {
- output<<"Error";
- break;
- }
- else
- {
- example_file>>transition_symbol;
- example_file>>_to_state;
- pointer new_pointer{_to_state,transition_symbol};
- StateVector[index].addPointer(new_pointer);
- }
- }
- }
- //reading start state and final states
- string start_or_final;
- while(getline(example_file,start_or_final))
- {
- //start state reading
- if(start_or_final=="Start")
- {
- int index=0;
- example_file>>state_name;
- while(StateVector[index].getState_name()!=state_name && index<StateVector.size())
- index++;
- if(index<StateVector.size())
- {
- start_state=StateVector[index];
- }
- else
- {
- output<<"Error in start state"<<endl;
- }
- }
- //final states reading
- if(start_or_final=="Final")
- {
- example_file>>number_final_states;
- final_states=new state[number_final_states+1];
- int input_index=0;
- while(example_file>>state_name && input_index<number_final_states && state_name!=';')
- {
- int index=0;
- while(StateVector[index].getState_name()!=state_name && index<StateVector.size())
- index++;
- if(index<StateVector.size())
- {
- final_states[input_index]=StateVector[input_index];
- input_index++;
- }
- else
- output<<"Error in final states"<<endl;
- }
- }
- }
- //we will copy th vector here ,because our automate work with array not with vector
- state* states=new state[StateVector.size()];//we will copy th vector here ,because our automate work with array not with vector
- for(int copy_index=0; copy_index<StateVector.size(); copy_index++)
- {
- states[copy_index]=StateVector[copy_index];
- }
- int number_of_states=StateVector.size();
- Automat new_automat(start_state,states,number_of_states,final_states,number_final_states);
- Nfa automat_with_id(new_automat);
- automates_list.add_element(automat_with_id);
- new_automat.print(output);
- }
- }
- }
- example_file.close();
- }
- return 0;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement