C中的有限状态机 [英] Finite State Machine In C

查看:26
本文介绍了C中的有限状态机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用 C 创建一个简单的有限状态机,但我对如何开始感到很困惑.我尝试在网上查找,但没有什么能真正解决我的问题.

I am trying to create a simple finite state machine in C and I'm quite confused on how to get started. I tried looking online but nothing has really cleared this up for me.

我的目标是检查字符串是八进制、十六进制还是十进制.

My goal is to check if a string is octal, hex or a decimal.

要成为八进制,字符串必须以 0 开头,后跟数字 0-7.要成为十六进制,字符串必须以 0x 或 OX 开头,后跟 (a-f, A-F, 0-9)

To be octal the string must start with a 0, followed by digits 0-7. To be hex the string must start with 0x or OX, followed by (a-f, A-F, 0-9)

我创建状态的尝试是:

typedef enum  {
   ERROR,
   OCTAL,
   HEX,
   DECIMAL
} stringStates;

现在,我将使用 switch 语句遍历整个字符串并在不同状态之间切换,直到我正确识别出它属于哪个状态.

Now, I would then use a switch statement to go through the entirety of the string and switch between the different states until I have correctly identified which state it belongs to.

 while (current_Position<=end_String-1)
 {
    switch( "input something 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 is still very new to me and I'm having hard time understanding its implementation. If anyone can shed some light, it'll be much appreciated.

推荐答案

这是一个非常直接的问题,解决方案也非常简单.

It is a pretty much straight forward question and the solution is also very simple.

我有 7 个状态,即从 0 到 6,如图所示.0 是初始状态.3,4,5 可能是最终状态,6 是死状态.

I have 7 states namely from 0 to 6 as shown by diagram.0 is the initial state. 3,4,5 could be the final states and 6 is the dead state.

state 0:初始状态,从这个状态我们只能遇到以下字符:

state 0: Initial state and from this state we can only encounter following chars:

0O1-9

如果有任何其他字符,则存在错误,无需进一步处理.

if any other char then an error is there and no need to process further.

状态 1:如果状态 0 的字符是 0 那么这是下一个状态和

state 1: if char from state 0 is 0 then this is the next state and

如果此状态的字符为 x,则字符串为十六进制(state=4),无需进一步处理,因为后面可以跟任何字符.

if char from this state is x then the string is hexadecimal(state=4) and no need to process further as any char can follow.

如果此状态的字符为 0-7,则字符串为八进制(状态 = 5),我们一直处理到字符串的末尾,以查看是否得到与 0-7 不同的字符,如果这样做,则错误如下无效的字符串,我们一拿到就不需要进一步处理了.

if char from this state is 0-7 then string is octal(state=5) and we process till the end of string to see if we get any char different from 0-7, if we do then error is there as invalid string and no need to process further as soon as we get it.

状态 2:如果状态 0 中的字符为 O,则这是下一个状态,如果下一个字符为 X,则从此状态开始,字符串为十六进制(状态 = 4),无需进一步处理, 如果不是,则存在错误.

state 2: if char from state 0 is O then this is the next state and from this state if next char is X then string is hexadecimal(state=4) and no need to process further, if it is not then error is there.

状态 3:如果状态 0 中的字符是 1-9,那么字符串是十进制数(状态=3),我们处理直到字符串的末尾,看看我们是否得到了与 0 不同的字符-9,如果我们这样做,那么错误是作为无效字符串存在,并且一旦我们得到它就不需要进一步处理.

state 3: if char from state 0 is 1-9 then string is decimal number(state=3) and we process till the end of string to see if we get any char different from 0-9, if we do then error is there as invalid string and no need to process further as soon as we get it.

state 4:十六进制数

状态 5:八进制数

状态 6:错误意味着无效字符串

state 6:error meaning invalid string

这是C代码.我将字符串的长度设为 9,只是为了简单起见,仅此而已.

Here is the C code. I have taken the length of the string to be 9, just for simplicity and 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 is a decimal number");
    else if(state==4)
      printf("it is a hexadecimal number");
    else if(state==5)
      printf("it is a octal number");
    else printf("error encountered as invalid string");
}

这篇关于C中的有限状态机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆