From 36d25bbac95260b8ee8c5fc03314c1a64c37c554 Mon Sep 17 00:00:00 2001 From: Harry He Date: Thu, 10 Sep 2015 16:14:43 -0700 Subject: [PATCH 1/3] Add three test cases: --- NumericStrings/NumericString.cpp | 166 +++++++++++++++++-------------- 1 file changed, 90 insertions(+), 76 deletions(-) diff --git a/NumericStrings/NumericString.cpp b/NumericStrings/NumericString.cpp index 6c60d35..c0faac5 100644 --- a/NumericStrings/NumericString.cpp +++ b/NumericStrings/NumericString.cpp @@ -1,104 +1,118 @@ -/* - * Question Description: - * (Question 12 in ) How do you check whether a string stands for a number or not? - * Numbers include positive and negative integers and floats. For example, strings “+100.”, “5e2”, “-.123”, “3.1416”, and “-1E-16” - * stand for numbers, but “12e”, “1a3.14”, “1.2.3”, “+-5”, and “12e+5.4” do not. +/* +* Question Description: +* (Question 12 in ) How do you check whether a string stands for a number or not? +* Numbers include positive and negative integers and floats. For example, strings "+100.", "5e2", "-.123", "3.1416", and "-1E-16" +* stand for numbers, but "12e", "1a3.14", "1.2.3", "+-5", and "12e+5.4" do not. */ #include -void scanDigits(char** string); +bool scanDigits(char** string); bool isExponential(char** string); bool isNumeric(char* string) { - if(string == NULL) - return false; - - if(*string == '+' || *string == '-') - ++string; - if(*string == '\0') - return false; - - bool numeric = true; - - scanDigits(&string); - if(*string != '\0') - { - // for floats - if(*string == '.') - { - ++string; - scanDigits(&string); - - if(*string == 'e' || *string == 'E') - numeric = isExponential(&string); - } - // for integers - else if(*string == 'e' || *string == 'E') + if (string == NULL) + return false; + + if (*string == '+' || *string == '-') + ++string; + if (*string == '\0') + return false; + + bool numeric = true; + + bool hasDigits = scanDigits(&string); + if (*string != '\0') + { + // for floats + if (*string == '.') + { + ++string; + if (*string == 'e' || *string == 'E') + return false; + if (!hasDigits && *string == '\0') + return false; + + scanDigits(&string); + + if (*string == 'e' || *string == 'E') numeric = isExponential(&string); - else - numeric = false; - } - - return numeric && *string == '\0'; + } + // for integers + else if (*string == 'e' || *string == 'E') + numeric = isExponential(&string); + else + numeric = false; + } + + return numeric && *string == '\0'; } -void scanDigits(char** string) +bool scanDigits(char** string) { - while(**string != '\0' && **string >= '0' && **string <= '9') - ++(*string); + char* pBefore = *string; + + while (**string != '\0' && **string >= '0' && **string <= '9') + ++(*string); + + // return true when there are some digits in string + return *string > pBefore; } bool isExponential(char** string) { - if(**string != 'e' && **string != 'E') - return false; - - ++(*string); - if(**string == '+' || **string == '-') - ++(*string); - - if(**string == '\0') - return false; - - scanDigits(string); - return (**string == '\0') ? true : false; + if (**string != 'e' && **string != 'E') + return false; + + ++(*string); + if (**string == '+' || **string == '-') + ++(*string); + + if (**string == '\0') + return false; + + scanDigits(string); + return (**string == '\0') ? true : false; } // ==================== Test Code ==================== void Test(char* testName, char* string, bool expected) { - if(testName != NULL) - printf("%s begins: ", testName); + if (testName != NULL) + printf("%s begins: ", testName); - if(isNumeric(string) == expected) - printf("Passed.\n"); - else - printf("FAILED.\n"); + if (isNumeric(string) == expected) + printf("Passed.\n"); + else + printf("FAILED.\n"); } int main(int argc, char* argv[]) { - Test("Test1", "100", true); - Test("Test2", "123.45e+6", true); - Test("Test3", "+500", true); - Test("Test4", "5e2", true); - Test("Test5", "3.1416", true); - Test("Test6", "600.", true); - Test("Test7", "-.123", true); - Test("Test8", "-1E-16", true); - Test("Test9", "1.79769313486232E+308", true); - - printf("\n\n"); - - Test("Test10", "12e", false); - Test("Test11", "1a3.14", false); - Test("Test12", "1+23", false); - Test("Test13", "1.2.3", false); - Test("Test14", "+-5", false); - Test("Test15", "12e+5.4", false); - - return 0; + Test("Test1", "100", true); + Test("Test2", "123.45e+6", true); + Test("Test3", "+500", true); + Test("Test4", "5e2", true); + Test("Test5", "3.1416", true); + Test("Test6", "600.", true); + Test("Test7", "-.123", true); + Test("Test8", "-1E-16", true); + Test("Test9", "1.79769313486232E+308", true); + Test("Test10", "179.", true); + + printf("\n\n"); + + Test("Test10", "12e", false); + Test("Test11", "1a3.14", false); + Test("Test12", "1+23", false); + Test("Test13", "1.2.3", false); + Test("Test14", "+-5", false); + Test("Test15", "12e+5.4", false); + Test("Test16", ".", false); + Test("Test17", ".e1", false); + Test("Test18", "+.", false); + + return 0; } From 18bdb32f10d5ba586a0b09b381110ae468859a8e Mon Sep 17 00:00:00 2001 From: Harry He Date: Thu, 10 Sep 2015 16:26:00 -0700 Subject: [PATCH 2/3] Format Code Rename parameter string as str in order to void potential name conflicts, because string is a type name in C++ STL. Test Cases added in the previous change: Test("Test16", ".", false); Test("Test17", ".e1", false); Test("Test18", "+.", false); --- NumericStrings/NumericString.cpp | 169 +++++++++++++++---------------- 1 file changed, 84 insertions(+), 85 deletions(-) diff --git a/NumericStrings/NumericString.cpp b/NumericStrings/NumericString.cpp index c0faac5..076d342 100644 --- a/NumericStrings/NumericString.cpp +++ b/NumericStrings/NumericString.cpp @@ -7,112 +7,111 @@ #include -bool scanDigits(char** string); -bool isExponential(char** string); +bool scanDigits(char** str); +bool isExponential(char** str); -bool isNumeric(char* string) +bool isNumeric(char* str) { - if (string == NULL) - return false; - - if (*string == '+' || *string == '-') - ++string; - if (*string == '\0') - return false; - - bool numeric = true; - - bool hasDigits = scanDigits(&string); - if (*string != '\0') - { - // for floats - if (*string == '.') - { - ++string; - if (*string == 'e' || *string == 'E') - return false; - if (!hasDigits && *string == '\0') - return false; - - scanDigits(&string); - - if (*string == 'e' || *string == 'E') - numeric = isExponential(&string); - } - // for integers - else if (*string == 'e' || *string == 'E') - numeric = isExponential(&string); - else - numeric = false; - } - - return numeric && *string == '\0'; + if (str == NULL) + return false; + + if (*str == '+' || *str == '-') + ++str; + if (*str == '\0') + return false; + + 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; + } + + return numeric && *str == '\0'; } -bool scanDigits(char** string) +bool scanDigits(char** str) { - char* pBefore = *string; + char* pBefore = *str; - while (**string != '\0' && **string >= '0' && **string <= '9') - ++(*string); + while (**str != '\0' && **str >= '0' && **str <= '9') + ++(*str); - // return true when there are some digits in string - return *string > pBefore; + // return true when there are some digits in str + return *str > pBefore; } -bool isExponential(char** string) +bool isExponential(char** str) { - if (**string != 'e' && **string != 'E') - return false; + if (**str != 'e' && **str != 'E') + return false; - ++(*string); - if (**string == '+' || **string == '-') - ++(*string); + ++(*str); + if (**str == '+' || **str == '-') + ++(*str); - if (**string == '\0') - return false; + if (**str == '\0') + return false; - scanDigits(string); - return (**string == '\0') ? true : false; + scanDigits(str); + return (**str == '\0') ? true : false; } // ==================== Test Code ==================== -void Test(char* testName, char* string, bool expected) +void Test(char* testName, char* str, bool expected) { - if (testName != NULL) - printf("%s begins: ", testName); + if (testName != NULL) + printf("%s begins: ", testName); - if (isNumeric(string) == expected) - printf("Passed.\n"); - else - printf("FAILED.\n"); + if (isNumeric(str) == expected) + printf("Passed.\n"); + else + printf("FAILED.\n"); } int main(int argc, char* argv[]) { - Test("Test1", "100", true); - Test("Test2", "123.45e+6", true); - Test("Test3", "+500", true); - Test("Test4", "5e2", true); - Test("Test5", "3.1416", true); - Test("Test6", "600.", true); - Test("Test7", "-.123", true); - Test("Test8", "-1E-16", true); - Test("Test9", "1.79769313486232E+308", true); - Test("Test10", "179.", true); - - printf("\n\n"); - - Test("Test10", "12e", false); - Test("Test11", "1a3.14", false); - Test("Test12", "1+23", false); - Test("Test13", "1.2.3", false); - Test("Test14", "+-5", false); - Test("Test15", "12e+5.4", false); - Test("Test16", ".", false); - Test("Test17", ".e1", false); - Test("Test18", "+.", false); - - return 0; + Test("Test1", "100", true); + Test("Test2", "123.45e+6", true); + Test("Test3", "+500", true); + Test("Test4", "5e2", true); + Test("Test5", "3.1416", true); + Test("Test6", "600.", true); + Test("Test7", "-.123", true); + Test("Test8", "-1E-16", true); + Test("Test9", "1.79769313486232E+308", true); + + printf("\n\n"); + + Test("Test10", "12e", false); + Test("Test11", "1a3.14", false); + Test("Test12", "1+23", false); + Test("Test13", "1.2.3", false); + Test("Test14", "+-5", false); + Test("Test15", "12e+5.4", false); + Test("Test16", ".", false); + Test("Test17", ".e1", false); + Test("Test18", "+.", false); + + return 0; } From 91e7155c385179b470c06353fb8dab43f68327c5 Mon Sep 17 00:00:00 2001 From: cooljacket Date: Wed, 7 Sep 2016 00:38:45 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E7=AE=80=E5=8C=96=E9=9D=A2=E8=AF=95?= =?UTF-8?q?=E9=A2=9854=E7=9A=84=E5=8C=B9=E9=85=8D=E9=80=BB=E8=BE=91?= =?UTF-8?q?=EF=BC=8C=E4=BF=AE=E6=94=B9=E5=8F=82=E6=95=B0=E7=B1=BB=E5=9E=8B?= =?UTF-8?q?=E4=B8=BAconst=20char*...=E4=BD=BF=E5=BE=97g++=E4=B8=8D?= =?UTF-8?q?=E6=8A=A5=E4=B8=80=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);