有限自动机的构造与识别

#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 << "输入有限状态集S:" << endl;
cin >> States;
cout << "输入字符集A:" << endl;
cin >> Alphabets;
cout << "输入状态转换式(格式为:状态-输入字符-下一状态,输入#结束):" << endl;
cout << "例如:1a1 \n 1a0 \n 2a1 \n #" << 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 << "输入初态:" << endl;
cin >> startStates;
cout << "输入终态:" << 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;
}

时间: 2024-08-14 21:06:13

有限自动机的构造与识别的相关文章

实验三有限自动机的构造与识别

#include<stdio.h> #include <ctype.h> #define ok 1 #define error 0 #define MAXREGLUARLONG 40 #define MAXSTATELONG 40 #define MAXCAHRSLONG 40 typedef int state; int iCurrentState=0; //初态以1开始 int iPreState=0; int iLastForkState=0; int iForkState=

第三次实验--有限自动机的构造与识别

实验目的:通过正规式构造NFA(有穷自动机)方法. 所运用到的语法规则: 语句                          规则 1.R=? /R=ε            X-----ε----->Y 2.R=a                     X-----a----->Y 3.R=r1r2                A-----r1----->B-----r2----->C 4.R=r1|r2                5.R=r1*          

实验三 有限自动机的构造与识别

一.实验目标 1.掌握有穷状态自动机的概念: 2.掌握有穷状态自动机的存储及表示方法: 3.掌握有穷状态自动机与正则式之间的关系. 二.实验要求 1.输入正规式: 2.构造该正规式的有穷状态自动机: 3. 以五元组形式输出. 三.算法 参见教材的转换规则. 练习: 2  (a|b)*abb 2  l(l|d)* 2  1(1010*|1(010)*1)*0 四.完成算法设计.编码和调试工作,完成实验报告. #include<string.h> #include<stdio.h> #

1118有限自动机的构造与识别

1 #include<string.h> 2 #include<stdio.h> 3 #include<stdlib.h> 4 int main() 5 { 6 char p[30][30]; 7 char q[30][30]; 8 int line=0; 9 int n; 10 int i,j; 11 int count=0; 12 int k,t=0; 13 int flag=0; 14 int l,m=0; 15 char VN[30]={'\0'}; 16 ch

1210有限自动机的构造与识别

#include<string.h>#include<stdio.h>#include<stdlib.h>int main(){char p[30][30];char q[30][30];int line=0;int n;int i,j;int count=0;int k,t=0;int flag=0;int l,m=0;char VN[30]={'\0'};char VT[30]={'\0'};printf("规则数:");scanf("

1118实验三有限自动机的构造与识别

#include<string.h>#include<stdio.h>#include<stdlib.h>int main(){ char p[30][30]; char q[30][30]; int line=0; int n; int i,j; int count=0; int k,t=0; int flag=0; int l,m=0; char VN[30]={'\0'}; char VT[30]={'\0'}; printf("请输入规则个数:&quo

有限自动机的构造和识别

#include<string.h>#include<stdio.h>#include<stdlib.h>int main(){    char p[30][30];//存放文法    char q[30][30];    int line=0;    int n;    int i,j;    int count=0;    int k,t=0;    int flag=0;    int l,m=0;    char VN[30]={'\0'};//存放非终结符号 

有限自动机的构造与识别3

#include<stdio.h> #define N 30 char str[N]={NULL}; int st[N]; int t=0,k=0,a,t1=0; struct node//定义一个队列 { char data; struct node * next; }; typedef struct node QueueNode; struct node2//定义一个链队列 { QueueNode *front; QueueNode *rear; }; typedef struct nod

1118 实验三 有限自动机的构造与识别

#include<stdio.h>char gra[80];char ch;int m=0,p=0;struct{ int A; char R[10]; int B;}NFA;main(){ char gra[10]; printf("---------------正规文法转换成有穷自动机------------------\n"); printf("请输入正规文法R:"); do{ ch=getchar(); gra[p++]=ch; }while(c