I understand the concept but my teacher is horrible and she never even practiced
ID: 3548722 • Letter: I
Question
I understand the concept but my teacher is horrible and she never even practiced/gave us an example of how to implement a FSM in c++ neither are there any in the textbook. Here is the question:
Write a C++ program to implement a finite state machine which accepts a series of input strings typed by the user and determines whether or not each input contains the substring "1101".
Note the input alphabet for this assignment is I ={0,1}.
You must make a decision in the design of your program how to handle characters entered by a user that do not belong in the input alphabet, Document your decision using comments in your source code.
HERE IS THE FSM DIAGRAM
HERE IS THE FSM TABLE(void the strings of 1s and 0s at the top those were just tests)
Explanation / Answer
please rate - thanks
#include <iostream>
using namespace std;
string input();
int nextState(int,char);
int main()
{int state=0,i,found;
string s;
s=input();
while(s.length()>0)
{cout<<"output for input string "<<s<<" is ";
found=0;
for(i=0;i<s.length();i++)
{state=nextState(state,s[i]);
if(state==4)
{found=1;
cout<<"1";
}
else
cout<<"0";
}
if(found==1)
cout<<" --- 1101 was found ";
else
cout<<" --- 1101 was not found ";
cout<<endl;
s=input();
}
system("pause");
return 0;
}
string input()
{cout<<"enter the input string (ctrl^z when finished): ";
string s;
cin>>s;
return s;
}
int nextState(int state,char c)
{switch(c)
{case '0':switch(state)
{case 0: return 0;
case 1: return 0;
case 2: return 3;
case 3: return 0;
case 4: return 0;
}
case '1': switch(state)
{case 0: return 1;
case 1: return 2;
case 2: return 0;
case 3: return 4;
case 4: return 0;
}
default: break; //invalid input was encountered and ignored
}
return state;
}