Ask Me Help Desk

Ask Me Help Desk (https://www.askmehelpdesk.com/forum.php)
-   C (https://www.askmehelpdesk.com/forumdisplay.php?f=438)
-   -   C++ code to convert nfa to dfa (https://www.askmehelpdesk.com/showthread.php?t=305129)

  • Jan 18, 2009, 01:25 AM
    yesmahaa
    C++ code to convert nfa to dfa
    I am looking for a c++ code to convert nfa to dfa and minimizing dfa..
    Please if you have it mail me.
    [email protected]
  • Mar 28, 2010, 04:46 AM
    lukymoon
    I need it :( can help me pl z
  • Jun 20, 2012, 05:44 PM
    midophone
    Hello, I have a home work in converting this following code of DFA to an NFA ,can any body help me?
    --------------------------------------------------------------------------------------------
    this program reads from a file called "dfa" contains:
    q0 q1 q2
    0 1
    q0
    q1
    q1 q0
    q1 q2
    q2 q2
    ---------------------------------------------------------------------------------------------
    #include<iostream.h>
    #include<fstream.h>
    #include<stdio.h>
    #include<string.h>


    struct stt
    {
    char s[20];
    }st;
    stt a[50],del[50],seg[50],fs[50],tt[25][25];
    int position(char [],stt [],int );
    int main()
    {

    int delc,segc,fsc,ttc;
    char q0[20];
    int I=0,c=0,t=0;
    char buf[50],bufer[100],path[20];
    puts("Enter the File Path with its name");
    gets(path);
    fstream f;
    f.open(path,ios::in);
    while(f.eof!=NULL)
    {
    f.getline(bufer,100,'\n');
    if(strcmp(bufer,"")==0) break;
    I=0;int ta=0;
    for(int x=0;bufer[x]!='\0';x++)
    {
    if (bufer[x]!=' ')
    {
    buf[ta]=bufer[x];
    ta=ta++;
    }
    if ((bufer[x]==' ') || (bufer[x+1]=='\0'))
    {
    buf[ta]='\0';
    strcpy(a[i].s,buf);
    i++;
    strcpy(buf,"");
    ta=0;
    }
    }
    if (c==0)
    {
    for(int k=0;k<=i-1;k++)
    strcpy(del[k].s,a[k].s);
    delc=i-1;
    c=1;
    }
    else if (c==1)
    {
    for(int k=0;k<=i-1;k++)
    strcpy(seg[k].s,a[k].s);
    segc=i-1;
    c=2;
    }
    else if (c==2)
    {
    strcpy(q0,a[i-1].s);
    c=3;
    }
    else if (c==3)
    {
    for(int k=0;k<=i-1;k++)
    strcpy(fs[k].s,a[k].s);
    fsc=i-1;
    c=4;
    }
    else if (c==4)
    {
    for(int k=0;k<=i-1;k++)
    strcpy(tt[t][k].s,a[k].s);
    ttc=i-1;
    t=t+1;
    }
    }
    printf("________\n");
    int j;
    for( j=0;j<=delc;j++)
    printf("%s ",del[j].s);

    printf("\n________\n");

    for(j=0;j<=segc;j++)
    printf("%s ",seg[j].s);

    printf("\n________\n%s",q0);

    printf("\n________\n");

    for(j=0;j<=fsc;j++)
    printf("%s ",fs[j].s);

    printf("\n__***______\n");

    for(I=0;i<t;i++)
    {
    for(j=0;j<=delc;j++)
    printf("%s ",tt[i][j].s);
    printf("\n");
    }
    f.close();

    char w[50],cs[10];
    strcpy(cs,q0);
    puts("Enter the string w:");
    gets(w);
    int fl,z,ip,sp;
    for( I=0;w[i]!='\0';i++)
    {
    fl=0;
    j=0;
    z=i;
    while(fl==0)
    {
    buf[j]=w[z];
    buf[++j]='\0';
    for(int x=0;x<=segc&&fl==0;x++)
    if (strcmp(buf,seg[x].s)==0)
    {
    fl=1;
    // cout<<buf<<endl;
    ip=position(buf,seg,segc);
    sp=position(cs,del,delc);

    if (strcmp(tt[sp][ip].s,"_")!=0)
    strcpy(cs,(tt[sp][ip].s));

    /////================================================== ========== P A T H ================================================
    cout<<cs<<" ";
    }
    if (fl==0) z=z+1;

    }
    i=z;
    strset(buf,NULL);
    }
    puts("current state is ");puts(cs);
    puts("path is ");



    //================================check acc/not

    for(int d=0;d<=fsc;d++)
    if(strcmp(fs[d].s,cs)==0)
    cout<<"\nAccepted <Even>\n";
    else
    cout<<"\nRejected <odd>\n";
    return 0;
    }

    int position(char x[50],stt array[50],int l )
    {
    int p,f=0;
    for(int z=0;z<=l&&f==0;z++)
    if (strcmp(x,array[z].s)==0)
    {
    p=z;
    f=1;
    }
    return p;
    }

  • All times are GMT -7. The time now is 07:51 PM.