-2

my program is supposed to implement a dfa for a binary string... dfa is a machine that can be in only one state at a time (when you enter a string the machine should use it and at the last step it should reach to the final state. if so, we say that string is accepted by the machine) ... this machine works this way:

at first program asks the number of states, if u consider my pic, u see that it has 3 states(q0,q1,q2) so we enter 3. then it asks about the number of inputs. my inputs are 0,1 so I enter 2... then it asks to enter the inputs, we enter 0 then 1! then it asks the number of final states, the final state here is only q1... so we enter it... then u see this: (q0,0) = q it means q0 goes to which state with 0... for example here q0 goes to q0 with 0 ... others are like that. after filling this part, we enter the sring and it would say if string is valid or not

here is the code:

   #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;
}

the problem is that when I enter my string, the program says it's invalid, however it should be accepted by the machine... for example consider the machine in the photo and test this string on it: 01

enter image description here

so what's wrong with the code?

wimh
  • 15,072
  • 6
  • 47
  • 98
Little Girl
  • 27
  • 2
  • 8
  • can you give an example of the data you enter in your program which gives the wrong result? – wimh Jun 15 '14 at 18:13
  • I gave an example: 3 states, q1 is final, string is 01 – Little Girl Jun 15 '14 at 18:14
  • Yes, but how do you enter that in your program? – wimh Jun 15 '14 at 18:16
  • at first program asks the number of states, if u consider my pic, u see that it has 3 states(q0,q1,q2) so we enter 3. then it asks about the number of inputs. my inputs are 0,1 so I enter 2... then it asks to enter the inputs we enter 0 then 1! then it asks the number of final states, the final state here is only q1... so we enter it... then u see this: (q0,0) = q it means q0 goes to which state with 0... for example here q0 goes to q0 with 0 ... others are like that. after filling this part, we enter the sring and it would say if string is valid or not – Little Girl Jun 15 '14 at 18:27
  • @LittleGirl Are you sure this has to be in the `c++` tag and not the `c` tag? – Scis Jun 15 '14 at 18:33
  • @Scis really sorry, yes I edited the post – Little Girl Jun 15 '14 at 18:39
  • @LittleGirl I added the inputs as comments in the code. Is this the situation the program does not work correctly? – wimh Jun 15 '14 at 18:42

4 Answers4

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;
}
Manish Bhadani
  • 437
  • 6
  • 13
1

Optimized C code for Accepting String using DFA

#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;
}
1

Just put an space before %c . And edit some lines of the code. This code is successfully complied on Code::Blocks 13.12 version. As it C++ Compiler save the file with .cpp extension. And Click On the " output image " . You will see how to input & output from console

<--------Here Is the edited code && It will work fine ---> #

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;
}
mukit khan
  • 31
  • 3
0

This part of the code:

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

uses and changes i in both loops. You need to use something else in one of them.

wimh
  • 15,072
  • 6
  • 47
  • 98