Algorithm:
Step 1: Get the string.
for(k=0;k(x[k]=getchar())!='\n'; k++)
Step 2: Set count=k and flag=a (starting state).
Step 3: For k=0 to k<count
i. Check the flag.
ii. If flag=state(any possible state of automata), choose that state.
iii. Inside that state
If x[k]=1
flag=state1(i.e another state that is reached when input bit to present state is 1).
Else
flag=state2 (i.e another state that is reached when input bit to present state is 0).
iv. Increment the value of k.
Step 4: If flag=any final state
Display string is accepted.
Else
Display string is not accepted.
Step 5: End of the program.
Coding:
#include<stdio.h>
#include<conio.h>
void main()
{
int k,count,a=0,b=1,c=2,d=3,e=4,phai=5,flag;
char x[15];
clrscr();
printf("\n Evaluation of -NFA");
printf("\n Enter the string: \n");
for(k=0;(x[k]=getchar())!='\n';k++);
count=k;
flag=a;
for(k=0;k<count;k++)
{
if(flag==a)
{
if(x[k]=='1')
{
if(x[k+1]=='\n')
flag=e;
else
flag=d;
}
else
{
if(x[k+1]=='\n')
flag=e;
else if(x[k+1]=='0')
{
if(x[k+2]=='\n')
flag=d;
else
flag=a;
}
else
{
if(x[k+2]=='\n')
flag=b;
else if(x[k+2]=='1')
{
if(x[k+3]=='\n')
flag=c;
else
flag=d;
}
else
{
if(x[k+3]=='\n')
flag=d;
else
flag=c;
}
}
}
}
else if(flag==b)
{
if(x[k]=='0')
flag=c;
else
flag=e;
}
else if(flag==c)
{
if(x[k]=='0')
flag=phai;
else
flag=b;
}
else if(flag==d)
{
if(x[k]=='0')
flag=e;
else
flag=d;
}
else if(flag==e)
{
flag=phai;
}
else
{
flag=phai;
}
}
if(flag==e)
printf("\n String is accepted.");
else
printf("\n String is not accepted.");
getch();
}
No comments:
Post a Comment