Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Alphabet A B C D E X
- // Strings to be recognized
- // String 1: CABED
- // String 2: ABBACCA
- // String 3: ECADE
- // String 4: ADACE
- #include <stdio.h>
- int automata_1(char c, int state);
- int automata_2(char c, int state);
- int automata_3(char c, int state);
- int automata_4(char c, int state);
- #define STATE_AUTOMATA_1_END 5
- #define STATE_AUTOMATA_2_END 7
- #define STATE_AUTOMATA_3_END 5
- #define STATE_AUTOMATA_4_END 5
- int main() {
- char user_input;
- int state_automata_1 = 0,
- state_automata_2 = 0,
- state_automata_3 = 0,
- state_automata_4 = 0;
- do {
- // Input character
- user_input = getchar();
- // Clearing input buffer
- getchar();
- // Exec and check automata 1
- state_automata_1 = automata_1(user_input, state_automata_1);
- if (state_automata_1 == STATE_AUTOMATA_1_END) {
- printf("Riconosciuta CABED\n");
- }
- // Exec and check automata 2
- state_automata_2 = automata_2(user_input, state_automata_2);
- if (state_automata_2 == STATE_AUTOMATA_2_END) {
- printf("Riconosciuta ABBACCA\n");
- }
- // Exec and check automata 3
- state_automata_3 = automata_3(user_input, state_automata_3);
- if (state_automata_3 == STATE_AUTOMATA_3_END) {
- printf("Riconosciuta ECADE\n");
- }
- // Exec and check automata 4
- state_automata_4 = automata_4(user_input, state_automata_4);
- if (state_automata_4 == STATE_AUTOMATA_4_END) {
- printf("Riconosciuta ADACE\n");
- }
- // printf(
- // "Current states: \n"
- // "Automata 1: %d"
- // "Automata 2: %d"
- // "Automata 3: %d"
- // "Automata 4: %d\n",
- // state_automata_1, state_automata_2, state_automata_3, state_automata_4
- // );
- } while (user_input != 'X');
- return 0;
- }
- int automata_1(char c, int state) {
- // C, 0
- // A, 1
- // B, 2
- // E, 3
- // D, 4
- if ((c == 'C' && state == 0) || (c == 'A' && state == 1) ||
- (c == 'B' && state == 2) || (c == 'E' && state == 3) ||
- (c == 'D' && state == 4)) {
- return state + 1;
- }
- return 0;
- }
- // ABBACCA
- int automata_2(char c, int state) {
- if ((c == 'A' && state == 0) || (c == 'B' && state == 1) ||
- (c == 'B' && state == 2) || (c == 'A' && state == 3) ||
- (c == 'C' && state == 4) || (c == 'C' && state == 5) ||
- (c == 'A' && state == 6)) {
- return state + 1;
- }
- return 0;
- }
- // ECADE
- int automata_3(char c, int state) {
- if ((c == 'E' && state == 0) || (c == 'C' && state == 1) ||
- (c == 'A' && state == 2) || (c == 'D' && state == 3) ||
- (c == 'E' && state == 4)) {
- return state + 1;
- }
- return 0;
- }
- // ADACE
- int automata_4(char c, int state) {
- if ((c == 'A' && state == 0) || (c == 'D' && state == 1) ||
- (c == 'A' && state == 2) || (c == 'C' && state == 3) ||
- (c == 'E' && state == 4)) {
- return state + 1;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement