Created
December 29, 2021 18:39
-
-
Save ppcamp/abd1173a63af20b4d375b7f563ae747a to your computer and use it in GitHub Desktop.
A simple code that implements a state machine in c++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* A abordagem adotada é a mais próxima do AFD desenvolvido. | |
* Problemas: Para uma cadeia de chars mt grande pode ocasionar | |
* em estouro da pilha | |
*/ | |
#include <iostream> | |
using namespace std; | |
bool S1(string, int &); | |
bool S2(string, int &); | |
bool check(string s) | |
{ | |
int pos = 0; | |
return S1(s, pos); | |
} | |
int main(int argc, char **argv) | |
{ | |
if (check(argv[1])) | |
cout << "Passou com sucesso! String aceita.\n"; | |
else | |
cout << "Encontrou um char fora da ordem.\n"; | |
} | |
bool S2(string s, int &pos) | |
{ | |
if (pos == s.size()) | |
return true; | |
switch (s[pos]) | |
{ | |
case 'a': | |
return S1(s, ++pos); | |
case 'd': | |
return S1(s, ++pos); | |
case 'c': | |
return S2(s, ++pos); | |
default: | |
return false; | |
} | |
} | |
bool S1(string s, int &pos) | |
{ | |
if (pos == (s.size() - 1)) | |
return true; | |
switch (s[pos]) | |
{ | |
case 'a': | |
return S1(s, ++pos); | |
case 'b': | |
return S1(s, ++pos); | |
case 'd': | |
return S1(s, ++pos); | |
case 'c': | |
return S2(s, ++pos); | |
default: | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment