In mathematics a finite state machine is a convenient way to

In mathematics, a finite state machine is a convenient way to model the behavior of various systems. To do this, it allows the system to be in various states, and uses set rules to determine when to switch to other ones. As a simple example, imagine a vending machine that sells cardy for 10¢ and accepts nickels and dimes. The machine could have three states-no money has been inserted, 5¢ has been, and 10¢ has been A nickel would move it from 0 to 5 or 5 to 10, a dime would move it from any state to 10 (sorry, no change), and pushing the dispense button would set the machine back to 0 and, more importantly, give you candy! For this program, you will use a finite state machine to help tokenize a string, splitting it every time a space shows up, unless that space is between quotation marks. Thankfully, your TAs have been gracious enough to do most of the work for you. All that\'s left in the project is implementing the function void transition(char c, int *state); This function will take the latest character and the current state to figure out which state it transitions to, and outputs the result to the same variable, To actually determine the next state, use these rules: If *state equals 0, we\'re waiting for something that isn\'t a space. Stay in state 0 if c is a space, switch to state 1 if c is a quotation mark, or switch to state 2 if c is anything else

Solution

Header File:(hw4p6.h)

#ifndef TRANSITION_H_   
#define TRANSITION_H_

void transition(char c,   int *state);

#endif

Code(hw4p6.c):

#include <stdio.h>
#include \"hw4p6.h\"

void transition(char c,   int *state)
{
if(*state==0)
{
   if(c==\' \') *state=0;
   else if(c==\'\"\') *state=1;
   else *state=2;
}
else if(*state==1)
{
   if(c==\'\"\') *state=2;
   else *state=1;
}
else if(*state==2)
{
   if(c==\' \') *state=0;
   else if(c==\'\"\') *state=1;
   else *state=2;
}
}
/* Just a sample main function, change it according to the requiremet
int main()
{
   char s[10]=\"abc\\\" ab \\\"\";
   int i=0,state=0;
   while(i<10)
   {
       transition(s[i],&state);
       printf(\"%d\",state);
       i++;
   }
   return 0;
}*/

 In mathematics, a finite state machine is a convenient way to model the behavior of various systems. To do this, it allows the system to be in various states,
 In mathematics, a finite state machine is a convenient way to model the behavior of various systems. To do this, it allows the system to be in various states,

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site