Friday 12 February 2016

Conversion of NFA to DFA

#include<iostream>
#include<string>
using namespace std;

string statear[40];

int find(string nift)
{
for(int i=0;i<40;i++)
{

      if(nift==statear[i])
   {  return 0;
  }
  }
  return 1;
}

int main()
{ int jk=2;
string stra[30];
string no0,no1;
statear[0]="a";
statear[1]="b";
statear[2]="c";
for(int e=0;e<6;e++)
{ cout<<"enter the state when ";

if(e%2==0)
  cout<<"0";
else
  cout<<"1";
  cout<<"is entered to";
  if(e==0||e==1)
  cout<<"a:  ";
  if(e==2||e==3)
  cout<<"b:  ";
  if(e==4||e==5)
  cout<<"c:  ";
  cin>>stra[e];
}
  cout<<"The NFA is"<<endl;
cout<<"    0   1"<<endl;
cout<<"a"<<"   "<<stra[0]<<"   "<<stra[1]<<endl;
cout<<"b"<<"   "<<stra[2]<<"   "<<stra[3]<<endl;
cout<<"c"<<"   "<<stra[4]<<"   "<<stra[5]<<endl;
cout<<"The equivalent DFA is"<<endl;
  cout<<    "           0            1"<<endl;
for(int pl=0;pl<3;pl++)
  {
if(pl==0)
{
  cout<<"a         ";
  cout<<stra[0]<<"       "<<stra[1]<<endl;

}
if(pl==1)
{
cout<<"b        ";
cout<<stra[2]<<"       "<<stra[3]<<endl;
}
if(pl==2)
{
  cout<<"c        ";

cout<<stra[4]<<"      "<<stra[5]<<endl;}
for(int bk=0;bk<13;bk++)

{

int l=find(stra[bk]);
if(l==1&&stra[bk]!="0")
{
jk++;

no0="";
no1="";

for(int j=0;j<3;j++)
{
statear[jk]=stra[bk];
char decide=stra[bk][j];
switch (decide)
{case 'a':

           if(stra[0]!="0") {
   no0.append(stra[0]);
 
 
 
      }
          if(stra[1]!="0")  no1.append(stra[1]);
            break;
            case 'b':
if(stra[2]!="0")   no0.append(stra[2]);
           if(stra[3]!="0") no1.append(stra[3]);
            break;
            case 'c':
if(stra[4]!="0")no0.append(stra[4]);
         if(stra[5]!="0")   no1.append(stra[5]);
            break;
}
}

string mr="0";
if(no0.length()==0)
no0="0";
if(no0.length()>=4)
no0="abc";
if(no1.length()==0)
no1="0";
if(no1.length()>=4)
no1="abc";
cout<<statear[jk]<<"      "<<no0<<"       "<<no1<<endl;
}

}
}

}




No comments:

Post a Comment