Advertisement
Guest User

Untitled

a guest
May 21st, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.27 KB | None | 0 0
  1. #ifdef _MSC_VER
  2. #define _CRT_SECURE_NO_WARNINGS
  3. #endif
  4.  
  5. #include <iostream>
  6. #include <string.h>
  7.  
  8. using namespace std;
  9.  
  10. struct Masina {
  11. const char* Q;
  12. const char* G;
  13. char B;
  14. const char* T;
  15. char P[100][5];
  16. int nrTranzitii;
  17. char Q0;
  18. const char* F;
  19. };
  20.  
  21. bool esteDeterminista(Masina* m)
  22. {
  23. for (int i = 0; i < m->nrTranzitii; ++i)
  24. {
  25. char intrareCurenta = m->P[i][1];
  26. char stareCurenta = m->P[i][0];
  27. char stareUrmatoare = m->P[i][2];
  28.  
  29. for (int j = i + 1; j < m->nrTranzitii; ++j)
  30. {
  31. if (m->P[j][0] == stareCurenta && m->P[j][1] != intrareCurenta && m->P[j][2] == stareUrmatoare)
  32. {
  33. return false;
  34. }
  35. }
  36. }
  37. return true;
  38. }
  39.  
  40. void afisareInformatiiMasina(Masina* m)
  41. {
  42. cout << "Starile masinii sunt: ";
  43. for (int i = 0; i < strlen(m->Q); ++i)
  44. {
  45. cout << m->Q[i] << "; ";
  46. }
  47. cout << endl << "Alfabetul de lucru este: ";
  48. for (int i = 0; i < strlen(m->G); ++i)
  49. {
  50. cout << m->G[i] << "; ";
  51. }
  52. cout << endl << "Simbolul vid este \"" << m->B << "\".";
  53. cout << endl << "Alfabetul de intrare este: ";
  54. for (int i = 0; i < strlen(m->T); ++i)
  55. {
  56. cout << m->T[i] << "; ";
  57. }
  58. cout << endl << "Multimea tranzitiilor este: " << endl;
  59. for (int i = 0; i < m->nrTranzitii; ++i)
  60. {
  61. cout << "\t(" << m->P[i][0] << ") -- " << m->P[i][1] << " --> " << m->P[i][2] << endl;
  62. }
  63. cout << endl << "Starea initiala este \"" << m->Q0 << "\".";
  64. cout << endl << "Starile finale sunt: ";
  65. for (int i = 0; i < strlen(m->F); ++i)
  66. {
  67. cout << m->F[i] << "; ";
  68. }
  69. cout << endl << endl;
  70. }
  71.  
  72. bool procesareIntrare(Masina* m, const char* sirCaractereIntrare)
  73. {
  74. char stareCurenta = m->Q0;
  75. char* memorieMasina = (char*)malloc(strlen(sirCaractereIntrare) * sizeof(char));
  76. strcpy(memorieMasina, sirCaractereIntrare);
  77.  
  78. cout << "Procesare input \"" << sirCaractereIntrare << "\"..." << endl;
  79. int pozitieMemorie = 0;
  80. while (true)
  81. {
  82. int j;
  83. for (j = 0; j < m->nrTranzitii; ++j)
  84. {
  85. if (m->P[j][0] == stareCurenta && m->P[j][1] == memorieMasina[pozitieMemorie])
  86. {
  87. stareCurenta = m->P[j][2];
  88. memorieMasina[pozitieMemorie] = m->P[j][3];
  89. cout << "Stare curenta: " << stareCurenta << " | Pozitie memorie: " << pozitieMemorie <<
  90. " | Input curent: " << sirCaractereIntrare[pozitieMemorie] << " | Output intermediar: " << memorieMasina << endl;
  91. switch (m->P[j][4])
  92. {
  93. case 'R':
  94. ++pozitieMemorie;
  95. break;
  96. case 'L':
  97. --pozitieMemorie;
  98. break;
  99. case 'N':
  100. break;
  101. default:
  102. break;
  103. }
  104. break;
  105. }
  106. }
  107. if (j == m->nrTranzitii)
  108. {
  109. free(memorieMasina);
  110. return false;
  111. }
  112. if (pozitieMemorie < 0 || pozitieMemorie >= strlen(sirCaractereIntrare))
  113. {
  114. free(memorieMasina);
  115. return false;
  116. }
  117. if (strchr(m->F, stareCurenta) != 0)
  118. {
  119. cout << "Output final: " << memorieMasina << endl;
  120. free(memorieMasina);
  121. return true;
  122. }
  123. }
  124. }
  125.  
  126. int main() {
  127. Masina m1 = {
  128. "AB",
  129. "axs",
  130. 's',
  131. "a",
  132. {
  133. {'A', 'a', 'A', 'x', 'R'},
  134. {'A', 's', 'B', 's', 'N'}
  135. },
  136. 2,
  137. 'A',
  138. "B"
  139. };
  140.  
  141. afisareInformatiiMasina(&m1);
  142.  
  143. if (esteDeterminista(&m1))
  144. {
  145. cout << "Masina este determinista." << endl;
  146. }
  147. else
  148. {
  149. cout << "Masina nu este determinista." << endl;
  150. }
  151. cout << endl;
  152.  
  153. if (procesareIntrare(&m1, "aaaas"))
  154. {
  155. cout << "Intrarea \"aaaas\" este valida." << endl;
  156. }
  157. else
  158. {
  159. cout << "Intrarea \"aaaas\" este invalida." << endl;
  160. }
  161. cout << endl;
  162.  
  163. if (procesareIntrare(&m1, "abb"))
  164. {
  165. cout << "Intrarea \"abb\" este valida." << endl;
  166. }
  167. else
  168. {
  169. cout << "Intrarea \"abb\" este invalida." << endl;
  170. }
  171. cout << endl;
  172.  
  173. if (procesareIntrare(&m1, "abcbs"))
  174. {
  175. cout << "Intrarea \"abcbs\" este valida." << endl;
  176. }
  177. else
  178. {
  179. cout << "Intrarea \"abcbs\" este invalida." << endl;
  180. }
  181. cout << endl;
  182.  
  183. if (procesareIntrare(&m1, "aabbs"))
  184. {
  185. cout << "Intrarea \"aabbs\" este valida." << endl;
  186. }
  187. else
  188. {
  189. cout << "Intrarea \"aabbs\" este invalida." << endl;
  190. }
  191. cout << endl << endl;
  192.  
  193. Masina m2 = {
  194. "ABCD",
  195. "abxys",
  196. 's',
  197. "ab",
  198. {
  199. {'A', 's', 'D', 's', 'L'},
  200. {'A', 'y', 'A', 'y', 'R'},
  201. {'A', 'a', 'B', 'x', 'R'},
  202. {'B', 'y', 'B', 'y', 'R'},
  203. {'B', 'a', 'B', 'a', 'R'},
  204. {'B', 'b', 'C', 'y', 'L'},
  205. {'C', 'y', 'C', 'y', 'L'},
  206. {'C', 'a', 'C', 'a', 'L'},
  207. {'C', 'x', 'A', 'x', 'R'}
  208. },
  209. 9,
  210. 'A',
  211. "D"
  212. };
  213.  
  214. afisareInformatiiMasina(&m2);
  215.  
  216. if (esteDeterminista(&m2))
  217. {
  218. cout << "Masina este determinista." << endl;
  219. }
  220. else
  221. {
  222. cout << "Masina nu este determinista." << endl;
  223. }
  224. cout << endl;
  225.  
  226. if (procesareIntrare(&m2, "abs"))
  227. {
  228. cout << "Intrarea \"abs\" este valida." << endl;
  229. }
  230. else
  231. {
  232. cout << "Intrarea \"abs\" este invalida." << endl;
  233. }
  234. cout << endl;
  235.  
  236. if (procesareIntrare(&m2, "aabbs"))
  237. {
  238. cout << "Intrarea \"aabbs\" este valida." << endl;
  239. }
  240. else
  241. {
  242. cout << "Intrarea \"aabbs\" este invalida." << endl;
  243. }
  244. cout << endl;
  245.  
  246. if (procesareIntrare(&m2, "abcbs"))
  247. {
  248. cout << "Intrarea \"abcbs\" este valida." << endl;
  249. }
  250. else
  251. {
  252. cout << "Intrarea \"abcbs\" este invalida." << endl;
  253. }
  254. cout << endl;
  255.  
  256. if (procesareIntrare(&m2, "aaabbbs"))
  257. {
  258. cout << "Intrarea \"aaabbbs\" este valida." << endl;
  259. }
  260. else
  261. {
  262. cout << "Intrarea \"aaabbbs\" este invalida." << endl;
  263. }
  264. cout << endl;
  265.  
  266. return 0;
  267. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement