#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
int my_atoi(char s[])
{
int i,n,sign;
for(i=0;isspace(s[i]);i++); //跳過空白
sign=(s[i]=='-')?-1:1;
if(s[i]=='+'||s[i]=='-') //跳過符号位
i++;
for(n=0;isdigit(s[i]);i++)
n=10*n+(s[i]-'0'); //将數字字元轉換成整形數字
return sign*n;
}
void my_itoa(int n,char s[])
{
int i,j,sign;
if((sign=n)<0) //記錄符号
n=-n; //使n成為正數
i=0;
do
{
s[i++]=n%10+'0'; //取下一個數字
}while((n/=10)>0); //循環相除
if(sign<0)
s[i++]='-';
s[i]='\0';
for(j=i-1;j>=0;j--) //生成的數字是逆序的,是以要逆序輸出
printf("%c",s[j]);
}
int main()
{
int n;
char str[100];
char s[10];
printf("Please input a string of number:\n");
gets(s);
printf("%d\n",my_atoi(s));
my_itoa(-123,str);
printf("\n");
printf("%d\n",my_atoi("123"));
system("pause");
}