In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
This article introduces the relevant knowledge of "how to verify the numbers on C++". In the operation of actual cases, many people will encounter such a dilemma. Then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Valid Number validate digit
Validate if a given string can be interpreted as a decimal number.
Some examples:
"0" = > true
"0.1" = > true
"abc" = > false
"1 a" = > false
"2e10" = > true
"- 90e3" = > true
"1e" = > false
"E3" = > false
"6e-1" = > true
"99e2.5" = > false
"53.5e93" = > true
"--6" = > false
"- + 3" = > false
"95a54e53" = > false
Note: It is intended for the problem statement to be ambiguous. You should gather all requirements up front before implementing one. However, here is a list of characters that can be in a valid decimal number:
Numbers 0-9
Exponent-"e"
Positive/negative sign-"+" / "-"
Decimal point-"."
Of course, the context of these characters also matters in the input.
Update (2015-02-10):
The signature of the C++ function had been updated. If you still see your function signature accepts a const char * argument, please click the reload button to reset your code definition.
First of all, it can be analyzed from some examples given in the title that we need to pay attention to special characters other than numbers, such as space'", decimal point". ", the natural number" eple E ", and the plus or minus sign" + /-". In addition to these characters need to consider the accident, there are any other characters, you can immediately determine that it is not a number. Let's take a look at these special characters that appear or may be numbers:
1. Spaces'": spaces are divided into two situations to consider, one is the space that appears at the beginning and the end, and the other is the character that appears in the middle. The space that appears at the beginning and the end does not affect the number, but once there is a space in the middle, it is not a number immediately. Solution: remove the first space of the character during preprocessing, and then detect the space in the middle, then determine that it is not a number.
two。 Decimal point ".": the decimal point needs to be divided more often, first of all, the decimal point can appear only once, but the decimal point can appear anywhere, the beginning (".3"), the middle ("1.e2"), and the end ("1."), and it should be noted that the decimal point can not appear after the natural number "eUniver E", such as "1e.1" false, "1e1.1" false. Also, when the decimal point is at the end, it must be preceded by a number, such as "1." True, "-. False . Solution: discuss the situation separately in the beginning, the middle and the end.
3. Natural number "e false E": there must be numbers before and after natural numbers, that is, natural numbers cannot appear at the beginning and end, such as "e" false, ".e1" false, "3.e" false, "3.e1" true. And the decimal point can only appear before the natural number, and there is that the natural number can not be preceded by a symbol, such as "+ E1" false, "1 decimal" false. Solution: discuss the situation separately in the beginning, the middle and the end.
4. Positive and negative sign "+ / -", positive and negative sign can appear again at the beginning, can appear after the natural number e, but can not be the last character, followed by a number, such as "+ 1.e+5" true. Solution: discuss the situation separately in the beginning, the middle and the end.
Let's officially discuss the situation in three positions at the beginning, the middle and the end:
1. Pre-processing before discussing the three positions, removing the spaces at the beginning and end of the string, you can use two pointers to point to the beginning and the end, respectively, skip when you encounter spaces, and point to characters that are not spaces at the beginning and end.
two。 For first character processing, the first character can only be a number or the plus or minus sign "+ / -". We need to define three flag before indicating whether we have detected a decimal point, a natural number and a plus or minus sign. If the first character is a number or a plus or minus sign, mark the corresponding flag, if not, return false directly.
3. When dealing with intermediate characters, there are five situations for intermediate characters: numbers, decimal points, natural numbers, plus or minus signs and other characters.
If it is a number, mark flag and pass.
If it is a natural number, it must be the first time that a natural number occurs, and the previous character cannot be a positive or negative sign, and a number must have appeared before to mark flag to pass.
If the sign is plus or minus, the preceding character must be the natural number e to mark flag through.
If it is a decimal point, it must be the first time that the decimal point appears and the natural number does not appear before you can mark flag to pass.
If anything else, return false.
4. For trailing character processing, the last character can only be a number or decimal point, and all other characters return false.
If it is a number, return true.
If the decimal point, it must be the first decimal point and the natural number has not appeared, and must be preceded by a number to return true.
Solution 1:
Class Solution {public: bool isNumber (string s) {int len = s.size (); int left = 0, right = len-1; bool eExisted = false; bool dotExisted = false; bool digitExisited = false; / / Delete spaces in the front and end of string while (s [left] = = ") + left; while (s [right] =")-right / / If only have one char and not digit, return false if (left > = right & & (s [left])
< "0" || s[left] >"9") return false; / / Process the first char if (s [left] = ".") DotExisted = true; else if (s [left] > = "0" & & s [left] = "0" & & s [I]
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.