Sei sulla pagina 1di 4

#include <stdio.

h>
#include <stdlib.h>
#include <string.h>

int main()
{
int num_input,num_states,num_final,i,j,k,c=0,count=1;
char input_symbols[10],states[10],final_states[10],initial_state,T_T[10]
[10],input[100],p_s,f_s;

printf("\nEnter number of input symbols : ");


scanf("%d",&num_input);
printf("\nEnter the input symbols : ");
for(i=0;i<num_input;i++)
{
printf("\n%d symbol : ",i+1);
scanf(" %c",&input_symbols[i]);
}

printf("\nEnter number of states : ");


scanf("%d",&num_states);
printf("\nEnter the states : ");
for(i=0;i<num_states;i++)
{
printf("\n%d state : ",i+1);
scanf(" %c",&states[i]);
}

printf("\nEnter the initial state : ");


scanf(" %c",&initial_state);

printf("\nEnter number of final states : ");


scanf("%d",&num_final);
printf("\nEnter the states : ");
for(i=0;i<num_final;i++)
{
printf("\n%d final state : ",i+1);
scanf(" %c",&final_states[i]);
}

printf("\nEnter the transition table : ");


for(i=0;i<num_input;i++)
{
for(j=0;j<num_states;j++)
{
printf("\nEnter the next transition state for (%c,%c) :
",states[j],input_symbols[i]);
scanf(" %c",&T_T[i][j]);
}
}

printf("\nThe respected DFA is saved");


while(count!=0)
{
printf("\nEnter a string to check whether it is accepted in the above
DFA : ");
scanf("%s",input);
p_s=initial_state;
for(i=0;i<strlen(input);i++)
{
for(j=0;j<num_input;j++)
{
c=0;
if(input[i]==input_symbols[j])
{
c=1;
break;
}
}
if(c==0)
{
printf("\nThe string is not accepted by the above DFA\n");
break;
}

for(j=0;input_symbols[j]!=input[i];j++);
for(k=0;states[k]!=p_s;k++);
p_s=T_T[j][k];
}
f_s=p_s;
c=0;
for(i=0;i<num_final;i++)
{
if(final_states[i]==f_s)
{
c=1;
}
}
if(c==1)
printf("\nThe string is accepted by the above DFA\n");
else
printf("\nThe string is not accepted by the above DFA\n");

printf("\nDo you want to check another string?");


printf("\n0 : No\t1 : yes\nYour choice : ");
scanf("%d",&count);
}
return 0;
}
/*
OUTPUT :
gcoej@gcoej-ThinkCentre-M70z-Invalid-entry-length-16-Fixed-up-to-11:~$ gcc
1003_03.c
gcoej@gcoej-ThinkCentre-M70z-Invalid-entry-length-16-Fixed-up-to-11:~$ ./a.out
Enter number of input symbols : 2

Enter the input symbols :


1 symbol : a

2 symbol : b

Enter number of states : 5

Enter the states :


1 state : A
2 state : B

3 state : C

4 state : D

5 state : E

Enter the initial state : A

Enter number of final states : 1

Enter the states :


1 final state : E

Enter the transition table :


Enter the next transition state for (A,a) : B

Enter the next transition state for (B,a) : D

Enter the next transition state for (C,a) : C

Enter the next transition state for (D,a) : E

Enter the next transition state for (E,a) : C

Enter the next transition state for (A,b) : C

Enter the next transition state for (B,b) : C

Enter the next transition state for (C,b) : C

Enter the next transition state for (D,b) : C

Enter the next transition state for (E,b) : C

The respected DFA is saved


Enter a string to check whether it is accepted in the above DFA : aaa

The string is accepted by the above DFA

Do you want to check another string?


0 : No 1 : yes
Your choice : 1

Enter a string to check whether it is accepted in the above DFA : aaab

The string is not accepted by the above DFA

Do you want to check another string?


0 : No 1 : yes
Your choice : 1

Enter a string to check whether it is accepted in the above DFA : aaba

The string is not accepted by the above DFA

Do you want to check another string?


0 : No 1 : yes
Your choice : 1

Enter a string to check whether it is accepted in the above DFA : asda

The string is not accepted by the above DFA

The string is not accepted by the above DFA

Do you want to check another string?


0 : No 1 : yes
Your choice : 0
gcoej@gcoej-ThinkCentre-M70z-Invalid-entry-length-16-Fixed-up-to-11:~$
*/