-
Notifications
You must be signed in to change notification settings - Fork 0
/
MooreToMealy.c
94 lines (84 loc) · 4.85 KB
/
MooreToMealy.c
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
#include<stdio.h>
#include<string.h>
int ninputs;
int mooretomealy(int,int);
int mealy(char,int,int); //function declaration
int dfa[10][10][2],conti,l=0,len,output[10];
char c[10], string[10]; int final[10];
int main()
{
int nstates;
int f[10];
int i,j,s=0,final=0;
printf("Enter the number of states that your DFA consist of :\n"); //Entering No. of states
scanf("%d",&nstates);
printf("Enter the number of input symbol that DFA have :\n"); //Entering the no. of symbols in Alphabet Set
scanf("%d",&ninputs);
printf("\nEnter input symbols:\t"); //The symbols are prementioned as {a,b,c,d,e,f,g,h,i,j}
c[0]='a'; c[1]='b'; c[2]='c'; c[3]='d'; c[4]='e'; c[5]='f'; c[6]='g'; c[7]='h'; c[8]='i'; c[9]='j';
for(i=0; i<ninputs; i++) //printing the no. of input symbols if 3, then {a,b,c}
{
printf("\n\n %d input\t %c", i+1,c[i]);
}
printf("\n-----------------------------------------------------------------------");
printf("\n\nDefine Transition Rule as (Initial State, Input Symbol ) = Final State\n"); //Defined transition rules as (q0,a)=q1 etc.
printf("\n∂: Q x ∑ -> Q\n");
printf("\nX: Q x ∑ -> O\n");
for(i=0; i<ninputs; i++)
{
for(j=0; j<nstates; j++)
{
printf("\n(q%d , %c ) = q",j,c[i]);
scanf("%d",&dfa[i][j][0]);
}
}
for(j=0;j<nstates;j++)
{
printf("Enter the output value for state q%d:",j);
scanf("%d",&output[j]);
}
mooretomealy(nstates,ninputs);
printf("States\t");
for(i=0;i<ninputs;i++)
{
printf("%c\t",c[i]);
}
for(i=0;i<nstates;i++)
{
printf("\nq%d\t",i);
for(j=0;j<ninputs;j++)
{
printf("q%d,%d\t",dfa[j][i][0],dfa[j][i][1]);
}
}
printf("\nEnter the string to check output:");
scanf("%s",string);
len=strlen(string);
mealy(string[0],0,len);
}
int mooretomealy(int nstates,int ninputs)
{
int i,j,x;
for(i=0;i<ninputs;i++)
{
for(j=0;j<nstates;j++)
{
x=dfa[i][j][0];
dfa[i][j][1]=output[x];
}
}
return 0;
}
int mealy(char ch, int present_state, int len)
{
int i=0;
if(len==0)
return 0;
while(ch!=c[i])
{ i++;
}
printf("%d",dfa[i][present_state][1]);
l+=1;
mealy(string[l],dfa[i][present_state][0],len-1);
return 0;
}