#include <stdio.h>
#include<ctype.h>
#include<LIMITS.H>
int my_atoi(const char* str,int state)
{
int flag = 0;
if (str == NULL)
return 0;
if (*str == '\0')
return 0;
while (isspace(*str))//去除空格
{
str++;
}
if (*str == '+')
{
flag = 1;//正数
str++;
}
else if (*str == '-')
{
flag = -1;//负数
str++;
}
long long ret = 0;
while (isdigit(*str))//调用库函数isdigit,判断*str是否为数字
{
ret = ret * 10 + flag*(*str - '0');
if (ret > INT_MAX || ret < INT_MIN)//ret越界
{
ret = 0;
return (int)ret;
}
str++;
}
if (*str == '\0')
{
state = 0;
return (int)ret;
}
return (int)ret;
}
int main()
{
char*p1 = " -123abc45r67SDF";
int state = 1;
int ret = my_atoi(p1,state);
printf("%s\n", p1);
printf("%d\n", ret);
return 0;
}