Created
July 12, 2015 13:48
-
-
Save devpruthvi/f6a193d4ec0bc7fe6dd0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include <stdio.h> | |
#include <string.h> | |
#include <string.h> | |
/* Define an array of transitions to sort. */ | |
int transtablecount; | |
char id[100]; | |
struct transition | |
{ | |
const char curstate[7]; | |
const char nextstate[5]; | |
}; | |
struct transition transtable[100]; | |
int transition_cmp (const struct transition *t1, const struct transition *t2) | |
{ | |
return strcmp (t1->curstate, t2->curstate); | |
} | |
char* find_transition (const char *curstate) { | |
struct transition target, *result; | |
strcpy(target.curstate, curstate); | |
result = bsearch (&target, transtable, transtablecount, sizeof (struct transition), | |
transition_cmp); | |
if(result) | |
{ | |
return result->nextstate; | |
} | |
else | |
return ""; | |
/* if (result) | |
printf("Next state: %s\n",result->nextstate); | |
else | |
printf ("Couldn't find %s.\n", curstate); */ | |
} | |
int main (void) | |
{ | |
char states[10][3]; | |
char inputsymbols[20],tempcurstate[5],nextstate[5],presentstate[5]; | |
char curstate[10]; | |
int i,j,k,numstates,numinps; | |
char curchar; | |
struct transition temp; | |
printf("Enter number of states in DFA: "); | |
scanf("%d",&numstates); | |
printf("Enter the states of DFA ( 'q0' for initial state and 'qf' for final state:\n"); | |
for(i=0;i<numstates;i++) | |
{ | |
scanf("%s",states[i]); | |
} | |
printf("Enter number of input symbols: "); | |
scanf("%d",&numinps); | |
printf("Number of input symbols: %d\n",numinps); | |
for(i=0;i<numinps;i++) | |
{ | |
scanf(" %c",&inputsymbols[i]); | |
} | |
strcpy(tempcurstate,""); | |
transtablecount = 0; | |
for(i=0;i<numstates;i++) | |
{ | |
printf("Enter transitions for state %s\n",states[i]); | |
for(j=0;j<numinps;j++) | |
{ | |
strcpy(tempcurstate,states[i]); | |
for(k=0;k<sizeof(tempcurstate);k++) | |
{ | |
if(tempcurstate[k] == '\0') | |
{ | |
tempcurstate[k] = inputsymbols[j]; | |
tempcurstate[k+1] = '\0'; | |
break; | |
} | |
} | |
strcpy(transtable[transtablecount].curstate,tempcurstate); | |
printf("\tEnter next state on input %c:",inputsymbols[j]); | |
scanf("%s",nextstate); | |
strcpy(transtable[transtablecount].nextstate ,nextstate); | |
transtablecount++; | |
} | |
} | |
qsort (transtable, transtablecount-1, sizeof (struct transition), transition_cmp); | |
for (i = 0; i < transtablecount; i++) | |
printf("\nInput: %s Output: %s\n",(&transtable[i])->curstate,(&transtable[i])->nextstate); | |
printf ("\n"); | |
while(1) | |
{ | |
printf("Enter a string to show it's Instantaneous Description: "); | |
scanf("%s",&id); | |
printf("%s",id); | |
strcpy(curstate,"q0"); | |
for(i=0;i<strlen(id);i++) | |
{ | |
strcpy(presentstate,curstate); | |
curchar = id[i]; | |
if(curchar == '\0') | |
break; | |
for(j=0;j<sizeof(presentstate);j++) | |
{ | |
if(presentstate[j] == '\0') | |
{ | |
presentstate[j] = curchar; | |
presentstate[j+1] = '\0'; | |
break; | |
} | |
} | |
strcpy(curstate,find_transition(presentstate)); | |
printf("\nINPUT SYMBOL: %c , NEXT STATE: %s\n",curchar,curstate); | |
} | |
if(strcmp(curstate,"qf") == 0) | |
printf("STRING ACCEPTED"); | |
else | |
printf("STRING NOT ACCEPTED"); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment