Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifdef _MSC_VER
- #define _CRT_SECURE_NO_WARNINGS
- #endif
- #include <iostream>
- #include <string.h>
- using namespace std;
- struct Masina {
- const char* Q;
- const char* G;
- char B;
- const char* T;
- char P[100][5];
- int nrTranzitii;
- char Q0;
- const char* F;
- };
- bool esteDeterminista(Masina* m)
- {
- for (int i = 0; i < m->nrTranzitii; ++i)
- {
- char intrareCurenta = m->P[i][1];
- char stareCurenta = m->P[i][0];
- char stareUrmatoare = m->P[i][2];
- for (int j = i + 1; j < m->nrTranzitii; ++j)
- {
- if (m->P[j][0] == stareCurenta && m->P[j][1] != intrareCurenta && m->P[j][2] == stareUrmatoare)
- {
- return false;
- }
- }
- }
- return true;
- }
- void afisareInformatiiMasina(Masina* m)
- {
- cout << "Starile masinii sunt: ";
- for (int i = 0; i < strlen(m->Q); ++i)
- {
- cout << m->Q[i] << "; ";
- }
- cout << endl << "Alfabetul de lucru este: ";
- for (int i = 0; i < strlen(m->G); ++i)
- {
- cout << m->G[i] << "; ";
- }
- cout << endl << "Simbolul vid este \"" << m->B << "\".";
- cout << endl << "Alfabetul de intrare este: ";
- for (int i = 0; i < strlen(m->T); ++i)
- {
- cout << m->T[i] << "; ";
- }
- cout << endl << "Multimea tranzitiilor este: " << endl;
- for (int i = 0; i < m->nrTranzitii; ++i)
- {
- cout << "\t(" << m->P[i][0] << ") -- " << m->P[i][1] << " --> " << m->P[i][2] << endl;
- }
- cout << endl << "Starea initiala este \"" << m->Q0 << "\".";
- cout << endl << "Starile finale sunt: ";
- for (int i = 0; i < strlen(m->F); ++i)
- {
- cout << m->F[i] << "; ";
- }
- cout << endl << endl;
- }
- bool procesareIntrare(Masina* m, const char* sirCaractereIntrare)
- {
- char stareCurenta = m->Q0;
- char* memorieMasina = (char*)malloc(strlen(sirCaractereIntrare) * sizeof(char));
- strcpy(memorieMasina, sirCaractereIntrare);
- cout << "Procesare input \"" << sirCaractereIntrare << "\"..." << endl;
- int pozitieMemorie = 0;
- while (true)
- {
- int j;
- for (j = 0; j < m->nrTranzitii; ++j)
- {
- if (m->P[j][0] == stareCurenta && m->P[j][1] == memorieMasina[pozitieMemorie])
- {
- stareCurenta = m->P[j][2];
- memorieMasina[pozitieMemorie] = m->P[j][3];
- cout << "Stare curenta: " << stareCurenta << " | Pozitie memorie: " << pozitieMemorie <<
- " | Input curent: " << sirCaractereIntrare[pozitieMemorie] << " | Output intermediar: " << memorieMasina << endl;
- switch (m->P[j][4])
- {
- case 'R':
- ++pozitieMemorie;
- break;
- case 'L':
- --pozitieMemorie;
- break;
- case 'N':
- break;
- default:
- break;
- }
- break;
- }
- }
- if (j == m->nrTranzitii)
- {
- free(memorieMasina);
- return false;
- }
- if (pozitieMemorie < 0 || pozitieMemorie >= strlen(sirCaractereIntrare))
- {
- free(memorieMasina);
- return false;
- }
- if (strchr(m->F, stareCurenta) != 0)
- {
- cout << "Output final: " << memorieMasina << endl;
- free(memorieMasina);
- return true;
- }
- }
- }
- int main() {
- Masina m1 = {
- "AB",
- "axs",
- 's',
- "a",
- {
- {'A', 'a', 'A', 'x', 'R'},
- {'A', 's', 'B', 's', 'N'}
- },
- 2,
- 'A',
- "B"
- };
- afisareInformatiiMasina(&m1);
- if (esteDeterminista(&m1))
- {
- cout << "Masina este determinista." << endl;
- }
- else
- {
- cout << "Masina nu este determinista." << endl;
- }
- cout << endl;
- if (procesareIntrare(&m1, "aaaas"))
- {
- cout << "Intrarea \"aaaas\" este valida." << endl;
- }
- else
- {
- cout << "Intrarea \"aaaas\" este invalida." << endl;
- }
- cout << endl;
- if (procesareIntrare(&m1, "abb"))
- {
- cout << "Intrarea \"abb\" este valida." << endl;
- }
- else
- {
- cout << "Intrarea \"abb\" este invalida." << endl;
- }
- cout << endl;
- if (procesareIntrare(&m1, "abcbs"))
- {
- cout << "Intrarea \"abcbs\" este valida." << endl;
- }
- else
- {
- cout << "Intrarea \"abcbs\" este invalida." << endl;
- }
- cout << endl;
- if (procesareIntrare(&m1, "aabbs"))
- {
- cout << "Intrarea \"aabbs\" este valida." << endl;
- }
- else
- {
- cout << "Intrarea \"aabbs\" este invalida." << endl;
- }
- cout << endl << endl;
- Masina m2 = {
- "ABCD",
- "abxys",
- 's',
- "ab",
- {
- {'A', 's', 'D', 's', 'L'},
- {'A', 'y', 'A', 'y', 'R'},
- {'A', 'a', 'B', 'x', 'R'},
- {'B', 'y', 'B', 'y', 'R'},
- {'B', 'a', 'B', 'a', 'R'},
- {'B', 'b', 'C', 'y', 'L'},
- {'C', 'y', 'C', 'y', 'L'},
- {'C', 'a', 'C', 'a', 'L'},
- {'C', 'x', 'A', 'x', 'R'}
- },
- 9,
- 'A',
- "D"
- };
- afisareInformatiiMasina(&m2);
- if (esteDeterminista(&m2))
- {
- cout << "Masina este determinista." << endl;
- }
- else
- {
- cout << "Masina nu este determinista." << endl;
- }
- cout << endl;
- if (procesareIntrare(&m2, "abs"))
- {
- cout << "Intrarea \"abs\" este valida." << endl;
- }
- else
- {
- cout << "Intrarea \"abs\" este invalida." << endl;
- }
- cout << endl;
- if (procesareIntrare(&m2, "aabbs"))
- {
- cout << "Intrarea \"aabbs\" este valida." << endl;
- }
- else
- {
- cout << "Intrarea \"aabbs\" este invalida." << endl;
- }
- cout << endl;
- if (procesareIntrare(&m2, "abcbs"))
- {
- cout << "Intrarea \"abcbs\" este valida." << endl;
- }
- else
- {
- cout << "Intrarea \"abcbs\" este invalida." << endl;
- }
- cout << endl;
- if (procesareIntrare(&m2, "aaabbbs"))
- {
- cout << "Intrarea \"aaabbbs\" este valida." << endl;
- }
- else
- {
- cout << "Intrarea \"aaabbbs\" este invalida." << endl;
- }
- cout << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement