This repository has been 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
/
5-mealy-machine.c
82 lines (71 loc) · 2.69 KB
/
5-mealy-machine.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
// 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>
#define clear() printf("\033[H\033[J")
char *getInputs(int *);
void inputTransFuncs(char *, char *, int, int);
void printStateTable(char *, char *, int, int);
int main(int argc, int *argv[]){
clear(); int r, c;
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);
printStateTable(&ar[0][0], ip, r, c);
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 printStateTable(char *ar, char *ip, int r, int c){
printf("\nState Table:\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");
}