/*atoi函数实现*/
#include <stdio.h>
int my_atoi(char *s)
{
int result;
int start,end,sign;
int i, j, temp;
char c;
j = 0;
//去掉头上的空格
while(s[j]==' ')
{
j++;
}
sign = 0;
if(s[j] == '-')
{
sign = 1;//负数
j++;
}
else if(s[j] == '+')
{
sign = 0;//正数
j++;
}
start = j;
while(s[j]!='/0' && s[j] != ' ' && s[j] >= '0' && s[j] <= '9')
{
j++;
}
end = j-1;
result = 0;
for(i=start;i<= end;i++)
{
c = s[i];
temp = c - '0';
result = result*10 + temp;
}
return (sign==1)?(-result):result;
}
int main()
{
char *s = " +12345678 9 ";
printf("%d/n",my_atoi(s));
}
--------------------------------------
写这个,一是为了复习一下基础知识,二是也为各位向找工作的兄弟作一个参考。代码写的简陋,如果有什么错误,请大家指出来,谢谢了。