状态机 判断实数
#include <stdio.h> // 状态机 int isRealNumber(char *p) { int state=1; int resualt=-1; while (*p) { switch(state) { case 1: if ('+'==*p||'-'==*p) { state=2; } else if (*p>='0'&&*p<='9') { state=4; } else if ('.'==*p) { state=3; } else { state=0; } break; case 2: if (*p>='0'&&*p<='9') { state=4; } else if ('.'==*p) { state=3; } else { state=0; } break; case 3: if (*p>='0'&&*p<='9') { state=5; } else { state=0; } break; case 4: if (*p>='0'&&*p<='9') { state=4; } else if ('.'==*p) { state=5; } else { state=0; } break; case 5: if (*p>='0'&&*p<='9') { state=5; } else { state=0; } break; } if ('0'==state) { return -1; } p++; } if (4==state||5==state) { resualt=0; } return resualt; } int main() { char str[255] = "..11111111111119"; printf("\n%d\n",isRealNumber(str)); return 0; }