To simulate deterministic finite automata, I defined a data file format that can
ID: 3858756 • Letter: T
Question
To simulate deterministic finite automata, I defined a data file format that can represent these. Here it is: Line 1 - a comment line, allow for 100 characters as in char comment[100] to read it into. Line 2 - a string to represent the alphabet used by the automaton. For example "01" or "abc" no quotes. Line 3 - an integer value for the number of states in the automaton. Lines 4 - 3+n (where n = the value in line 2) shows the delta mapping. For each state, and for each possible input, show what state that input triggers. Thus if there were 3 states (1 2 3) and state 1 on 0 went back to state 1 while 1 sent it to state 2, then the line for state 1 would read 1 2. After this is a line for the start state (like 1 for state 1).
link to input file http://cs2.uco.edu/~stockwel/pages/endba.dfa
After that we need to identify the accepting states. Have a line such as
na s1 s2 s3...
where na tells how many accepting states you have, and s1 s2 s3... is a list of their numbers. Finally, have a list of strings in your alphabet which the machine will either accept or reject.
Here is a link to the input file endba.dfa
Write C++ code
that can run against such an input file and simulate the DFA
action, printing each string and either accepting or rejecting
it. In this case, the language of acceptance is all of the strings
of a's and b's that actually end in ba. Here is what your output should
look like for the first 4 lines of input:
ababaaba --> ACCEPT
a --> REJECT
b --> REJECT
aba --> ACCEPT
Explanation / Answer
Answer:
#include<iostream.h>
#include<math.h>
void main()
{
int i=0,f=0,s=0;
char value[80];
printf("Enter input Input terminated by $ symbol");
scanf("%s",value);
while(value[i]!='$' && f==0)
{
switch(s)
{
case 0:
if(value[i] >='0' && value[i] <='9')
{
s=1;
i++;
}
else
f=1;
break;
case 1:
if(value[i] >='0' && value[i] <='9')
i++;
elseif(value[i]=='.')
{
s=2;
i++;
}
else
f=1;
break;
case 2:
if(value[i] >='0' && value[i] <='9')
{
s=3;
i++;
}
else
f=1;
break;
case 3:
if(value[i] >='0' && value[i] <='9')
i++;
elseif(value[i]=='E' || 'e')
{
s=4;
i++;
}
else
f=1;
break;
case 4:
if(value[i] == '+' || value[i] == '-')
{
s=5;
i++;
}
else
f=1;
break;
case 5: if(value[i] >='0' && value[i] <='9')
i++;
else
f=1;
break;
}
}
if(f==1)
cout<<"Invalid Input ";
else
cout<<"Input is Valid ";
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.