当前位置:网站首页>[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;
}
边栏推荐
- Use abp Zero builds a third-party login module (III): web side development
- How to play a data mining game entry Edition
- New developments in Data Governance: what is the impact of the EU's Data Governance Research Report on China
- Sword finger offer 54. the k-th node of the binary search tree
- 二叉搜索树(DAY 75)
- Detailed explanation of arm instruction CMP
- ROI pooling and ROI align
- VBA common objects
- The code spell checker plug-in avoids some specific vocabulary errors "XXX": unknown word.cspell
- Blocking Queue Analysis
猜你喜欢

Baidu, Alibaba, Tencent, who fell first?
![[daily practice] day (14)](/img/83/d924fa0fc5ae01d151a0880da62f7e.png)
[daily practice] day (14)

How to play a data mining game entry Edition

Pdf snapshot artifact

Easy to understand: basic knowledge of MOS tube

R奇怪语法总结
![[unity3d] ugui callback function](/img/6f/312e7f2cf76fa932e66c5ba0737219.png)
[unity3d] ugui callback function

EOL offline sequence based on iso13209 (Otx)

"Everyday Mathematics" serial 61: March 1

Brief introduction of acoustic filter Market
随机推荐
MySQL中建表时 pk、nn、qu、b、un、zf、ai、g代表的意思
Solve the problem of invalid modification of QT 5 interface. Solve the problem of invalid modification of qtdesigner
函数模板学习记录
Temperature table lookup and calculation formula
VB variable types and control statements (basic)
Dry goods | training AI model can't find data? Collect 20 selected open source communities!
C#控件开源库:MetroFramework的下载
Quick sort code implementation
Android interview question: why do activities rebuild ViewModel and still exist—— Jetpack series (3)
Use abp Zero builds a third-party login module (4): wechat applet development
target_ compile_ features specified unknown feature “cxx_std_14“ for target
Keilc51 usage details (III)
Sword finger offer 32 - I. print binary tree from top to bottom
The code of Keil and Si compiler is not aligned??
嵌入式c语言开发之宏定义求两个数的最大值的使用技巧
Unity Animator动画与状态机
Interlocked atom access series of functions
How to troubleshoot the problem of too many inodes
ARM裸板调试之JTAG调试源码级调试
CRC8 CRC16 table lookup method