C???????????????
???????????? ???????[ 2013/5/8 9:54:27 ] ????????
??????????????????????????????????У?????????????????????仯??????????????Щ?????????????????
??????????????·??????????????п???????????г?????????????????е????ж????????к????????????????????y?????????????????????????????????????????????????????????
????FSM?????????
????1??switch/case????if/else
???????????????????????????????ж???????????????????????С?????????????????????????????????????????????????????????????
????2??????
???????????????????????????????????????????????????????洢????????????????????????????????????????????洢?????????
????3?????State Pattern
???????State Pattern????????????switch/case???????????????????к???????????????100????????????Robert C. Martin???????????????FSM?????????for java??for C++?????????http://www.objectmentor.com/resources/index?????????????????????????????????????????????????????State Pattern?????????developer???????????????????????????????e????bug?????????code??
????4????ú???????????
????????????C++??????????????#define????????????????????ú??????庯???????????????????????????????????????????????????????????Ч????MFC????ú???????????????
?????????FSM???????????Щ????????if/else???л??????????????У?????????????п???3??????????????????д???????????????????????1???????Ч?????????????C??????????????C++????????MFC???????????????????е??
????????????????
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef enum{
STATE0 = 0??
STATE1??
STATE2??
STATE3??
STATE4??
}STATE;
int main()
{
char ch;
STATE current_state = STATE0;
while(1){
printf("In put password:");
while((ch = getchar()) != '
')
{
if((ch < '0') || (ch > '9'))
{
printf("Input num??ok?/n");
break;
}
switch(current_state){
case STATE0:
if(ch == '2') current_state = STATE1;
break;
case STATE1:
if(ch == '4') current_state = STATE2;
break;
case STATE2:
if(ch == '7') current_state = STATE3;
break;
case STATE3:
if(ch == '9') current_state = STATE4;
break;
default:
current_state = STATE0;
break;
}
}
if(current_state == STATE4){
printf("Correct?? lock is open!
");
current_state = STATE0;
}else
{
printf("Wrong?? locked!
");
current_state = STATE0;
}
break;
}
return 0;
}
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11