【问题标题】:DFA is not accepting stringsDFA 不接受字符串
【发布时间】:2014-08-05 14:55:40
【问题描述】:

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

首先程序询问状态数,如果你考虑我的图片,你会看到它有 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

那么代码有什么问题?

【问题讨论】:

  • 你能举一个你在程序中输入的数据给出错误结果的例子吗?
  • 我举了一个例子:3个状态,q1是final,字符串是01
  • 是的,但是如何在程序中输入?
  • 首先程序询问状态数,如果你考虑我的图片,你会看到它有 3 个状态(q0,q1,q2)所以我们输入 3。然后它询问输入的数量.我的输入是 0,1 所以我输入 2... 然后它要求输入我们输入 0 然后 1 的输入!然后它询问最终状态的数量,这里的最终状态只有 q1...所以我们输入它...然后您会看到: (q0,0) = q 这意味着 q0 进入 0 的哪个状态...例如,这里 q0 以 0 转到 q0 ...其他人就是这样。填写完这部分后,我们输入sring,它会说字符串是否有效
  • @LittleGirl 你确定这必须在c++ 标签而不是c 标签中吗?

标签: c visual-studio-2012 dfa


【解决方案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;
}

【讨论】:

    【解决方案2】:

    只需在 %c 之前放一个空格。并编辑一些代码行。 此代码在 Code::Blocks 13.12 版本上成功编译。 因为它 C++ 编译器使用 .cpp 扩展名保存文件。 并单击“输出图像”。您将看到如何输入和输出 控制台

    #

    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】:

      使用 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;
      }
      

      【讨论】:

        【解决方案4】:

        这部分代码:

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

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2017-07-26
          • 2016-01-08
          • 2019-09-02
          • 2014-03-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多