[C language] atoi function simulation implementation


1. What is the atoi function?

int atoi ( const char * str );

Function : Convert a string to an integer.
Parses the C string str, interpreting its contents as an integer, which is returned as an int value.

The function first discards as many whitespace characters as possible until the first non-whitespace character is found. Then, starting from this character, take an optional initial plus or minus sign, followed by as many digits as possible, and interpret them as a numeric value. Example: "-123456" is converted to -123456

The string may contain other characters after the characters forming the integer, which are ignored and have no effect on the behavior of the function. Example: "123abc456" is converted to 123, although the final output is an integer, this is an illegal conversion

If the first sequence of non-whitespace characters in str is not a valid integer, or no such sequence exists because str is empty or contains only whitespace characters, no conversion is performed. Example: "abc" " abc" "" is an illegal conversion, and the integer 0 will be output eventually.

string integer legality
" 123456" 123456 legitimate
“-123456” -123456 legitimate
“123abc456” 123 illegal
“abc” 0 illegal
“” 0 illegal
“2222222222” any value illegal

Note: There is no standard specification for what happens when the converted value is outside the range of values ​​that can be represented by int.
Example: The value output in the VS2013 compilation environment
insert image description here

Two, atoi function simulation implementation

#include <stdio.h>
#include <limits.h>
#include <ctype.h>

//通过枚举设置两种状态,分别代表字符串转换的合法性
enum Status
{
    
    
	VALID,  //合法
	INVALID  //非法
};

enum Status status = INVALID; //定义全局变量 status 为 INVALID,若转换合法,则将 status 变为 VALID,若非法则不变

int my_atoi(const char* str)
{
    
    
	if (str == NULL)  //字符串为空
	{
    
    
		return 0;
	}
	if (*str == '\0')  //空白字符
	{
    
    
		return 0;
	}
	while (isspace(*str))  //字符串前面有多余的空格,则一直往后移寻找符号或数字
	{
    
    
		str++;
	}
	int flag = 1;  //flag 标志数字的正负
	if (*str == '+')
	{
    
    
		flag = 1;
		str++;
	}
	else if (*str == '-')
	{
    
    
		flag = -1;
		str++;
	}
	long long ret = 0;
	while (isdigit(*str))
	{
    
    
		ret = ret * 10 + flag*(*str - '0');
		if (ret<INT_MIN || ret>INT_MAX)  //判断转换后的数字是否越界
		{
    
    
			return 0;
		}
		str++;
	}
	if (*str == '\0')  //若字符串遍历完就走这一步,也就意味着该字符串的转换为合法的
	{
    
    
		status = VALID;
		return (int)ret;
	}
	else  //非法转换
	{
    
    
		return (int)ret;
	}
}

int main()
{
    
    
	//int ret = my_atoi("-123");
	int ret = my_atoi("  -2222222222");
	if (status == VALID)
	{
    
    
		printf("合法的转换:%d\n", ret);
	}
	else
		printf("转换不合法!返回值为:%d\n",ret);
	return 0;
}

Summarize

Basically, the details that should be paid attention to in the simulation implementation of this function are in the above code comments.

What needs to be explained again is why the return value should be defined as a long long long integer instead of the int type of the return value of the stoi function?

This is because if there is an out-of-bounds when calculating the sum, since the return value is defined as an int type, the number that has out-of-bounds will be converted to a non-out-of-bounds number, which will lead to wrong output results and Misjudgment of legality.

Guess you like

Origin blog.csdn.net/weixin_47648037/article/details/127337726