Atoi implementation, atoi implementation

Source: Internet
Author: User

Atoi implementation, atoi implementation

The processing policy completely imitates C language library functions

Overflow handling policy:

Output upper or lower bound (2147483647 and-2147483648)

Test data:

Char * s1 = "\ t \ f \ v \ n \ r-00100 \ n \ t \ f \ v \ n \ r1234"; char * s2 = "-- 099 "; char * s3 = "nas"; char * s4 = "+ 2147483647sc"; char * s5 = "20.0sc"; char * s6 = "20-1sc "; char * s7 = "0-099"; char * s8 = "-9999999999999999999"; // overflow char * s9 = "+ 9999999999999999999 ";

Processing result:

The first behavior is the library function result, and the second behavior is the My_atoi result.




Code implementation:

# Pragma once # include <iostream> using namespace std; int My_atoi (const char * pstr) // The content pointed to by the pointer cannot be changed, the pointer cannot be changed to char * const pstr {if (! Pstr) {printf ("Pointer is NULL \ n"); return 0;} unsigned int result = 0; // use unsigned to handle int sign = 1; // The flag. The default value is int out = false. // aoti () requires that the first digit or symbol must be a number with the last digit or symbol, when the first digit or symbol is met, the value out = true, and then the non-digit exit while (* pstr) {if (* pstr> '9' | * pstr <'0') // if (out) return result * sign is returned if a non-number is returned after conversion; if (* pstr = '-') {sign =-1; out = true;} else if (* pstr = '+') out = true; else if (* pstr <= '9' & * pstr> = '0') {result = res Ult * 10 + * pstr-'0'; // returns the upper and lower bounds if (result> = 2147483647 & sign = 1) return 2147483647; if (result> = 2147483648 & sign =-1) // if result * sign> 2147483648 is written, the binary value of 2147483648 is automatically expressed (unsigned ), // convert the expression to a signed integer of 100000 after comparison with int... that is,-2147483648. Therefore, the unsigned int is used here. // If int is used, the value 2147483648 is changed to-2147483648 for comparison, and the maximum value of int Is 2147483647. Even if it remains unchanged, the return-2147483647-1 is not applied; // cannot be written as-2147483648out = true;} else if (* pstr! = ''& * Pstr! = '\ N' & * pstr! = '\ R' & * pstr! = '\ T' & * pstr! = '\ V' & * pstr! = '\ F') // return result * sign; pstr ++;} return result * sign;} void main () {char * s1 = "\ t \ f \ v \ n \ r-00100 \ n \ t \ f \ v \ n \ r1234"; char * s2 = "-- 099 "; char * s3 = "nas"; char * s4 = "+ 2147483647sc"; char * s5 = "20.0sc"; char * s6 = "20-1sc "; char * s7 = "0-099"; char * s8 = "-9999999999999999999"; // overflow char * s9 = "+ 9999999999999999999"; int i1 = atoi (s1 ); int i2 = atoi (s2); int i3 = atoi (s3); int i4 = atoi (s4); int i5 = atoi (s5); int i6 = atoi (s6 ); int i7 = atoi (s7); int i8 = atoi (s8); int i9 = atoi (s9 ); cout <i1 <''<i2 <'' <i3 <''<i4 <'' <i5 <''<i6 <<''<i7 <'' <i8 <''<i9 <endl; i1 = My_atoi (s1); i2 = My_atoi (s2); i3 = My_atoi (s3); i4 = My_atoi (s4); i5 = My_atoi (s5 ); i6 = My_atoi (s6); i7 = My_atoi (s7); i8 = My_atoi (s8); i9 = My_atoi (s9 ); cout <i1 <''<i2 <'' <i3 <''<i4 <'' <i5 <''<i6 <<''<i7 <'' <i8 <''<i9 <endl; int a = 2147483648; cout <a <endl; //-2147483648 system ("pause ");}



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.