当前位置:网站首页>[sword finger offer] analog implementation ATOI
[sword finger offer] analog implementation ATOI
2022-07-25 06:25:00 【Life is made by oneself ~】
atoi The implementation of the
One 、 Usage mode
int atoi( const char *string );
atoi What we achieve is String reshaping The operation of :
int main()
{
char arr1[20] = "123456";
char arr2[20] = "-123456";
char arr3[20] = "12abc3456";// Non numeric stop conversion encountered
int ret1 = atoi(arr1);
int ret2 = atoi(arr2);
int ret3 = atoi(arr3);
printf("%d %d %d", ret1, ret2, ret3);
return 0;
}

Now let's simulate the implementation :
Two 、 Simulation Implementation
2.1 Regular conversion
Suppose you want to put the string "123456" Convert to plastic :
int ret = 0;
while (*str)
{
ret = ret * 10 + *str - '0';
str++;
}
2.2 Special situations to deal with
1️⃣ Null pointer
2️⃣ An empty string
3️⃣ Space
4️⃣ The sign
5️⃣ Transboundary
6️⃣ Nonnumeric character
An empty string :
When we pass an empty string , What should we return ?
If you return 0, That passes characters 0 What to do when ?
So we should judge whether it is legal .
We can create an enumeration variable to judge
enum status
{
VALID,// 0
INVALID// 1
}sta = INVALID;
sta Global variable , Assume first that it is illegal , If the legitimate , Change later .
if (*str == '\0')
{
return 0;// illegal 0
}
Space :
If you encounter spaces in Start of string Will automatically skip , If it is in the middle of the string, it is 6️⃣ In this case , Just skip .
if (isspace(*str))// Judge whether it is a character '0'
{
str++;
}
The sign :
int flag = 1;
if (*str == '+')
{
str++;
}
else if(*str == '-')
{
flag = -1;
str++;
}
Nonnumeric character :
while (*str)
{
if (isdigit(*str))// Determine whether it is a numeric character
{
ret = ret * 10 + *str - '0';
}
else
{
return ret;
}
str++;
}
Cross border judgment :
To judge whether to cross the line is to see ret Is it greater than INT_MAX, For a negative number, it is less than INT_MIN, it is to be noted that int Is never more than INT_MAX, Should use the long long
if (isdigit(*str))// Determine whether it is a numeric character
{
ret = ret * 10 + flag * (*str - '0');
if (ret > INT_MAX || ret < INT_MIN)
{
return 0;
}
}
else
{
return (int)ret;
}
Full code :
enum status
{
VALID,// 0
INVALID// 1
}sta = INVALID;
int my_atoi(const char* str)
{
int flag = 1;
assert(str);
if (*str == '\0')
{
return 0;// illegal 0
}
if (isspace(*str))// Judge whether it is a character '0'
{
str++;
}
if (*str == '+')
{
str++;
}
else if(*str == '-')
{
flag = -1;
str++;
}
long long ret = 0;
while (*str)
{
if (isdigit(*str))// Determine whether it is a numeric character
{
ret = ret * 10 + flag * (*str - '0');
if (ret > INT_MAX || ret < INT_MIN)
{
return 0;
}
}
else
{
return (int)ret;
}
str++;
}
if (*str == '0')
{
sta = VALID;
}
return (int)ret;
}
int main()
{
char arr[200] = "123abc45";
int ret = my_atoi(arr);
if (sta == INVALID)
{
printf(" Illegal return : %d", ret);
}
else if (sta == VALID)
{
printf("%d", ret);
}
return 0;
}
边栏推荐
猜你喜欢

SAP FICO section III BDC and ltmc import S4 financial account

The code of Keil and Si compiler is not aligned??

The LAF protocol elephant of defi 2.0 may be one of the few profit-making means in your bear market

Unity animator animation and state machine

DOM event type

Design of automatic machine dot drawing script based on C language
![[C language] document processing and operation](/img/d7/3d34401f78399dcd6d571bc0bc84bf.png)
[C language] document processing and operation

【剑指Offer】模拟实现atoi
![[ultra detailed diagram] FPN + mask RCNN](/img/ef/ddd62fe7e54074c134aa5ee4cc5840.png)
[ultra detailed diagram] FPN + mask RCNN

Do you know the same period last year in powerbi
随机推荐
Classic cases of static keywords and block blocks
In depth analysis: is the hottest business model in 2022 linked by 2+1 a legal model?
Using JS to realize the linkage effect of form form's secondary menu
Temperature table lookup and calculation formula
Learning notes: detailed use of 12864 LCD module
Machine learning keras fitting sine function
Req.body in node.express is always undefind
It is said that screentogif is a GIF recording artifact, but I don't know that its strength is far from here
Pic16f877xa instruction system (assembly language)
Keilc51 usage details (III)
Baidu SEM bidding avoidance
2022 "strong country Cup" preliminary WP (with script and detailed process)
【Node】服务端口被占用Error: listen EADDRINUSE: address already in use :::9000-如何关闭node启动的端口
Openzeppelin scalable template library contract initialization details
Mysql database backup and recovery
node.express中req.body总是undefind解决
Dry goods | training AI model can't find data? Collect 20 selected open source communities!
Easy to understand: basic knowledge of MOS tube
VO, dto, do, Po distinction and use
二叉搜索树(DAY 75)