csu 1031 Parsing Real Numbers

5373 ワード

まずマークして、それから状況によって判断して、最後に必ず多くのテストをしなければなりません.いささか面倒を呈する.
/* 1031: Parsing Real Numbers */
# include <stdio.h>
# include <ctype.h>
# include <string.h>

# define BITSET(i) ((sign)|=(char)(0x1<<(i)))
# define GETBIT(i) ((sign)>>(i) & (0x1))

char a[1000];
char sign;

/* sign: 0 - wrong?
1 - coeff sign
2 - integ
3 - digit
4 - floatdigit
5 - e
6 - e sign
7 - e integ
*/

int main()
{
int i, T, len, judge;

//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);

scanf("%d", &T);
while (T > 0)
{
scanf("%s", a);
len = strlen(a);

sign = 0;
i = 0;
if (a[i]=='+' || a[i]=='-') { BITSET(1); ++i;}
while (isdigit(a[i])) { BITSET(2); ++i;}
if (a[i]=='.') { BITSET(3); ++i;}
while (isdigit(a[i])) { BITSET(4); ++i;}
if (a[i]=='E' || a[i]=='e') { BITSET(5); ++i;}
if (a[i]=='+' || a[i]=='-') { BITSET(6); ++i;}
while (isdigit(a[i])) { BITSET(7); ++i;}

if ((a[i] != '\0')
|| (GETBIT(3) && !GETBIT(2) && !GETBIT(4))
|| ((GETBIT(5)||GETBIT(6)) && !GETBIT(7))
|| (len && !GETBIT(2) && !GETBIT(4)))
BITSET(0);

if (GETBIT(0)) printf("NO
");
else printf("YES
");
--T;
}

return 0;
}