资源简介
采用c++语言编程,从而实现NFA与DFA之间的转换,代码简单
代码片段和文件信息
#include
#include
#define MAXS 100
using namespace std;
string NODE; //结点集合
string CHANGE; //终结符集合
int N; //NFA边数
struct edge {
string first;
string change;
string last;
};
struct chan {
string ltab;
string jihe[MAXS];
};
void kong(int a)
{
int i;
for (i = 0; i {
cout << ‘ ‘;
}
}
//排序
void paixu(string &a)
{
int i j;
char b;
for (j = 0; j for (i = 0; i if (NODE.find(a[i])>NODE.find(a[i + 1]))
{
b = a[i];
a[i] = a[i + 1];
a[i + 1] = b;
}
}
void eclouse(char c string &he edge b[])
{
int k;
for (k = 0; k {
if (c == b[k].first[0])
if (b[k].change == “*“)
{
if (he.find(b[k].last)>he.length())
he += b[k].last;
eclouse(b[k].last[0] he b);
}
}
}
void move(chan &he int m edge b[])
{
int i j k l;
k = he.ltab.length();
l = he.jihe[m].length();
for (i = 0; i for (j = 0; j if ((CHANGE[m] == b[j].change[0]) && (he.ltab[i] == b[j].first[0]))
if (he.jihe[m].find(b[j].last[0])>he.jihe[m].length())
he.jihe[m] += b[j].last[0];
for (i = 0; i for (j = 0; j if ((CHANGE[m] == b[j].change[0]) && (he.jihe[m][i] == b[j].first[0]))
if (he.jihe[m].find(b[j].last[0])>he.jihe[m].length())
he.jihe[m] += b[j].last[0];
}
//输出
void outputfa(int len int h chan *t)
{
int i j m;
cout << “ I “;
for (i = 0; i cout << ‘I‘ << CHANGE[i] << “ “;
cout << endl << “-------------------------“ << endl;
for (i = 0; i {
cout << ‘ ‘ << t[i].ltab;
m = t[i].ltab.length();
for (j = 0; j {
kong(8 - m);
m = t[i].jihe[j].length();
cout << t[i].jihe[j];
}
cout << endl;
}
}
void main()
{
edge *b = new edge[MAXS];
int i j k m n h x y len;
bool flag;
string jh[MAXS] endnode ednode sta;
cout << “请输入NFA各边信息(起点 条件[空为*] 终点),以#结束:“ << endl;
for (i = 0; i {
cin >> b[i].first;
if (b[i].first == “#“) break;
cin >> b[i].change >> b[i].last;
}
N = i;
/*for(j=0;j cout< for (i = 0; i {
if (NODE.find(b[i].first)>NODE.length())
NODE += b[i].first;
if (NODE.find(b[i].last)>NODE.length())
NODE += b[i].last;
if ((CHANGE.find(b[i].change)>CHANGE.length()) && (b[i].change != “*“))
CHANGE += b[i].change;
}
len = CHANGE.length();
cout << “结点中属于终态的是:“ << endl;
cin >> endnode;
for (i = 0; i if (NODE.find(endnode[i])>NODE.length())
{
cout << “所输终态不在集合中,错误!“ << endl;
return;
}
//cout<<“endnode=“< chan *t = new chan[MAXS];
t[0].ltab = b[0].first;
h = 1;
eclouse(b[0].first[0] t[0].ltab b); //求e-clouse
//cout< for (i = 0; i {
for (j = 0; j for (m = 0; m
评论
共有 条评论