#include<iostream>
using namespace std;
int getlen(char *s)
{
int n;
for(n = 0; *s != '\0'; s++)
n++;
return n;
}
void reverse(char s[])
{
char c,i,j;
for(i = 0,j = getlen(s) - 1; i < j; i++,j--)
{
c = s[i];
s[i] = s[j];
s[j] = c;
}
}
void my_itoa(int n,char s[])
{
int i=0,sign;
if((sign = n) < 0)
n = -n;
do
{/*以反序生成数字*/
s[i++] = n%10 + '0';/*get next number*/
}while((n /= 10) > 0);/*delete the number*/
if(sign < 0)
s[i++] = '-';
s[i] = '\0';
reverse(s);
}
void itochar(int num)
{
int i = 0;
int j ;
char stra[10];
char strb[10];
while ( num )
{
stra[i++]=num%10+48;
num=num/10;
}
stra[i] = '\0';
for( j=0; j < i; j++)
{
strb[j] = stra[i-j-1];
}
strb[j] = '\0';
cout<<strb<<endl;
}
int main()
{
int num;
cout<<"please input your number"<<endl;
cin>>num;
cout<<"your input is "<<num<<endl;
char b[16]={0};
my_itoa(num,b);
cout<<"after my_itoa(num,b);"<<b<<endl<<endl;
itochar(num);
}
不用库函数,用C语言实现将一整型数字转化为字符串
最新推荐文章于 2023-12-07 22:58:47 发布