回复 91楼 TonyDeng
恩,而且只能是正数,嘻嘻,
程序代码:#include <stdio.h>
#define NUL ('\0')
#define notZero(x) ((x)!=0)
char *
foo (char *s, unsigned n)
{
int i, i2;
char *p = s;
char buf[32];
i = 0;
do
buf[i++] = '0' + (n%10U);
while ((n/=10U));
i2 = i % 3;
p[0] = buf[i-1];
p[1] = buf[i-2];
p[i2]= ',';
p += i2 + notZero(i2);
i -= i2;
while (i > 2)
{
p[0] = buf[i-1];
p[1] = buf[i-2];
p[2] = buf[i-3];
p[3] = ',';
p += 4;
i -= 3;
}
p[-1] = NUL;
return s;
}
char *
foos (char *s, int n)
{
*s = '-';
return n < 0 ? foo(s+1, -n) -1
: foo(s , n);
}
int main (void)
{
char buf[1024];
int i0 = 0;
int i1 = 1234567;
int i2 = -1234567;
printf ("%10d : %10s\n", i0, foos(buf,i0));
printf ("%10d : %10s\n", i1, foos(buf,i1));
printf ("%10d : %10s\n", i2, foos(buf,i2));
return 0;
}
程序代码:#include<stdio.h>
int main()
{
char number1[1000],number2[1000],ch;
int i,j,k=1,sign=0;
printf("请输入任意整数:");
for(i=0;(ch=getchar())!='\n';)
{
if(ch=='-')
{
sign=1;//负数标记
}
else
{
number1[i]=ch;//把除负号的数字输入数组1
i++;
}
}
i--;//i为最后一个数字序号
for(j=1;i>=0;i--,j++,k++)//完成逆置并添加‘,’到数组2,此时为逆序
{
number2[j]=number1[i];
if(k%3==0)
{
number2[++j]=',';
}
}
j--;
if(number2[j]==',')//开始正序输出
{
j--;
}
if(sign==1)
putchar('-');
for(;j>=1;j--)
printf("%c",number2[j]);
printf("\n");
return 0;
}
