"C Language" simulation implements ATOI function

Source: Internet
Author: User

Atoi (which means ASCII to integer) is a function that converts a string into an integer number.

The Atoi () function scans the parameter nptr string, skips the preceding white space character (such as a space, tab indent, and so on, which can be detected by the Isspace () function), until a number or sign is encountered, and the conversion is not completed until the non-digit or string ends (' + '). and returns the result. If nptr cannot be converted to int or nptr is an empty string, then 0 is returned

When we simulate the implementation of the Atoi function, we should pay attention to the following points:

1. Whitespace issues before strings

2. Positive and negative sign

3. When the string is empty

4. The number being converted is too large (positive overflow, negative overflow)

5. Other, unable to convert the case (all the letters ....) or the like)

After we understand the functions of the Atoi function and some considerations, start the simulation to implement it, the code is as follows:

#include <stdio.h> #include <assert.h> #include <stdlib.h>enum{vaild = 0,invaild = 1};int flag = vaild; int my_atoi (const char *str) {long long ret = 0;int symbol = 1;//assertion Str!=nullassert (str);//Judge null character if (' + = = *str ') {flag = in Vaild;return 0;} Remove spaces, tabs while (Isspace (*STR)) {str++;} The sign bit determines if ('-' ==*str ') {symbol = -1;str++;} else if (' + ' ==*str) {str++;} else if (((*str<= ' 0 ') && (*str>= ' 9 ')) {flag = Invaild;return 0;} Other exception cases are processed, start converting while ((*str!= ') && (*str>= ' 0 ') && (*str<= ' 9 ')) {ret = (ret*10 + *str-' 0 '); str ++;}    With sign bit RET *= symbol;//detection overflow//int 0111 1111 1111 1111 1111 1111 1111 1111 positive overflow//7 F F f f f f f// 1000 0000 0000 0000 0000 0000 0000 0000 Negative Overflow//8 0 0 0 0 0 0 0if ((ret>0x7fffffff) &&amp ;(1==symbol)) | | (ret< (signed int) 0x80000000) && ( -1==symbol)) {flag = Invaild;return 0;} RET legal flag = Vaild;return ret;} Print Atoi function State +\nvoid printstate () {if (flag) {printf ("Exception \ n");} else{printf ("normal \ n");}} Test function void Funtest () {printf ("value=%d,state=", My_atoi ("123456789")); Printstate ();//Normal printf ("value=%d,state=", My_atoi ("123456789")); Printstate ();//Normal printf ("value=%d,state=", My_atoi (" -123456789sassa")); Printstate ();//Normal, Encounter letter termination printf ("value=%d,state=", My_atoi (" -123456789sassa")); Printstate ();//Normal, preceded by a space printf ("value=%d,state=", My_atoi ("")),//////////////abnormal situation/////////printf ("\ n"); Printstate ();//exception: empty string printf ("Value=%d,state=", My_atoi ("123456789123456789"), flag); Printstate ();//exception: Overflow printf ("value=%d,state=", My_atoi (" -123456789123456789"), flag); Printstate ();//exception: Negative overflow printf ("value=%d,state=", My_atoi ("DASDSA"), flag); Printstate ();//exception: Cannot convert}int main () {funtest (); return 0;}

  easter Egg: When writing a test function, there is one thing that always has to be solved, and at first I wrote the test code like this :

void Funtest () {printf ("value=%d,state=%d\n", My_atoi ("123456789"), flag);//Normal printf ("value=%d,state=%d\n", My_atoi (" -123456789"), flag);//Normal printf ("value=%d,state=%d\n", My_atoi (" -123456789sassa"), flag);//Normal, Encounter letter termination printf (" Value=%d,state=%d\n ", My_atoi ("  -123456789sassa "), flag);//Normal, preceded by a space of printf (" \n\n\n ");p rintf (" value=%d,state=% D\n ", My_atoi (" "), flag);//exception: empty string printf (" value=%d,state=%d\n ", My_atoi (" 123456789123456789 "), flag);//exception: is overflow printf ("value=%d,state=%d\n", My_atoi (" -123456789123456789"), flag);//exception: Negative overflow printf ("value=%d,state=%d\n", My_ Atoi ("DASDSA"), flag);//exception: cannot convert}

When testing, I found something strange!!!!!!!

The result output is this:

Value=123456789,state=0value=-123456789,state=0value=-123456789,state=0value=-123456789,state=0value=0,state=0    //Bedding!!! Why 0?value=0,state=1value=0,state=1value=0,state=1?

I also deliberately follow the body of the function to see that the global variable flag has indeed been changed to 1, but why the output is 0???

Just when I was baffled, it occurred to me that the calling convention of the printf function was _cdel!!!!

Therefore, the_cdel calling convention is to press the argument from right to left, so it pushes the flag stack and then executes the My_atoi function, modifying the global variable flag in the My_atoi function....

So, the final output is 0!

That's why the final code is there!

  

"C Language" simulation implements ATOI function

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.