函數實現的過程我貼出來
#include <iostream>
#include <math.h>
#include <stdlib.h>
using namespace std;
//整數轉換為字符串的函數
char * num_to_str(int ival)
{
char *str = new char[50];
int len =0;
int temp = abs(ival);
char ichar [50];
while(temp!=0)
{ichar[len]=temp%10+48;
len++;
temp/=10;
}
ichar[len]='\0';
int i;
if(ival == 0)
{str[0]='0';
i=1;
}
else if(ival < 0)
{str[0]='-';i=1;len++;}
else i=0;
for(;i<len;i++)
str[i] = ichar[len-i-1];
str[i]='\0';
return str;
}
int main()
{
cout<<num_to_str(0)<<endl;
cout<<num_to_str(12345)<<endl;
cout<<num_to_str(-123)<<endl;
system("pause");
return 0;
}