展开全部
#include "stdafx.h"
#include
#include
#include
#include
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 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 state; //状态集
vector table; //输入字母表
vector fun; //状态变迁函数
char q0; //初态
vector Z; //终态集
string GetChangeState(char state1,char table);
void RemoveRepeat(string &str); //消除重复元素
private:
vector changetable;
bool IsInChangeTable(string str);
int GetIndexofChangeTable(); //找出第一个flag为0的索引e5a48de588b662616964757a686964616f31333238653266
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::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
if(changetable[index].flag==0)
return index;
return -1;
}
int FA::GetIndexofChangeTable(string str)
{
int index;
for(index=0;index
if(changetable[index].state==str)
return index;
return -1;
}
string FA::GetChangeState(char state1,char table)
{
string str;
vector::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<
cin>>ch;
while(ch!='#')
{
state.push_back(ch);
cin>>ch;
}
cout<
cin>>ch;
while(ch!='#')
{
table.push_back(ch);
cin>>ch;
}
cout<
cout<
cin>>tran;
while(tran!='#')
{
fun.push_back(tran);
cin>>tran;
}
cout<
cin>>q0;
cout<
cin>>ch;
while(ch!='#')
{
Z.push_back(ch);
cin>>ch;
}
}
void FA::GetChangeTable()
{
ChangeTable ct,midct;
string str;
string mid;
queue q;
vector::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::iterator pos;
for(pos=changetable.begin();pos!=changetable.end();++pos)
{ cout<
for(int i=0;i
cout<
cout<
}
}
void FA::Deterministic(FA &dfa)
{
GetChangeTable();
//OutputChangeTable(); //输出中间状态转换表
dfa.table=table;
int size=0;
while(size
{
dfa.state.push_back('A'+size);
size++;
}
dfa.q0='A'; //求初态
Transform tran;
vector::iterator pos;
vector::iterator p;
for(int i=0;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::iterator pos;
cout<
cout<
for(pos=state.begin();pos!=state.end()-1;++pos)
cout<
cout<
cout<
for(pos=table.begin();pos!=table.end()-1;++pos)
cout<
cout<
cout<
for(std::vector::iterator p=fun.begin();p!=fun.end();++p)
cout<
cout<
cout<
for(pos=Z.begin();pos!=Z.end()-1;++pos)
cout<
cout<
}
int main(int argc, char* argv[])
{
FA nfa,dfa;
nfa.input();
nfa.output();
nfa.Deterministic(dfa);
cout<
dfa.output();
return 0;
}
来源:http://topic.csdn.net/t/20041024/23/3486893.html
本回答由网友推荐
已赞过
已踩过<
你对这个回答的评价是?
评论
收起