简答题
C语言
题号 #2257012
下列给定程序中函数proc()的功能是:将长整型数中为偶数的数依次逆向取出,构成一个新数放在t中。高位在低位,低位在高位。例如,当S中的数为l2345678时,则t 中的数为8642。
请修改函数proc()中的错误,使它能得出正确的结果。
注意:不要改动main()函数,不得增行或删行,也不得更改程序的结构。
试题程序:
#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
void proc(long S,long*t)
{
int d;
long s1=1,i=1:
*t=0:
while(s/i>0)
i=i*10:
i=i/10:
while(s>0)
{
d=s/i:
//****found****
if(d%2 1=0)
{
//****found****
t=d*s1+t:
s1*=10:
}
s=s%i;
i=i/10:
}
}
void main()
{
long s,t;
system("CLS");
printf("\nPlease enter s");
scanf("%1d",&s);
proc(s,&t):
printf("The result is:%1d\n",t);
}
参考答案
暂无
答案解析
(1)错误:if(d%2!=O)
正确:if(d%2==0)
(2)错误:t=d*s1+t;
正确:*t=d*s1+*t;
【解析】题目要求长整型数中每一位上为偶数的数依次逆向取出,构成一个新数放在t中。首先要判断每一位上的数是否为偶数,因此“if(d%2!=0)”应改为“if(d%2== 0)”;变量t是一个指针变量,其直接运算操作的内存地址,因此“t=d*s1+t;”应改为“*t=d*s1+*t;”。