-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNFAsecond.cpp
More file actions
131 lines (102 loc) · 2.07 KB
/
NFAsecond.cpp
File metadata and controls
131 lines (102 loc) · 2.07 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include<stdio.h>
#include<string.h>
#define fl(i,a,b) for(i=a; i<b; i++)
#define scan(a) scanf("%d", &a)
#define nline printf("\n")
#define MAX 1000
int states, symbols, symdir[20], final_states, mark[20], mat[20][20][20];
char str1[MAX];
int curr[20], t[20];
int ind, ind1;
int l1;
int main()
{
int i, j, k;
fl(i,0,20)
fl(j,0,20)
fl(k,0,20)
mat[i][j][k]=-1;
printf("Enter the number of states : ");
scan(states);
printf("Enter the number of symbols : ");
scan(symbols);
printf("Enter the symbols : ");
nline;
fl(i,0,symbols)
{
printf("Enter the symbol number %d : ", i);
scan(symdir[i]);
}
printf("Enter the number of final states : ");
scan(final_states);
printf("Enter the number of the states which are final : ");
fl(i,0,final_states)
{
int temp;
scan(temp);
mark[temp]=1;
}
printf("Define the relations for the states and symbols : ");
nline;
fl(i,0,states)
{
fl(j,0,symbols)
{
int ntemp;
printf("Enter the number of relations for Q(%d) -> %d : ", i, symdir[j]);
scan(ntemp);
fl(k,0,ntemp)
{
printf("Enter the relation number %d for Q(%d) -> %d : ", k, i, symdir[j]);
scan(mat[i][symdir[j]][k]);
}
}
}
//--------------------------------------------------------//
int cases;
printf("Enter the number of strings to be tested : ");
scan(cases);
fl(k,0,cases)
{
printf("Enter the string to be tested : ");
scanf("%s", str1);
curr[0]=0;
ind=1;
int limit=strlen(str1);
fl(i,0,limit)
{
int ele=(int)(str1[i]-'0');
ind1=0;
fl(l1,0,ind)
{
j=0;
while(mat[curr[l1]][ele][j]!=-1)
{
t[ind1++]=mat[curr[l1]][ele][j];
j++;
}
}
fl(l1,0,ind1)
{
curr[l1]=t[l1];
}
ind=ind1;
}
int flag=0;
fl(i,0,ind)
{
if(mark[curr[i]]==1)
{
flag=1;
break;
}
}
printf("The entered string is ");
if(flag==1)
printf("Accepted");
else
printf("Rejected");
nline;
}
return 0;
}