当前位置:网站首页>[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;
}
边栏推荐
- Review of some classic exercises of arrays
- Installation and configuration of automatic operation and maintenance management workers ansible
- Case ---- how efficient is the buffer stream compared with the ordinary input stream and output stream?
- VSCode 如何开启多个终端?如何横向显示?
- Cout format output common functions and flags summary
- 10. Rendering Basics
- 【luogu P6629】字符串(Runs)(树状数组)
- Date (day 76)
- Solve the problem of invalid modification of QT 5 interface. Solve the problem of invalid modification of qtdesigner
- It is said that screentogif is a GIF recording artifact, but I don't know that its strength is far from here
猜你喜欢

Easy gene chip SEQ analysis method: practical workflow and advanced applications
This is how the permission system is designed, yyds

VO, dto, do, Po distinction and use

NFT: how to improve rentable NFT (erc-4907)

SAP FICO 第三节 BDC和LTMC导入S4财务科目

Case ---- how efficient is the buffer stream compared with the ordinary input stream and output stream?

Data too long for column ‘data‘ at row 1以及设置成longblob造成的乱码解决。node-mysql

Unity model simplification / consolidation one click plug-in

JSON、

Data too long for column 'data' at row 1 and the garbled code caused by setting to longblob are solved. node-mysql
随机推荐
【transformer】DeiT
Function template learning record
Review of some classic exercises of arrays
Common API of window
What does PK, NN, Qu, B, UN, ZF, AI, G mean when creating tables in MySQL
【luogu P6629】字符串(Runs)(树状数组)
Case ---- how efficient is the buffer stream compared with the ordinary input stream and output stream?
Android interview question: why do activities rebuild ViewModel and still exist—— Jetpack series (3)
Mysql database backup and recovery
深度解析:2022年最火的商业模式链动2+1,是合法模式吗?
ARM裸板调试之JTAG调试源码级调试
Unity animator animation and state machine
Daily question brushing record (XXVIII)
“蔚来杯“2022牛客暑期多校训练营2 Link with Game Glitch (spfa找正负环)
[unity3d] ugui callback function
51 timer initial value calculation
The code spell checker plug-in avoids some specific vocabulary errors "XXX": unknown word.cspell
Dry goods | training AI model can't find data? Collect 20 selected open source communities!
Qt 5界面修改无效的问题解决QtDesigner修改之后无效的解决办法
Data too long for column 'data' at row 1 and the garbled code caused by setting to longblob are solved. node-mysql