Advertisement
Guest User

Untitled

a guest
Jun 15th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.58 KB | None | 0 0
  1. int main()
  2. {
  3. vector<state> StateVector;
  4. ifstream example_file;
  5. ofstream output;
  6. output.open("file_out.txt");
  7. example_file.open("file._");
  8. string line,command;
  9. char transition;
  10. int id1,id2;
  11.  
  12. if(example_file.is_open())
  13. {
  14. List automates_list;
  15. while (getline(example_file,line))
  16. {
  17. if(line=="Command")
  18. {
  19. string command;
  20. while(example_file>>command && command!=";")
  21. {
  22. if(command=="Print")
  23. {
  24. example_file>>id1;
  25. output<<"print"<<endl;
  26. automates_list.getAutomat(id1).print(output);
  27.  
  28. }
  29. if(command=="List")
  30. {
  31. output<<"list"<<endl;
  32. Listfunction(automates_list,output);
  33.  
  34. }
  35. if(command=="Union")
  36. {
  37. example_file>>id1>>id2;
  38. Union(automates_list,id1,id2,output);
  39. }
  40. if(command=="Un")
  41. {
  42. example_file>>id1>>id2;
  43. Un(automates_list,id1,output);
  44. }
  45. if(command=="Concat")
  46. {
  47. example_file>>id1>>id2;
  48. Concat(automates_list,id1,id2,output);
  49. }
  50. if(command=="Empty")
  51. {
  52. example_file>>id1;
  53. Empty(automates_list,id1,output);
  54. }
  55. if(command=="Final")
  56. {
  57. example_file>>id1;
  58. Final(automates_list,id1,output);
  59. }
  60. if(command=="Recognise")
  61. {
  62. string word;
  63. example_file>>id1;
  64. example_file>>word;
  65. int n = word.length();
  66. char* _word= new char[n+1];
  67. strcpy(_word, word.c_str());
  68. Recognise(automates_list,id1,_word,output);
  69. }
  70.  
  71. }
  72. }
  73. else
  74. {
  75. if(line=="Automat")//reading Automate
  76. {
  77. state * final_states;
  78. state start_state;
  79. int number_final_states=0;
  80. char state_name;
  81. string automate_part;
  82. //reading the states
  83. while(example_file>>state_name && state_name!=';')
  84. {
  85. Transition new_transition;
  86. state new_s{state_name,new_transition};
  87. StateVector.push_back(new_s);
  88. }
  89. for(int index=0; index<StateVector.size(); index++)
  90. {
  91. output<<StateVector[index].getState_name()<<endl;
  92. }
  93. //reading transitions
  94. char transition_symbol;
  95. char _to_state;
  96. while(example_file>>state_name && state_name!=';' && _to_state!=';')
  97. {
  98. if(StateVector.empty())
  99. {
  100. output<<"Error"<<endl;
  101. break;
  102. }
  103. else
  104. {
  105. int size=StateVector.size();
  106. int index=0;
  107. while(StateVector[index].getState_name()!=state_name && index<size)
  108. index++;
  109. if(index==size)
  110. {
  111. output<<"Error";
  112. break;
  113. }
  114. else
  115. {
  116. example_file>>transition_symbol;
  117. example_file>>_to_state;
  118. pointer new_pointer{_to_state,transition_symbol};
  119. StateVector[index].addPointer(new_pointer);
  120. }
  121. }
  122. }
  123. //reading start state and final states
  124. string start_or_final;
  125. while(getline(example_file,start_or_final))
  126. {
  127. //start state reading
  128. if(start_or_final=="Start")
  129. {
  130. int index=0;
  131. example_file>>state_name;
  132. while(StateVector[index].getState_name()!=state_name && index<StateVector.size())
  133. index++;
  134. if(index<StateVector.size())
  135. {
  136. start_state=StateVector[index];
  137. }
  138. else
  139. {
  140. output<<"Error in start state"<<endl;
  141. }
  142. }
  143. //final states reading
  144. if(start_or_final=="Final")
  145. {
  146. example_file>>number_final_states;
  147. final_states=new state[number_final_states+1];
  148. int input_index=0;
  149. while(example_file>>state_name && input_index<number_final_states && state_name!=';')
  150. {
  151. int index=0;
  152. while(StateVector[index].getState_name()!=state_name && index<StateVector.size())
  153. index++;
  154. if(index<StateVector.size())
  155. {
  156. final_states[input_index]=StateVector[input_index];
  157. input_index++;
  158. }
  159. else
  160. output<<"Error in final states"<<endl;
  161.  
  162. }
  163. }
  164. }
  165. //we will copy th vector here ,because our automate work with array not with vector
  166. state* states=new state[StateVector.size()];//we will copy th vector here ,because our automate work with array not with vector
  167. for(int copy_index=0; copy_index<StateVector.size(); copy_index++)
  168. {
  169. states[copy_index]=StateVector[copy_index];
  170. }
  171. int number_of_states=StateVector.size();
  172. Automat new_automat(start_state,states,number_of_states,final_states,number_final_states);
  173. Nfa automat_with_id(new_automat);
  174. automates_list.add_element(automat_with_id);
  175. new_automat.print(output);
  176. }
  177.  
  178. }
  179. }
  180.  
  181. example_file.close();
  182. }
  183. return 0;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement