#include<string>
#include<windows.h>
using namespace std;
void gotoxy(int x, int y)
{
CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
HANDLE hConsoleOut;
hConsoleOut = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfo(hConsoleOut, &csbiInfo);
csbiInfo.dwCursorPosition.X = x;
csbiInfo.dwCursorPosition.Y = y;
SetConsoleCursorPosition(hConsoleOut, csbiInfo.dwCursorPosition);
}
class DFA {
private:
int array[10][10] = { {1, 0}, {3, 2}, {4, 3}, {3, 2, 4} };
int state = 1;
int start = 1;
int end = 3;
int count = 1;
int i = 0;
string s;
public:
DFA(string s) {
this->s = s;
handle();
}
int move(char ch) {
string s2(s, count++);
if ((state == 4&&ch!=‘#‘)) {
cout << "不接受" << endl;
exit(0);
}else if(ch == ‘#‘) {
gotoxy(4, count + 1);
cout << this->state;
gotoxy(11, count + 1);
cout << ch;
gotoxy(22, count + 1);
cout << "ok 接受" << endl;
return 1;
}
if (ch == ‘a‘) {
gotoxy(4, count + 1);
cout << this->state;
gotoxy(11, count + 1);
cout << ch;
gotoxy(15, count + 1);
cout << s2;
state = array[state][0];
gotoxy(22, count + 1);
cout << state << endl;
return 0;
}else if (ch == ‘b‘ && state == 3)
{
gotoxy(4, count + 1);
cout << this->state;
gotoxy(11, count + 1);
cout << ch;
gotoxy(15, count + 1);
cout << s2;
state = array[state][2];
gotoxy(22, count + 1);
cout << state << endl;
return 0;
}else if (ch == ‘b‘ && state == 1) {
gotoxy(4, count + 1);
cout << this->state;
gotoxy(11, count + 1);
cout << ch;
state = array[state][1];
gotoxy(15, count + 1);
cout << s2;
gotoxy(22, count + 1);
cout << state << endl;
return 0;
}else if (ch == ‘c‘) {
gotoxy(4, count + 1);
cout << this->state;
gotoxy(11, count + 1);
cout << ch;
gotoxy(15, count + 1);
cout << s2;
state = array[state][1];
gotoxy(22, count + 1);
cout << state << endl;
return 0;
}else if (ch == ‘d‘) {
gotoxy(4, count + 1);
cout << this->state;
gotoxy(11, count + 1);
cout << ch;
gotoxy(15, count + 1);
cout << s2;
state = array[state][0];
gotoxy(22, count + 1);
cout << state << endl;
return 0;
}
else {
cout << "不接受" << endl;
exit(0);
}
return 0;
}
void handle() {
state = start;
for (int i = 0; i < s.length(); i++) {
move(s.at(i));
}
}
};
int main() {
string input;
cout << "请输入一个字符串:" << endl;
cin >> input;
cout << "| state | ch | 剩余 | 变换 | 备注 |" << endl;
DFA dfa(input);
return 0;
}
原文:https://www.cnblogs.com/Hukai/p/14760374.html