- Method One: Using the function atoi (const char *nptr) in the C function library, a line of code can solve the problem, but this is not the question of the solution to the idea;
- Method Two: If we enter the "345", from left to right in turn, the first time the result is "3", the second time the result is "34", 34=3*10+4, the third time the result is "345", 345=34*10+5. Thus, we can iterate through each character from left to right, *10+ the result of the previous one, and use a loop to turn a string into an integer.
- If the first character in the string is "-", then a negative integer is obtained. Therefore, you need to first determine whether the first character is "-", if it is, then the final result is multiplied by-1;
- If there is an illegal input, there is a character in the string that is not between "0" ~ "9". If an illegal input character is encountered, it exits the conversion operation and returns a Boolean value of 0 (the input string may also be 0, in order to differentiate, you can also print a single hint statement).
- If you enter a string that is long, it may overflow. If it is a positive overflow, it is greater than the largest integer and, if it is a negative overflow, less than the smallest integer. Use
if (Result > Numeric_limits<int>::max () &&!isnegative) | | (Result < Numeric_limits<int>::min () && isnegative))
To judge. It also exits the conversion operation directly after overflow.
#include <iostream>
#include <string>
#include <limits>
using namespace Std;
BOOL Validinput = false;//indicates an illegal input
int strtoint (const char *str) {
BOOL Invalid
BOOL isnegative = false;//used to determine whether a negative number is a flag
Long result = 0;//is used to hold the final result
const char *digit = str;
if (digit! = NULL) {
if (*digit = = ' + ') {//To determine if it is a positive number
Isnegative = false;//does not need to be re-assigned
digit++;
}
else if (*digit = = '-') {//To determine if it is a negative number
Isnegative = true;
digit++;
}
while (*digit! = ')} {
if (*digit >= ' 0 ' && *digit <= ' 9 ') {
result = Result*10 + (*digit-' 0 ');//Get a number
if (Result > Numeric_limits<int>::max () &&!isnegative) | | (Result < Numeric_limits<int>::min () && isnegative)) {
result = processing after overflow of 0;//
Break
}
digit++;
}
else{//encounters illegal characters, stops calculation, exits directly
result = 0;
Break
}
}
if (*digit = = ')} {//If the last character is reached, it is a valid input.
Validinput = true;
if (isnegative) {
result = ( -1) *result;//or result =0-result
}
}
}
return result;
}
int main (void) {
cout<< "Enter A string:" <<endl;
Char *mystring = new CHAR[100];
cin>>mystring;
cout<< "After convert,the result is:" <<endl;
Cout<<strtoint (mystring) <<endl;
cout<< "The status of the string is" <<validInput<<endl;
Delete[] mystring;//free memory space
System ("pause");
return 0;
}
The
enters a string that represents an integer, converts the string to an integer, and outputs it. For example, the input string "345", the output integer 345.