一 个计算机C语言题

来源:百度知道 编辑:UC知道 时间:2024/05/29 00:35:46
#include <stdio.h>
int main(void)
{
char s[7];
int k;
void split(int,char *);
printf("Please input a Integer( Range:-32768 to 32767 ):");
scanf("%d",&k);
split(k,s);
printf("%s\n",s);
return 0;
}
void split(int t,char *ps)
{
int i;
char *q=ps,tmp;
if (t>0)
*ps++='+';
if (t<0)
{
*ps++='-';
t=-t;
}
/*------------Found Mistake Below------------*/
while(t/10!=0)
{
i=t/10;
*ps++=t-i*10;
t=i;
}
*ps++='0'+t;
*ps='\0';
/*------------Found Mistake Below------------*/
for(q++;q<ps;q++,ps--)
tmp=*q,*q=*ps,*ps=tmp;

}
程序改错题
输入一个范围-32768——32767的整数,调用函数split(),拆解出各位的数字,放入字符数组s[]中。
正数前面加“+”号,负数前面加“-”号,零不加符号。
gc486.c程序有两处错误,请找出并修正,使之符合上述要求。不得增加行或删除行,也不得更改程序结构。

#include <stdio.h>
int main(void)
{
char s[7];
int k;
void split(int,char *);
printf("Please input a Integer( Range:-32768 to 32767 ):");
scanf("%d",&k);
split(k,s);
printf("%s\n",s);
return 0;
}
void split(int t,char *ps)
{
int i;
char *q=ps,tmp;
if (t>0)
*ps++='+';
if (t<0)
{
*ps++='-';
t=-t;
}
/*------------Found Mistake Below------------*/
while(t/10!=0)
{
i=t/10;
*ps++='0'+t-i*10; //加上字符0,不然显示出的是乱码
t=i;
}
*ps++='0'+t;
*ps--='\0'; //由于下面语句要求ps开始时指向最后一个非空字符,所以这里ps要前移一们
/*------------Found Mistake Below------------*/
for(q++;q<ps;q++,ps--)
tmp=*q,*q=*ps,*ps=tmp;

}