-
Notifications
You must be signed in to change notification settings - Fork 71
/
CheckValidDFA.cpp
102 lines (90 loc) · 2.18 KB
/
CheckValidDFA.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <iostream>
using namespace std;
int no_of_states;
int nextStateIndex(char current_state, char states[])
{
for (int i = 0; i < no_of_states; i++)
{
if (states[i] == current_state)
{
return i;
}
}
}
int main()
{
char table[50][2];
string check_string;
int no_of_final_states;
int input_symbols = 2;
char initial_state;
char current_state;
char next_state;
cout << "Enter no. of states: ";
cin >> no_of_states;
char states[no_of_states];
cout << "Enter states: ";
for (int i = 0; i < no_of_states; i++)
{
cin >> states[i];
}
cout << "Enter initial state: ";
cin >> initial_state;
current_state = initial_state;
cout << "Enter number of final states : ";
cin >> no_of_final_states;
char final_state[no_of_final_states];
cout << "Enter Final states : ";
for (int i = 0; i < no_of_final_states; i++)
{
cin >> final_state[i];
}
for (int i = 0; i < no_of_states; i++)
{
for (int j = 0; j < input_symbols; j++)
{
cout << "Enter next state for " << states[i] << " state when input symbol is " << j << " : ";
cin >> table[i][j];
}
}
char choice;
do
{
cout << "Enter string to check whether it can be accepted or not : ";
cin >> check_string;
for (int i = 0; i < check_string.length(); i++)
{
int tmp;
int temp = nextStateIndex(current_state, states);
if (check_string[i] == '0')
{
tmp = 0;
}
else
{
tmp = 1;
}
next_state = table[temp][tmp];
cout << current_state << " -> " << check_string[i] << " -> " << next_state << endl;
current_state = next_state;
}
int flag = 0;
for (int i = 0; i < no_of_final_states; i++)
{
if (current_state == final_state[i])
{
cout << "String accepted" << endl;
flag = 1;
break;
}
}
if (flag == 0)
{
cout << "String not accepted" << endl;
}
cout << "Do you want to check another string ? ";
cin >> choice;
current_state = initial_state;
} while (choice == ('y' | 'Y'));
return 0;
}