#include <stdio.h>
#include <assert.h>
int atoi(const char *str)
{
int signal=1;
int result=0;
assert(str != NULL);
if((*str>='0'&& *str<='9') || *str=='-' || *str=='+')
{
if(*str=='-' || *str=='+')
{
if(*str=='-')
{
signal=-1;
}
str++;
}
}
else
{
return 0;
}
while(*str!='\0')
{
char c=*str;
if(c>='0'&& c<='9')
{
int temp=c-'0';
result=temp+result*10;
str++;
}
}
return signal*result;
}
int main(void){
//const char *str = "+12467";//12467
//const char *str = "-12345";//-12345
//const char *str = "";//0
//const char *str = "+12345";//12345
const char *str = NULL;
printf("the result is:%d\n",atoi(str));
return 0;
}
atoi函数实现
最新推荐文章于 2024-09-11 21:49:54 发布