1.注意类型转化
2.负号的处理
3.str最后加'\0'
#include<iostream>
using namespace std;
using namespace std;
void intstr(int n, char *str)
{
char buf[10] = " ";
int i = 0;
int len = 0;
int temp = n < 0 ? -n : n; //temp为n的绝对值
if (str == NULL)
{
return;
}
{
char buf[10] = " ";
int i = 0;
int len = 0;
int temp = n < 0 ? -n : n; //temp为n的绝对值
if (str == NULL)
{
return;
}
while (temp)
{
buf[i++] = (temp % 10)+'0'; //类型转化
temp = temp / 10;
}
//复数多一位存符号
len = n < 0 ? ++i : i;
str[i] = '\0';
while (1)
{
i--;
if (buf[len - i - 1] == 0)
{
break;
}
str[i] = buf[len - i - 1];//逆序拷贝到str中
}
if (i == 0)
{
str[i] = '-';
}
}
{
buf[i++] = (temp % 10)+'0'; //类型转化
temp = temp / 10;
}
//复数多一位存符号
len = n < 0 ? ++i : i;
str[i] = '\0';
while (1)
{
i--;
if (buf[len - i - 1] == 0)
{
break;
}
str[i] = buf[len - i - 1];//逆序拷贝到str中
}
if (i == 0)
{
str[i] = '-';
}
}
int main()
{
int num;
char p[10];
{
int num;
char p[10];
cout << "shuru yige shu" << endl;
cin >> num;
cout << "output" << endl;
intstr(num, p);
cout << p<< endl;
cin >> num;
cout << "output" << endl;
intstr(num, p);
cout << p<< endl;
cout << "hello......" << endl;
system("pause");
return 0;
}
system("pause");
return 0;
}