"Offoffer" indicates a numeric string and a numeric string.
[Disclaimer: All Rights Reserved. indicate the source for reprinting. Do not use it for commercial purposes. Contact mailbox: libin493073668@sina.com]
Question link: http://www.nowcoder.com/practice/6f8c901d091949a5837e24bb82a731f2? Rp = 3 & ru =/ta/coding-interviews & qru =/ta/coding-interviews/question-ranking
Description
Implement a function to determine whether a string represents a value (including an integer and a decimal number ). For example, strings "+ 100", "5e2", "-123", "3.1416", and "-1E-16" both represent numerical values. But neither "12e", "1a3. 14", "1.2.3", "+-5" or "12e + 4.3.
Ideas
There is nothing special about it. We only need to make various judgments.
class Solution{public:bool isNumeric(char* string){if(string==nullptr)return false;if(*string=='+' || *string=='-')++string;if(*string=='\0')return false;bool judge = true;scanDigits(&string);if(*string!='\0'){if(*string=='.'){++string;scanDigits(&string);if(*string=='e' || *string=='E')judge = isExponential(&string);}else if(*string=='e' || *string=='E')judge = isExponential(&string);elsejudge = false;}return judge && (*string=='\0');}void scanDigits(char **string){while(**string!='\0' && **string>='0' && **string<='9')++(*string);}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');}};
Copyright Disclaimer: This article is the original article of the blogger. If it is reproduced, please indicate the source