"Offoffer" indicates a numeric string and a numeric string.

Source: Internet
Author: User

"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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.