问题
程序实现
源程序
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int getnext(int cur, char c)
{
switch (cur){
case 1:
if (c=='+' || c=='-') return 2;
if (isdigit(c)) return 3;
return -1;
case 2:
if (isdigit(c)) return 3;
return -1;
case 3:
if (isdigit(c)) return 3;
if (c == '.') return 4;
if (c=='e' || c=='E') return 6;
return -1;
case 4:
if (isdigit(c)) return 5;
return -1;
case 5:
if (isdigit(c)) return 5;
if (c == '\0') return 0;
if (c=='e' || c=='E') return 6;
return -1;
case 6:
if (c=='+' || c=='-') return 7;
if (isdigit(c)) return 8;
return -1;
case 7:
if (isdigit(c)) return 8;
return -1;
case 8:
if (isdigit(c)) return 8;
if (c == '\0') return 0;
return -1;
default:
return -1;
}
}
int main()
{
char s[256];
char t[256];
int i, j, k, len;
int state;
gets(t);
len = strlen(t);
i = 0;
while (i<len && t[i]==' ') i++;
j = len-1;
while (j>=0 && t[j]==' ') j--;
k = 0;
for (; i<=j; i++)
s[k++] = t[i];
s[k] = '\0';
state = 1;
for (i=0; i<=strlen(s); i++){
state = getnext(state, s[i]);
if (state == -1) break;
}
printf("%s\n", state==0 ? "YES" : "NO");
return 0;
}
结果