DFA不接受字符串

时间:2014-06-15 18:07:59

标签: c visual-studio-2012 dfa

我的程序应该为二进制字符串实现dfa ... dfa是一次只能处于一种状态的机器(当你输入一个字符串时,机器应该使用它,在最后一步它应该达到最终状态。如果是这样,我们说该字符串被机器接受了)... 这台机器就是这样工作的:

在第一个程序询问状态的数量,如果你考虑我的pic,你看它有3个状态(q0,q1,q2)所以我们输入3.然后它询问输入的数量。我的输入是0,1所以我输入2 ...然后它要求输入输入,我们输入0然后输入1!然后它询问最终状态的数量,这里的最终状态只有q1 ......所以我们输入它...然后你看到这个:(q0,0)= q这意味着q0进入哪个状态0 ...例如,这里q0与0一起变为q0 ......其他就像那样。在填写这部分之后,我们输入sring,它会说是字符串是否有效

这是代码:

   #include<stdio.h>
#include<conio.h>

int ninputs;

int check(char,int ); //function declaration
int dfa[10][10];
char c[10], string[10];

int main()
{
     int nstates, nfinals;
     int f[10];
     int i,j,s=0,final=0;
     printf("enter the number of states that your dfa consist of \n");
     scanf("%d",&nstates); // 3
     printf("enter the number of input symbol that dfa have \n");
     scanf("%d",&ninputs); // 2
     printf("\nenter input symbols\t");

     for(i=0; i<ninputs; i++) 
     {
           printf("\n\n %d input\t", i+1);
           printf("%c",c[i]=getch()); // 01
     }

     printf("\n\nenter number of final states\t");
     scanf("%d",&nfinals); // 1

     for(i=0;i<nfinals;i++)
     {
          printf("\n\nFinal state %d : q",i+1);
          scanf("%d",&f[i]); // 1
     }

     printf("-----------------------------------------------------------------------");
     printf("\n\ndefine transition rule as (initial state, input symbol ) = final state\n");

     for(i=0; i<ninputs; i++)
     {
          for(j=0; j<nstates; j++)
          {
               printf("\n(q%d , %c ) = q",j,c[i]);
               scanf("%d",&dfa[i][j]);
               // q(0,0)=0
               // q(1,0)=0
               // q(2,0)=2
               // q(0,1)=1
               // q(1,1)=2
               // q(2,1)=1
          }
     }

     do
     {
          i=0;
          printf("\n\nEnter Input String.. ");
          scanf("%s",string);  // 01

          while(string[i]!='\0')
          {
              if((s=check(string[i++],s))<0)
                  break;
              for(i=0 ;i<nfinals ;i++)
              {
                  if(f[i] ==s )
                      final=1;
                  if(final==1)
                      printf("\n valid string"); 
                  else
                      printf("invalid string");
                  getch();

                  printf("\nDo you want to continue.?  \n(y/n) ");
              }
          }
     }
     while(getch()=='y');

     getch();
}
int check(char b,int d)
{
     int j;
     for(j=0; j<ninputs; j++)
         if(b==c[j])
             return(dfa[d][j]);
     return -1;
}

问题是,当我输入我的字符串时,程序说它无效,但它应该被机器接受...例如考虑照片中的机器并测试它上面的字符串:01

enter image description here

那么代码有什么问题?

4 个答案:

答案 0 :(得分:1)

#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;

main()
{

    int state,symbol;
    char str[30];

    cout<<"Enter the string which you want to check :: "<<endl;
    fgets(str,sizeof(str),stdin);

    cout<<"Enter the Number of state    :: ";cin>>state;
    cout<<"Enter the Number of input symbol :: ";cin>>symbol;

    cout<<"Enter the states : "<<endl;
    char st[state];
    for(int i=0;i<state;i++)
    {
        cout<<"state : "<<i<<" : ";cin>>st[i];
    }
    cout<<"Enter the input symbol : "<<endl;
    char sy[symbol];
    for(int i=0;i<symbol;i++)
    {
        cout<<"symbol : "<<i<<" : ";cin>>sy[i];
    }
    char table[state][symbol];
    cout<<"------Enter next move or state------ if no relation put -"<<endl;
    for(int i=0;i<state;i++)
    {
        for(int j=0;j<symbol;j++)
        {
            cout<<"("<<st[i]<<", "<<sy[j]<<") = ";cin>>table[i][j];
        }
    }
    char ststate,fnstate;
    cout<<"Start state :: ";cin>>ststate;
    cout<<"Final state :: ";cin>>fnstate;


    cout<<"----------------------------------"<<endl;

    int str_len=strlen(str);
    int p,q;
    int flag=1;
    int j=0;

    for(j=0;j<str_len-1;j++)
    {
        cout<<"input state"<<endl;
        for(int i=0;i<state;i++)
        {   
            if(st[i]==ststate)
            {
                p=i;
                cout<<p<<endl;
                break;
            }
            else
            {
                p=-1;
            }
        } 
        cout<<"input string"<<endl;
        for(int i=0;i<state;i++)
        {   
            if(sy[i]==str[j])
            {
                q=i;
            //  cout<<q<<endl;
                break;
            }
            else
            {
                q=-1;
            }
        } 
        if(p!=-1 && q!=-1)
        {
            cout<<"table output :: "<<table[p][q]<<endl;
            ststate=table[p][q];
        }   

    }
    cout<<endl;
    cout<<"----------------------------------------------------------------------------------";
    if((table[p][q]==fnstate) && (j==str_len-1))
            {
                cout<<"String is recognized"<<endl;
                flag=0;
            }
    if(flag!=0)
    {
        cout<<"String is not recognized.";
    }
    cout<<endl;


return 0;
}
 // i Can't be able to put image just because my reputation is only 1 but i am dam sure this program work successfully as not much as my reputation on stack overflow.

答案 1 :(得分:1)

使用DFA

接受字符串的优化C代码
#include<stdio.h>
#include<conio.h>
#include<string.h>

int main()
{
int a,b,i,j,k,state,ch;
char s[10][10],*st,v[10],ss[10];
printf("Enter the number of state:\n");
scanf("%d",&a);
printf("Enter State :\n");
for(i=0;i<a;i++)
{
    fflush(stdin);
    scanf("%c",&ss[i]);
}
printf("Enter th no. of var..:\n");
scanf("%d",&b);
printf("Enter variable :\n");
for(i=0;i<b;i++)
{
    fflush(stdin);
    scanf("%c",&v[i]);
}
printf("Enter table:\n");
for(i=0;i<a;i++)
{
    for(j=0;j<b;j++)
    {
       fflush(stdin);
       scanf("%c",&s[i][j]);
    }
}
printf("Enter string :\n");
fflush(stdin);
gets(st);
i=0;
state=0;
while(st[i]!='\0')
{
    for(j=0;j<b;j++)
    {
    if(st[i]==v[j])
    {
          if(s[state][j]=='-')
          {
               goto check;
          }
          else
          {
            for(k=0;k<a;k++)
            {
                 if(s[state][j]==ss[k])
                 {
                      printf("State:%c\n",s[state][j]);
                      state=k;
                      goto o;
                 }
            }
          }
          o:
    }
    }
    i++;
}
check:
ch=1;
for(i=0;i<b;i++)
{
    if(s[state][i]!='-')
    {
        ch=0;
    }
}
if(ch==1)
{
    printf("String is matching..");
}
else
{
    printf("String is not matching..");
}
getch();
return 0;
}

答案 2 :(得分:1)

  

在%c之前放一个空格。并编辑代码的某些行。   Code :: Blocks 13.12版本成功编译了此代码。   因为C ++编译器以.cpp扩展名保存文件。   然后单击“输出图像”。你会看到如何输入&amp;输出来自   控制台

     

&lt; --------这是编辑过的代码&amp;&amp;它会正常工作---&gt; #

output image

#include<stdio.h>
#include<cstdio>
#include<iostream>


int ninputs,bb;

int check(char,int ); //function declaration
int dfa[10][10];
char c[10], string[10],b;

int main()
{
     int nstates, nfinals;
     int f[10];
     int i,j,s=0,final=0;
     printf("enter the number of states that your dfa consist of \n");
     scanf("%d",&nstates);
     printf("enter the number of input symbol that dfa have \n");
     scanf("%d",&ninputs);
     printf("\nenter input symbols");

     for(i=0; i<ninputs; )
     {
     bb =i;

           printf("\n %d input",bb+1);
          // printf("  %c",c[i]=getchar());
          scanf(" %c" , &c[i]); //just put an space before %c


          i++;

     }

     printf("\n\nenter number of final states\t");
     scanf("%d",&nfinals);

     for(i=0;i<nfinals;i++)
     {
          printf("\n\nFinal state %d : q",i+1);
          scanf("%d",&f[i]);
     }

     printf("-----------------------------------------------------------------------");
     printf("\n\ndefine transition rule as (initial state, input symbol ) = final state\n");

     for(i=0; i<ninputs; i++)
     {
          for(j=0; j<nstates; j++)
          {
               printf("\n(q%d , %c ) = q",j,c[i]);
               scanf("%d",&dfa[i][j]);
          }
     }

     do
     {
          i=0;
          printf("\n\nEnter Input String.. ");
          scanf("%s",string);

          while(string[i]!='\0')
          if((s=check(string[i++],s))<0)
          break;
          for(i=0 ;i<nfinals ;i++)
          if(f[i] ==s )
          final=1;
          if(final==1)
          printf("\n valid string");
          else
          printf("invalid string");

          printf("\nDo you want to continue.?  \n(y/n) ");
     }
     while(b =='y');

     scanf(" %c", &b); // chnge here
}
int check(char b,int d)
{
     int j;
     for(j=0; j<ninputs; j++)
     if(b==c[j])
     return(dfa[d][j]);
     return -1;
}

答案 3 :(得分:0)

这部分代码:

      while(string[i]!='\0')
      {
          if((s=check(string[i++],s))<0)
              break;
          for(i=0 ;i<nfinals ;i++)
          {

在两个循环中使用和更改i。你需要在其中一个中使用其他东西。