Enter a string representing an integer to convert the string to an integer and output. For example, the input string "345", the output integer 345.

Source: Internet
Author: User

    1. 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;
    2. 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.

  

    1. 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;
    2. 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).
    3. 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.

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.