473,396 Members | 1,775 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

implementing dfa for a binary string with at least two 0's after every 1

i want to implement dfa that accepts the binary string with at least two 0's after every 1 .....

please help me ...
Feb 24 '13 #1
3 2644
weaknessforcats
9,208 Expert Mod 8TB
Are you going to do this in C or C++?

If you are using C++, you use the State design pattern to implement your finite state automaton.

Read this: http://bytes.com/topic/c/insights/66...patterns-state

There is an example.

If you are using C, then replace the polymorphism with a switch statement where the cases are the states.
Feb 24 '13 #2
thank you........this is my code using C++.I did it very simple.

but it didn't say either it is accepted or rejected :/


Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include <iostream>
  4.  
  5. using namespace std;
  6. int main ()
  7.  
  8. {
  9.     int state = 0;
  10.     char ch;
  11.     while(1)
  12.     {
  13.         cin.get(ch);
  14.             if (state == 0)
  15.             {
  16.                       if (ch == '0')
  17.                       state = 0;
  18.                       else
  19.                       state = 1;
  20.             }
  21.  
  22.             else if (state == 1)
  23.             {
  24.                       if (ch == '0')
  25.                       state = 2;
  26.                       else 
  27.                       state = 4;
  28.             }
  29.  
  30.             else if (state == 2)
  31.             {
  32.                       if (ch == '0')
  33.                       state = 3;
  34.                       else
  35.                       state = 4;
  36.             }
  37.  
  38.             else if (state == 3)
  39.             {
  40.                       if (ch == '0')
  41.                       state = 3;
  42.                       else 
  43.                       state = 1;
  44.             }
  45.  
  46.             else if (state == 4)
  47.             {
  48.                       if (ch = '0')
  49.                       state = 4;
  50.                       else
  51.                       state = 4;
  52.             }
  53.  
  54.     }
  55.     if (state == 3 || state == 0)
  56.     cout << "ACCEPTED :-)" <<endl ;
  57.     else 
  58.     cout << "REJECTED :-(" <<endl;   
  59.  
  60.    /* if (state == 3)
  61.  
  62.      cout << "ACCEPTED :-)" <<endl; 
  63.     else if (state == 0)
  64.      cout << "ACCEPTED :-)" <<endl; 
  65.      else 
  66.      cout << "REJECTED :-(" <<endl; */
  67.  
  68.      getch();
  69.      return 0;
  70. }
Feb 26 '13 #3
donbock
2,426 Expert 2GB
The while loop never ends, so your program doesn't fall through to that little bit at the end that decides whether or not to print ACCEPTED or REJECTED.
(If you used CODE tags then I could refer to specific line numbers.)
Feb 26 '13 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Johannes | last post by:
Hi, I tried to pack eight integer values and one string into one binary string, which I would like to store in a mysql db. I encountered two problems doing this: 1) $this->packed =...
7
by: Dan Jones | last post by:
Hello, I'm trying to figure out how to get bit operators to work on a binary string. This is what I'm trying to do: list(frame) #where frame is a binary string y = frame << 8 It gives me a...
2
by: Pete | last post by:
Sorry for the ambiguity of my last post, What I am try to do is enter a 10 bit binary string eg: 1110001010 and then permute them into an array using an array containing 3,5,2,7,4,10,1,9,8,6 as...
5
by: sathyashrayan | last post by:
Group, I have some doubts in the following program. ------------------program--------------------- /* ** Make an ascii binary string into an integer. */ #include <string.h> unsigned int...
6
by: Tarun | last post by:
Hi All, I need to find a particular substring in a binary string(some text appended & prepended to binary data). I cant use strstr since it terminates on receiving '\0'which can be there in...
10
by: jt | last post by:
I'm needing to take a binary string start at a certain position and return a pointer from that postion to the end of the binary stirng. something like this: char bstr; char *pos; ...
7
by: elliotng.ee | last post by:
I have a text file that contains a header 32-bit binary. For example, the text file could be: %%This is the input text %%test.txt Date: Tue Dec 26 14:03:35 2006...
5
by: gezerpunta | last post by:
Hi strlen does not return the correct value .I compared the filesize() and strlen byte size but they are not equal. I must find binary string length and it must be equal to filesize() thks.
5
by: andrew.douglas11 | last post by:
Hello, I'm looking to display an image in the browser using a binary string containing all the bytes that make up a GIF image. I've tried all the standard encodings in System.Text.Encoding,...
3
by: Andrew Lentvorski | last post by:
Basically, I'd like to use the ctypes module as a much more descriptive "struct" module. Is there a way to take a ctypes.Structure-based class and convert it to/from a binary string? Thanks,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.