我不了解该程序的工作方式;有人可以解释吗?
我知道它与状态机连接,但是我不了解状态机的工作原理。
void abba();
int main()
{
printf("Enter 10 characters, a or b.\n");
abba();
return 0;
}
void abba()
{
int x = 0;
char a;
while ((scanf_s("%c", &a) == 1) && (a == 'a' || a == 'b')) {
switch (x) {
case 0:
if (a == 'a')
x = 1;
break;
case 1:
if (a == 'b')
x = 2;
break;
case 2:
if (a == 'b')
x = 3;
else
x = 1;
break;
case 3:
if (a == 'b')
x = 0;
else {
puts("abba is found !");
x = 4;
}
break;
}
}
}