首页 > 编程语言 > 详细

基于C++DFA编程的实现

时间:2021-05-12 21:13:45      阅读:22      评论:0      收藏:0      [点我收藏+]

include

#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;

}

基于C++DFA编程的实现

原文:https://www.cnblogs.com/Hukai/p/14760374.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!