This repository was archived by the owner on Jun 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy path7-mealy-to-moore.c
More file actions
199 lines (181 loc) · 6.5 KB
/
7-mealy-to-moore.c
File metadata and controls
199 lines (181 loc) · 6.5 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// Author : Anindya Kundu - 510817020
// This program stores and prints the State Table of a
// Mealy Machine. It takes the "number of states", the
// "inputs", the "transitional functions" and the res-
// pective "outputs" for each function as input.
// The Table is a 2D array of rows = number of states
// and columns = 2 * number of inputs.
// It is assumed that the states are serially denoted
// from 'A', hence present state isn't stored. It is
// also assumed that input syntax is correct semanti-
// cally and syntactically as follows:
// * Enter Number of States: *integer*
// * Enter Inputs: #,#,...,# (# : lower alpha / digit)
// * Enter Transitional Functions:
// * δ(%, $) → *uppercase alphabet*
// * Output: *character*
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#define clear() printf("\033[H\033[J")
typedef struct mealy{
int staNo, subNo;
} cell;
char *getInputs(int *);
void inputTransFuncs(char *, char *, int, int);
void printStateTableMealy(char *, char *, int, int);
void printStateTableMoore(char *, char *, int, int);
char **toMoore(char *, char *, int, int, int *);
int main(int argc, int *argv[]){
clear(); int r, c, len;
printf("Enter Number of States: ");
scanf("%d", &r);
printf("Enter Inputs: ");
char *ip = getInputs(&c);
printf("Enter Transitional Functions:\n");
char ar[r][2 * c]; inputTransFuncs(&ar[0][0], ip, r, c);
printStateTableMealy(&ar[0][0], ip, r, c);
char **ot = toMoore(&ar[0][0], ip, r, 2 * c, &len);
char tb[len][c + 1];
for(int i = 0; i < len; i++)
for(int j = 0; j < c + 1; j++)
tb[i][j] = ot[i][j];
printStateTableMoore(&tb[0][0], ip, len, c + 1);
return 0;
}
char *getInputs(int *n){
char s[64], t[64]; *n = 0;
scanf("%s", s);
for(int i = 0; s[i] != '\0'; i++)
if(islower(s[i]) || isdigit(s[i])) t[(*n)++] = s[i];
char *ip = (char *) malloc(sizeof(char) * (*n));
for(int i = 0; i < (*n); i++) ip[i] = t[i];
return ip;
}
void inputTransFuncs(char *ar, char *ip, int r, int c){
for(int i = 0; i < r; i++){
for(int j = 0; j < c; j++){
printf(" δ(%c, %c) → ", ('A' + i), *(ip + j));
scanf(" %c", (ar + (2 * c * i) + ((2 * j) + 0)));
printf(" Output: ");
scanf(" %c", (ar + (2 * c * i) + ((2 * j) + 1)));
printf("\n");
}
}
}
void printStateTableMealy(char *ar, char *ip, int r, int c){
printf("\nState Table for Mealy:\n----------------------\n PS\t");
for(int i = 1; i < c; i++) printf("\t");
printf(" NS / OP\n--------");
for(int i = 0; i < 2 * c; i++) printf("--------"); printf("\n");
for(int i = 0; i < c; i++) printf("\t|\t%c", ip[i]);
printf("\n--------");
for(int i = 0; i < 2 * c; i++) printf("--------"); printf("\n");
for(int i = 0; i < r; i++){
printf(" %c\t", ('A' + i));
for(int j = 0; j < c; j++){
printf("| %c\t", *(ar + (2 * c * i) + ((2 * j) + 0)));
printf("| %c\t", *(ar + (2 * c * i) + ((2 * j) + 1)));
}
printf("\n");
} printf("\n");
}
void printStateTableMoore(char *ar, char *ip, int r, int c){
printf("\nState Table for Moore:\n----------------------\n PS\t");
if(c % 2 == 0)
{ for(int i = 1; i < c / 2; i++) printf("\t"); printf(" "); }
else
{ for(int i = 1; i < (c - 1) / 2; i++) printf("\t"); printf(" "); }
printf("N S\n--------");
for(int i = 0; i < c; i++) printf("--------"); printf("\n");
printf("\t"); for(int i = 0; i < c - 1; i++) printf("| %c\t", ip[i]);
printf("\n--------");
for(int i = 0; i < c; i++) printf("--------"); printf("\n");
for(int i = 0; i < r; i++){
printf(" %c\t", ('A' + i));
for(int j = 0; j < c - 1; j++)
printf("| %c\t", *(ar + (c * i) + j));
printf("| %c\n", *(ar + (c * i) + (c - 1)));
} printf("\n");
}
char **toMoore(char *ar, char *ip, int r, int c, int *len){
int ipn = strlen(ip), sts[r][ipn], k = 0;
for(int i = 0; i < r; i++)
for(int j = 0; j < ipn; j++)
sts[i][j] = 0;
for(int i = 0; i < r; i++)
for(int j = 0; j < c; j += 2){
int st = *(ar + (c * i) + j + 0) - 'A';
int op = *(ar + (c * i) + j + 1) - ip[0];
sts[st][op] = 1;
}
for(int i = 0; i < r; i++)
for(int j = 0; j < ipn; j++)
if(sts[i][j] == 1) k++;
cell *intStates[k][(c / 2) + 1];
char intOutput[k][c / 2]; k = 0;
for(int i = 0; i < r; i++)
for(int j = 0; j < ipn; j++){
if(sts[i][j] == 1){
cell *temp = (cell *) malloc(sizeof(cell));
temp -> staNo = i;
temp -> subNo = j;
intStates[k++][0] = temp;
}
}
for(int i = 0; i < k; i++){
cell *temp = intStates[i][0];
int curr = temp -> staNo;
for(int j = 0; j < c; j += 2){
cell *new = (cell *) malloc(sizeof(cell));
new -> staNo = *(ar + (c * curr) + j + 0) - 'A';
new -> subNo = *(ar + (c * curr) + j + 1) - ip[0];
intStates[i][(j / 2) + 1] = new;
intOutput[i][j / 2] = new -> subNo;
}
}
printf("\nState Table for Intermediate:\n-----------------------------\n");
for(int i = 0; i < k; i++){
cell *temp = intStates[i][0];
printf("|%c%d|\t", (temp -> staNo) + 'A', temp -> subNo);
for(int j = 1; j <= c / 2; j++){
temp = intStates[i][j];
printf("|%c%d|\t", (temp -> staNo) + 'A', temp -> subNo);
printf("| %d |\t", intOutput[i][j - 1]);
} printf("\n");
} printf("\n");
char **final = (char **) malloc(sizeof(char *) * k);
for(int i = 0; i < k; i++)
final[i] = (char *) malloc(sizeof(char) * ((c / 2) + 1));
for(int i = 0; i < k; i++){
for(int j = 0; j < c / 2; j++){
cell *temp = intStates[i][j + 1];
int st = temp -> staNo;
int op = temp -> subNo, p, l = 0;
for(int i = 0; i < op; i++)
if(sts[st][i] == 1) l++;
for(int i = 0; i < k; i++){
cell *pos = intStates[i][0];
if(pos -> staNo == st){ p = i; break; }
} final[i][j] = p + l;
}
cell *now = intStates[i][0];
final[i][c / 2] = now -> subNo;
} *len = k;
if(final[0][c / 2] == 1){
char **nwfin = (char **) malloc(sizeof(char *) * (k + 1));
for(int i = 0; i < k + 1; i++)
nwfin[i] = (char *) malloc(sizeof(char) * ((c / 2) + 1));
for(int i = 1; i < k + 1; i++){
for(int j = 0; j < c / 2; j++)
nwfin[i][j] = final[i - 1][j] + 'B';
nwfin[i][c / 2] = final[i - 1][c / 2] + ip[0];
} *len = k + 1;
for(int j = 0; j < c / 2; j++)
nwfin[0][j] = nwfin[1][j];
nwfin[0][c / 2] = ip[0];
return nwfin;
}
return final;
}