Is It a Number
Time Limit: 2000/1000 MS (Java/Others)Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 889Accepted Submission(s): 264
Problem Description
In some programming languages(like C/C++),we define numbers like this:
number -> digits optional_fraction optional_exponent
optional_fraction -> [empty] | .digits
optional_exponent -> [empty] | E digits | E - digits
digits -> digit | digits digit
digit -> [0-9]
Given a string,you have to tell whether it is a legal number.
number -> digits optional_fraction optional_exponent
optional_fraction -> [empty] | .digits
optional_exponent -> [empty] | E digits | E - digits
digits -> digit | digits digit
digit -> [0-9]
Given a string,you have to tell whether it is a legal number.
Input
The first line of input contains an integer T(1<=T<=100) which indicate the number of test cases. Then T test cases follow. Each test case contains one line. Each line contains a string whose length will not exceed 100.
Note: there are no spaces and tabs at the begin and the end of the string.
Note: there are no spaces and tabs at the begin and the end of the string.
Output
For each test case,you have to tell whether the string is a legal number. If it is a legal number,output "YES" in a single line,otherwise,output "NO" in a single line.
Sample Input
5 0123 123.456 0.456E-5 123 456 0.456EF
Sample Output
YES YES YES NO NO
符则直接匹配。一开始用getchar() WA 是两次,估计是末尾符号没有处理好,换成字符数组就AC了。
#include<stdio.h> #include<stdlib.h> char buf[3000]; char *token; //全局字符标志 bool match(char expectedToken)//字符匹配,匹配当前字符并且读入下一个字符 { if(expectedToken == *token) { //token = getchar(); ++token; return true; } return false; } bool isDigit(char expectedToken)//digit -> [0-9] { return ('0' <= expectedToken && expectedToken <='9'); } //digits -> digit | digits digit //消除左递归 //digits -> digit digits' //digits'-> digit digits'|$ bool digits() { if(!isDigit(*token)) return false; while(isDigit(*token)) { //token = getchar(); ++token; } return true; } //optional_fraction -> [empty] | .digits bool optional_fraction() { if(*token != '.') return true; else { match('.'); return digits(); } } //optional_exponent -> [empty] | E digits | E - digits bool optional_exponent() { if(*token != 'E') return true; else { match('E'); if(*token == '-') { match('-'); } return digits(); } } //number -> digits optional_fraction optional_exponent bool number() { return digits() && optional_fraction() && optional_exponent() && (*token == 0);//细节:最后判断是否已到末尾,很重要 } int main() { int T; scanf("%d\n",&T); while(T--) { gets(buf); token = buf; puts(number() ? "YES" : "NO"); } return 0; }