From 91e7155c385179b470c06353fb8dab43f68327c5 Mon Sep 17 00:00:00 2001 From: cooljacket Date: Wed, 7 Sep 2016 00:38:45 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AE=80=E5=8C=96=E9=9D=A2=E8=AF=95=E9=A2=9854?= =?UTF-8?q?=E7=9A=84=E5=8C=B9=E9=85=8D=E9=80=BB=E8=BE=91=EF=BC=8C=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=E5=8F=82=E6=95=B0=E7=B1=BB=E5=9E=8B=E4=B8=BAconst=20c?= =?UTF-8?q?har*...=E4=BD=BF=E5=BE=97g++=E4=B8=8D=E6=8A=A5=E4=B8=80?= =?UTF-8?q?=E5=A0=86warning?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- NumericStrings/NumericString.cpp | 70 ++++++++++++-------------------- 1 file changed, 25 insertions(+), 45 deletions(-) diff --git a/NumericStrings/NumericString.cpp b/NumericStrings/NumericString.cpp index 076d342..37b4909 100644 --- a/NumericStrings/NumericString.cpp +++ b/NumericStrings/NumericString.cpp @@ -7,52 +7,39 @@ #include -bool scanDigits(char** str); -bool isExponential(char** str); +bool scanUnsignedInteger(const char** str); +bool scanInteger(const char** str); -bool isNumeric(char* str) +// the form of exponent is like A[.[B]][e|EC] or .B[e|EC] +// where A and C is an integer with or without sign, and B is an unsigned integer +bool isNumeric(const char* str) { if (str == NULL) return false; - if (*str == '+' || *str == '-') + bool numeric = scanInteger(&str); + + // for floats + // you can have no integer part such as .123 means 0.123 + // in the meanwhile, you can have no decimal part such as 233. means 233.0 + // what's more, 233.666 is OK also. + if (*str == '.') { ++str; - if (*str == '\0') - return false; + numeric = scanUnsignedInteger(&str) || numeric; + } - bool numeric = true; - - bool hasDigits = scanDigits(&str); - if (*str != '\0') - { - // for floats - if (*str == '.') - { - ++str; - if (*str == 'e' || *str == 'E') - return false; - if (!hasDigits && *str == '\0') - return false; - - scanDigits(&str); - - if (*str == 'e' || *str == 'E') - numeric = isExponential(&str); - } - // for integers - else if (*str == 'e' || *str == 'E') - numeric = isExponential(&str); - else - numeric = false; + // for exponent + if (*str == 'e' || *str == 'E') { + ++str; + numeric = scanInteger(&str) && numeric; } return numeric && *str == '\0'; } -bool scanDigits(char** str) +bool scanUnsignedInteger(const char** str) { - char* pBefore = *str; - + const char* pBefore = *str; while (**str != '\0' && **str >= '0' && **str <= '9') ++(*str); @@ -60,25 +47,17 @@ bool scanDigits(char** str) return *str > pBefore; } -bool isExponential(char** str) +// an integer's form is like [+|-]B, where B is an unsigned integer +bool scanInteger(const char** str) { - if (**str != 'e' && **str != 'E') - return false; - - ++(*str); if (**str == '+' || **str == '-') ++(*str); - - if (**str == '\0') - return false; - - scanDigits(str); - return (**str == '\0') ? true : false; + return scanUnsignedInteger(str); } // ==================== Test Code ==================== -void Test(char* testName, char* str, bool expected) +void Test(const char* testName, const char* str, bool expected) { if (testName != NULL) printf("%s begins: ", testName); @@ -89,6 +68,7 @@ void Test(char* testName, char* str, bool expected) printf("FAILED.\n"); } + int main(int argc, char* argv[]) { Test("Test1", "100", true);