当前位置:网站首页>[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;
}
边栏推荐
- Addition, deletion, modification and query of DOM elements
- R strange grammar summary
- 日期(DAY 76)
- JS gets the text selected by the mouse and is in the selected state
- Mlx90640 infrared thermal imager temperature measurement module development notes (I)
- Baidu, Alibaba, Tencent, who fell first?
- The code spell checker plug-in avoids some specific vocabulary errors "XXX": unknown word.cspell
- 【datawhale202207】强化学习:策略梯度和近端策略优化
- Sword finger offer 54. the k-th node of the binary search tree
- 都说ScreenToGif是GIF录制神器,却不知其强大之处远不在此
猜你喜欢
![[C language] in depth understanding of pointers and arrays (phase I)](/img/4b/26cf10baa29eeff08101dcbbb673a2.png)
[C language] in depth understanding of pointers and arrays (phase I)

4、 MFC toolbar, runtime class information mechanism, runtime creation mechanism

10. Rendering Basics

Use abp Zero builds a third-party login module (4): wechat applet development

DOM event type

Keil uvisin5 code auto completion or code Association
![[daily practice] day (14)](/img/83/d924fa0fc5ae01d151a0880da62f7e.png)
[daily practice] day (14)

Easy to understand: basic knowledge of MOS tube

It is said that screentogif is a GIF recording artifact, but I don't know that its strength is far from here

Sword finger offer 36. binary search tree and bidirectional linked list
随机推荐
Seekbar attribute reference
Review of three traversal methods of map
Typedef usage and template
嵌入式c语言开发之宏定义求两个数的最大值的使用技巧
Cout format output common functions and flags summary
VO, dto, do, Po distinction and use
[ultra detailed diagram] FPN + mask RCNN
Draw Bezier curve through screen interaction
Multithreading programming under Win32 API
Baidu, Alibaba, Tencent, who fell first?
Developers must read: 2022 mobile application operation growth insight white paper
JS 获取鼠标选中的文字并处于选中状态
Netease game Flink SQL platform practice
Sword finger offer 54. the k-th node of the binary search tree
The code spell checker plug-in avoids some specific vocabulary errors "XXX": unknown word.cspell
VBA common objects
Bubble sort code implementation
The most comprehensive multi-threaded application tutorial - summary in detail
Define usage method and template
DOM events