switch statement - Finite State Machine In C -


i trying create simple finite state machine in c , i'm quite confused on how started. tried looking online nothing has cleared me.

my goal check if string octal, hex or decimal.

to octal string must start 0, followed digits 0-7. hex string must start 0x or ox, followed (a-f, a-f, 0-9)

my attempt @ creating states be:

typedef enum  {    error,    octal,    hex,    decimal } stringstates; 

now, use switch statement go through entirety of string , switch between different states until have correctly identified state belongs to.

 while (current_position<=end_string-1)  {     switch( "input here")       {         case 0:              //process string              break;         case 1:              //process string              break;           case 2:              //process string              break;          case 3:              //process string              break;          default:              break;       }   } 

this concept still new me , i'm having hard time understanding implementation. if can shed light, it'll appreciated.

it pretty straight forward question , solution simple.

i have 7 states namely 0 6 shown diagram.0 initial state. 3,4,5 final states , 6 dead state.

enter image description here

state 0: initial state , state can encounter following chars:

0 or o or 1-9

if other char error there , no need process further.

state 1: if char state 0 0 next state ,

if char state x string hexadecimal(state=4) , no need process further char can follow.

if char state 0-7 string octal(state=5) , process till end of string see if char different 0-7, if error there invalid string , no need process further it.

state 2: if char state 0 o next state , state if next char x string hexadecimal(state=4) , no need process further, if not error there.

state 3: if char state 0 1-9 string decimal number(state=3) , process till end of string see if char different 0-9, if error there invalid string , no need process further it.

state 4:hexadecimal number

state 5:octal number

state 6:error meaning invalid string

here c code. have taken length of string 9, simplicity , nothing else.

#include <stdio.h> #include <stdlib.h> int main() {   char *a="066676777";   int state=0;int i=0;   while(state!=6&&i<9)   {       switch(state)       {       case 0:         if(a[i]=='0')             state=1;         else if(a[i]=='o')             state=2;         else if(a[i]>=49&&a[i]<=57)             state=3;         else {state=6;i=9;}         break;       case 1:          if(a[i]=='x')          {               state=4;i=9;          }          else if(a[i]>=48&&a[i]<=55)          {              state=5;              while(i<9)                 if(a[i]>=48&&a[i]<=55)                  ++i;                 else {state=6;i=9;}          }          else {state=6;i=9;}          break;       case 2:           if(a[i]=='x')           {               state=4;i=9;           }           else {state=6;i=9;}          break;       case 3:           while(i<9)             if(a[i]>=48&&a[i]<=57)                 ++i;             else {state=6;i=9;}             break;       default:         printf("please select correct initial state");          break;       }       ++i;     }     if(state==3)       printf("it decimal number");     else if(state==4)       printf("it hexadecimal number");     else if(state==5)       printf("it octal number");     else printf("error encountered invalid string"); } 

Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -