//atoi的實現
代碼如下:
#include<iostream>
using namespace std;
int atio1(char *s)
{
int sign=1,num=0;
if(*s=='-')
sign=-1;
s++;
while((*s)!='\0')
{
num=num*10+(*s-'0');
s++;
}
return num*sign;
}
//itoa的實現
代碼如下:
char *itoa(int num, char *str, int radix)
{
char* ptr = str;
int i;
int j;
while (num)
{
*ptr++ = string[num % radix];
num /= radix;
if (num < radix)
{
*ptr++ = string[num];
*ptr = '\0';
break;
}
}
j = ptr - str - 1;
for (i = 0; i < (ptr - str) / 2; i++)
{
int temp = str[i];
str[i] = str[j];
str[j--] = temp;
}
return str;
}
代碼如下:
int main()
{
char *s="-123567890";
cout<<atio1(s);
system("pause");
}