String to integer

Source: Internet
Author: User

 1 class Solution { 2     public: 3         //计算一个整数的位数,在溢出判断中使用 4         int bit_length(int n)    { 5             int l=0; 6             while(n!=0){ 7                 l++; 8                 n/=10; 9             }10             return l;11         }12     int atoi(const char *str){13         if(*str==‘\0‘) return 0;14         // 将开头的空白字符和0去掉15         while(isspace(*str) || *str-‘0‘ ==0 )str++;16         int number=0;17         bool times_minus=false;//是否是负数18         bool symbol=false; //是否违法19         bool of=false;  //是否溢出20         while(*str!=‘\0‘){21             //+号处理22             if(*str==‘-‘)    {23                 if(((*(str+1)==‘\0‘ )&&number!=0)|| !isdigit(*(str+1))){24                     symbol=true;25                     break;26                 }27                 times_minus=true;28             }29             //-号处理30             else if(*str==‘+‘){31                  if(((*(str+1)==‘\0‘)&&number!=0) || !isdigit(*(str+1))){32                      symbol=true;33                      break;34                      35                  }36               }37             //数字处理38             else if(isdigit(*str)){   39                 if(*str-‘0‘==0 && number==0)40                 {}41                 else{42                     int num_copy=number;43                     number=number*10+*str-‘0‘;44                     //溢出判断45                     if(bit_length(number)==bit_length(num_copy)){46                         of=true;47                     }48                 }49                 50             }51             //其他字符处理52             else 53                 break;54 55             str++;56         }    57     58         if(times_minus==true)59             number*=-1;60         //负溢出            61         if (( of&&times_minus==true )|| (number>0 && times_minus==true))62             number=-2147483648;63         //正溢出64         else if((of &&times_minus==false)|| (number<0 && times_minus==false))65             number=2147483647;66             67         if(symbol)68             return 0;69         else return number;70     }71 };

It is easy to convert a string into an integer, but here we want to handle the overflow problem. For details about integer overflow, refer to csapp (Khan, basically, the number of references is csapp, SiCp, and CLRS... However, these books are all excellent books ).
Question requirements

1. A string can start with a blank character;

2. If an invalid character appears in the string, it is truncated;

3. the plus and minus signs can only appear at the beginning of a number and have only one;

4. After the number overflows, it is represented by a 32-bit integer. This step is important. I combined the number of digits transformation and the symbol of the operand to make a comprehensive judgment.

5. the number may start with 0 and needs to be filtered out.

String to integer

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.