c 语言状态机编程,【C语言】状态机编程

#include int get_input_type(char c)

{

if(c == ' ')

return 1;

return 0;

}

int main(void)

{

char buf[] = " This is a book. That is a bag\n";

//input 11000010011101000001111000011001010000

//state 012000010012201000001222000012001010000

//print p pppppppp pppppppp ppppp ppppppppp

//0 0 -> 0

//0 1 -> 1

//1 0 -> 0

//1 1 -> 2

//2 0 -> 0

//2 1 -> 2

int state = 0;

int i = 0;

char *p;

int counter = 0;

printf("buf = %s\n",buf);

i = 0;

while(1)

{

char c;

int input = 0;

c = buf[i];

input = get_input_type(c);

//printf("c = %c,input = %d\n",c,input);

if(c == '\0')

break;

//0 0 -> 0

//0 1 -> 1

//1 0 -> 0

//1 1 -> 2

//2 0 -> 0

//2 1 -> 2

if(state == 0 && input == 0)

{

state = 0;

printf("%c",c);

}

else

if(state == 0 && input == 1)

{

state = 1;

printf("%c",c);

}

else

if(state == 1 && input == 0)

{

state = 0;

printf("%c",c);

}

else

if(state == 1 && input == 1)

{

state = 2;

//do nothing ,dont output

}

if(state == 2 && input == 0)

{

state = 0;

printf("%c",c);

}

else

if(state == 2 && input == 1)

{

state = 2;

//do nothing ,dont output

}

i++;

}

return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值