Advertisement
Guest User

Untitled

a guest
Mar 21st, 2019
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.14 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <set>
  5. #include <stdlib.h>
  6. #include <conio.h>
  7.  
  8. using namespace std;
  9.  
  10. class Transition {
  11. private:
  12. int Node;
  13. char Value;
  14. public:
  15. friend class NFA;
  16. Transition(int _Node, char _Value = '\0') : Node(_Node), Value(_Value) {}
  17. };
  18.  
  19. class NFA {
  20. private:
  21. int Nodes;
  22. vector < vector <Transition> > Graph;
  23. set <int> Final;
  24. public:
  25. NFA(int _Nodes) : Nodes(_Nodes) {Graph.resize(_Nodes);}
  26. void Insert(int, Transition);
  27. bool Accept(int, char*);
  28. void SetFinal(set <int> q) {Final = q;}
  29. };
  30.  
  31. void NFA :: Insert(int Node, Transition A) {
  32. Graph[Node].push_back(A);
  33. }
  34.  
  35. bool NFA :: Accept(int Curr, char* word) {
  36. if (word[0] == '\0') {
  37. if (Final.find(Curr) != Final.end()) return 1;
  38. return 0;
  39. }
  40. vector <Transition> :: iterator it;
  41. bool OK = 0;
  42. for (it = Graph[Curr].begin(); it != Graph[Curr].end(); ++it) {
  43. if ((*it).Value == word[0]) OK = max(OK, Accept((*it).Node, word + 1));
  44. }
  45. if (OK == 1) return 1;
  46. return 0;
  47. }
  48.  
  49. int main()
  50. {
  51. cout << "~~~~~~~IFRIMENCO ALEXANDRU - DANIEL - LIMBAJE FORMALE SI AUTOMATE - TEMA 1~~~~~~~~\n\n\n\n\n\n";
  52. cout << "PENTRU A SCHIMBA DATELE DESPRE AUTOMAT ACCESATI FISERUL DATE.IN\n\n";
  53. cout << "ORDINE DATE.IN:\nLINIA 1: NR. STARI\nLINIILE 2&3 : NR. STARI FINALE SI INDICELE ACESOTRA\nLINIA 4: M - NR. TRANZITII\nURMATOARELE M LINII - TRANZITILE\n";
  54. getch();
  55. system("cls");
  56. ifstream fin("date.in");
  57. int x, y, m, n;
  58. set <int> a;
  59. char z;
  60. fin >> n;
  61. NFA A(n);
  62. fin >> m;
  63. for (int i = 0; i < m; ++i) {
  64. fin >> x;
  65. a.insert(x - 1);
  66. }
  67. A.SetFinal(a);
  68. fin >> m;
  69. for (int i = 0; i < m; ++i) {
  70. fin >> x >> y >> z;
  71. Transition q(y - 1, z);
  72. A.Insert(x - 1, q);
  73. }
  74. int Curr;
  75. char word[100];
  76. cout << "Introduceti cuvantul care urmeaza a fi verificat si starea initiala\n";
  77. cin.get(word, 99);
  78. cin >> Curr;
  79. if (A.Accept(Curr - 1, word)) cout << "\n\nCUVANT ACCEPTAT\n";
  80. else cout << "CUVANT REFUZAT\n";
  81. return 0;
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement