1211 Finite automaton

Source: Internet
Author: User

#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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.