编写atoi函数
#include <stdio.h>
void my_gets(char *a,int n)
{
int i=0;
while(i<n)
{
if((a[i]=getchar())=='\n')
{
i++;
break;
}
i++;
}
a[i]=0;
}
int my_atoi(char *s)
{
int x=0,y=1;
if(*s=='-')
{
y=-1;
}
if(*s=='-'||*s=='+')
{
s++;
}
while((*s)>='0'&&(*s)<='9')
{
x=x*10+*s-'0';
s++;
}
return y*x;
}
int main()
{
char z[50]={0};
my_gets(z,100);
printf("%d\n",my_atoi(z));
return 0;
}
标签:函数,int,void,atoi,编写,gets
From: https://www.cnblogs.com/yesiming/p/17438827.html