#include "iostream.h"
#include "string.h"
#include "fstream.h"
#define NULL 0
Class Transtile
{
Public
Char current;
Char next;
char input;
Transtile (char C,char I,char Ne) {
current = C;
Next = Ne;
input = I;
}
};
Class DFA
{
Public
string states;
Char startstates;
String finalstates;
String alphabets;
Vector <TransTile> Tile;
DFA () {
Init ();
}
void Init ()
{
cout << "input finite state set S:" << Endl;
CIN >> states;
cout << "input character set a:" << Endl;
CIN >> alphabets;
cout << "Input state Transition (format: status-input character-next state, enter # end):" << Endl;
cout << "For example: 1a1 \ 1a0 \ 2a1 \ \" << Endl;
int h = 0;
while (Cin>>input) {
Transtile Transval (input[0], input[1], input[2]);
Tile.push_back (Transval);
//}
while (true) {
Char input[4];
cin>>input;
if (strcmp (Input, "#") ==0)
Break
Transtile Transval (input[0],input[1],input[2]);
Tile.push_back (Transval);
}
cout << "Input initial state:" << Endl;
Cin >> Startstates;
cout << "Input end state:" << Endl;
Cin >> Finalstates;
}
Char Move (char P,char I) {
for (int i = 0; i < tile.size (); i++) {
if (tile[i].current = = P&&tile[i].input = = i) {
return tile[i].next;
}
}
Return ' E ';
}
void recognition () {
String str;
cout << "Please input string:" << Endl;
CIN >> str;
int i = 0;
char current = startstates;
while (I < str.length ()) {
Current = Move (current, str[i]);
if (current = = ' E ') {
Break
}
i++;
}
if (finalstates.find (current) = Finalstates.npos) {
cout << "ERROR! "<< Endl;
}
Else
{
cout << "error! "<< Endl;
}
}
};
int main () {
DFA DFA;
BOOL tag;
while (1) {
cout<< "continue to ' 1 ', else to ' 0 ':" <<endl;
cin>>tag;
if (tag) {
Dfa.recognition ();
}else
Break
}
return 0;
}
1211 Finite automaton