NFA转换成DFA

liangkaiyu 2009-03-24 10:45:11
老师布置的题目
没底
算法书本上有介绍一点
但数据结构...
难啊
以前学数据结构的时候关于图的问题没有编过一个程序
汗!
行过路过的
给点建议
...全文
1276 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
nicosile5258 2011-04-28
  • 打赏
  • 举报
回复
将(#include "stdafx.h" )改为(#include<stdio.h>应该就没错了
nicosile5258 2011-04-28
  • 打赏
  • 举报
回复
#include "stdafx.h"
#include <vector>
#include <string>
#include <queue>
#include <iostream>

using namespace std;

struct Transform
{
friend class FA;
char state1;
char letter;
char state2;
bool operator!=(char ch);
friend istream& operator>>(istream& in,Transform& t);
};
struct ChangeTable
{
string state;
int flag;
vector<string> changestate;
};
bool Transform::operator!=(char ch)
{
return state1!=ch||letter!=ch||state2!=ch;
}
namespace std
{
istream& operator>>(istream& in,Transform& t)
{
return in>>t.state1>>t.letter>>t.state2;
}
}
class FA
{
private:
vector<char> state; //状态集
vector<char> table; //输入字母表
vector<Transform> fun; //状态变迁函数
char q0; //初态
vector<char> Z; //终态集
string GetChangeState(char state1,char table);
void RemoveRepeat(string &str); //消除重复元素
private:
vector<ChangeTable> changetable;
bool IsInChangeTable(string str);
int GetIndexofChangeTable(); //找出第一个flag为0的索引
int GetIndexofChangeTable(string str); //找出str 在表中的索引
void GetChangeTable();
void OutputChangeTable();
public:
// FA(){}
void Deterministic(FA &dfa);
void input();
void output();

};
void FA::RemoveRepeat(string &str)
{
string mid;
string::size_type idx;
string::iterator pos;
for(pos=str.begin();pos!=str.end();++pos)
{
idx=mid.find(*pos);
if(idx==string::npos)
mid+=*pos;
}
str=mid;
}
bool FA::IsInChangeTable(string str)
{
vector<ChangeTable>::iterator pos;
for(pos=changetable.begin();pos!=changetable.end();++pos)
if((*pos).state==str)
return 1;
return 0;
}
int FA::GetIndexofChangeTable()
{
int index;
for(index=0;index<changetable.size();++index)
if(changetable[index].flag==0)
return index;
return -1;
}
int FA::GetIndexofChangeTable(string str)
{
int index;
for(index=0;index<changetable.size();++index)
if(changetable[index].state==str)
return index;
return -1;
}
string FA::GetChangeState(char state1,char table)
{
string str;
vector<Transform>::iterator pos;
for(pos=fun.begin();pos!=fun.end();++pos)
if((*pos).state1==state1&&(*pos).letter==table)
str+=(*pos).state2;
RemoveRepeat(str);
return str;
}
void FA::input()
{
char ch;
Transform tran;
cout<<"input the set of state,ended with '#'"<<endl;
cin>>ch;
while(ch!='#')
{
state.push_back(ch);
cin>>ch;
}
cout<<"input the set of input's table,ended with '#'"<<endl;
cin>>ch;
while(ch!='#')
{
table.push_back(ch);
cin>>ch;
}

cout<<"input the transform of function,formation is state1 letter state1 "<<endl;
cout<<"ended with '# # #'"<<endl;
cin>>tran;
while(tran!='#')
{
fun.push_back(tran);
cin>>tran;
}
cout<<"input the state of Initlization table"<<endl;
cin>>q0;

cout<<"input the set of termination,ended with '#'"<<endl;
cin>>ch;
while(ch!='#')
{
Z.push_back(ch);
cin>>ch;
}
}
void FA::GetChangeTable()
{
ChangeTable ct,midct;
string str;
string mid;
queue<string> q;
vector<char>::iterator pos;
string::iterator p;
ct.state=string(1,q0); //从初始状态开始
ct.flag=0;
changetable.push_back(ct);
int index=GetIndexofChangeTable();
while(index!=-1)
{
changetable[index].flag=1;
str=changetable[index].state; //弹出状态

for(pos=table.begin();pos!=table.end();++pos) //每种输入状态
{
mid.erase();
for(p=str.begin();p!=str.end();++p) //每个状态的每个字符
{
mid+=GetChangeState(*p,*pos);
RemoveRepeat(mid);
}

changetable[index].changestate.push_back(mid);

if(!mid.empty()&&!IsInChangeTable(mid))
{
ct.state=mid;
ct.flag=0;
changetable.push_back(ct);
}
}

index=GetIndexofChangeTable();
}
}
void FA::OutputChangeTable()
{
vector<ChangeTable>::iterator pos;
for(pos=changetable.begin();pos!=changetable.end();++pos)
{ cout<<"{"<<(*pos).state<<"} ";
for(int i=0;i<(*pos).changestate.size();i++)
cout<<"{"<<(*pos).changestate[i]<<"} ";
cout<<endl;
}
}
void FA::Deterministic(FA &dfa)
{
GetChangeTable();
//OutputChangeTable(); //输出中间状态转换表
dfa.table=table;
int size=0;
while(size<changetable.size()) //求状态集
{
dfa.state.push_back('A'+size);
size++;
}
dfa.q0='A'; //求初态
Transform tran;
vector<string>::iterator pos;
vector<char>::iterator p;
for(int i=0;i<changetable.size();++i)
{
tran.state1='A'+i;
for(p=table.begin(),pos=changetable[i].changestate.begin();pos!=changetable[i].changestate.end();++pos,++p)
{
tran.letter=*p;
int index=GetIndexofChangeTable(*pos);
tran.state2='A'+index;
if(index!=-1)
dfa.fun.push_back(tran);
}
//下面来求终态集
string mid;
string str=changetable[i].state;
for(p=Z.begin();p!=Z.end();++p)
{
mid.erase();
mid+=*p;
}
int idx=str.find(mid);
if(idx!=string::npos)
dfa.Z.push_back('A'+i);
}


}
void FA::output()
{
vector<char>::iterator pos;
cout<<"M={Vn,Vt,F,q0,Z},其中:"<<endl;
cout<<"Vn={";
for(pos=state.begin();pos!=state.end()-1;++pos)
cout<<*pos<<",";
cout<<*pos<<"}"<<endl;
cout<<"Vt={";
for(pos=table.begin();pos!=table.end()-1;++pos)
cout<<*pos<<",";
cout<<*pos<<"}"<<endl;

cout<<"F is:"<<endl;
for(std::vector<Transform>::iterator p=fun.begin();p!=fun.end();++p)
cout<<"f("<<(*p).state1<<","<<(*p).letter<<")="<<(*p).state2<<endl;

cout<<"q0="<<q0<<endl;

cout<<"Z={";
for(pos=Z.begin();pos!=Z.end()-1;++pos)
cout<<*pos<<",";
cout<<*pos<<"}"<<endl;

}
int main(int argc, char* argv[])
{
FA nfa,dfa;
nfa.input();
nfa.output();
nfa.Deterministic(dfa);
cout<<"转换成DFA为:"<<endl;
dfa.output();
return 0;
}
rui18 2010-05-27
  • 打赏
  • 举报
回复
···忘了,可以的话发个过来吧,415642063@qq.com
rui18 2010-05-27
  • 打赏
  • 举报
回复
8楼的老大,也给我发个啊,俺在这个问题纠结了好久,救我脱离苦海吧~~
qq675927952 2009-04-17
  • 打赏
  • 举报
回复
邮件已发,请查收
qq675927952 2009-04-17
  • 打赏
  • 举报
回复
我有源程序要的话可以发给你,675927952@qq.com
chyjp2008 2009-04-16
  • 打赏
  • 举报
回复
Ding~
liangkaiyu 2009-03-29
  • 打赏
  • 举报
回复
读入一个转换函数
以”开始状态 输入符号 到达状态“这种方式读入
既有数字也有字符
应该怎样做?
还是有其他更好的方法读入转换函数?

路过的给点意见
waiting~~
  • 打赏
  • 举报
回复
http://blog.sina.com.cn/s/blog_4b26f7af01000b8p.html
http://hi.baidu.com/%B1%E0%B3%CC%D6%AE%C3%C0/blog/item/98cb921e0628d0f21bd57620.html
NFA转DFA代码
jieao111 2009-03-24
  • 打赏
  • 举报
回复
自己理解了,就写程序
liangkaiyu 2009-03-24
  • 打赏
  • 举报
回复
期待更多的建议......
wxgiter 2009-03-24
  • 打赏
  • 举报
回复
编译原理时做过题目,不过没用程序实现。。。。
jn989 2009-03-24
  • 打赏
  • 举报
回复
算法用“龙书”上的算法就可以了啊,好像叫什么闭包算法
数据结构嘛,用图的邻接表吧,记得当时我做的时候就是这个结构,很好用的:
a e
0 -->1 -->2
b
0 -->3
就可以这样定义,最好用stl的vector和list
vector定义一个连通图,其中每个元素为一个以下标为开始节点的链表,链表节点为一个二元组(边上的字母和后续节点编号)
上面的图就定义为:
vector vect(4);
vect[0]:<a,1>,<b,3>
vect[1]:<e,2>
vect[2]:null
vect[3]:null

69,369

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧