atoi函数--字符串转换为整型
int main()
{
char *p= "1234";
int ret = atoi(p);
printf("%d", ret);
return 0;
}
输出结果为整型值:1234
接下来我们自己来实现一个atoi函数:
首先我们得考虑到: //my_atoi函数
//1.非数字字符串
//2.空格、空字符串
//3.正负号
//4.超出范围
enum judgement //枚举便于区分是否错误
{
incorrect, //0
correct //1
};
enum judgement judge = incorrect; //默认0都是错误
先设一个枚举类型,并定义为默认0都错误信息
int my_atoi(const char* ps)
{
if (NULL == ps) //判断空指针
return 0;
if ('\0' == *ps) //判断空字符串
return 0;
while (isspace(*ps)) //判断空格
{
ps++;
}
int flag = 1; //判断正负
if (*ps == '-')
flag = -1;
ps++;
int num = 0; //判断是否数字
while (*ps)
{
if (isdigit(*ps))
{
num = num * 10 + flag * (*ps - '0');
ps++;
}
else
return num;
}
judge = correct;
return num;
}
int main()
{
char* p = " -1234";
int ret = my_atoi(p);
if (judge == correct)
{
printf("正确显示: %d\n", ret);
}
else if (judge == incorrect)
{
printf("错误显示: %d\n", ret);
}
return 0;
}
输出结果为:"正确显示:-1234" ,如果*p是" -1234a",结果将是"错误显示:-1234";