填空题:
请补充fun函数,该函数的功能是:把一个证书转换成字符串,并到序保存在字符数组str中。例如:当n=12345678时,str=“87654321”。
请勿改动主函数main和其他函数中的任何内容,仅在fun函数的横线上填入所编写的若干表达式或语句。
填空题:
#include
#include
#define N 80
char str[N];
void fun(long int n)
{
int i = 0;
while (___1___)
{
str[i] = ___2___;
n /= 10;
i++;
}
___3___;
}
main()
{
long int n = 1234567;
printf("****** the origial data ********\n");
printf("n=%ld", n);
fun(n);
printf("\n%s", str);
}
参考答案:
第一处填空:n>0或0
第二处填空:n%10+’0’或’0’+n%10
第三处填空:str[i]=’\0’或str[i]=0
改错题:
下列给定程序中,函数fun的功能是:从N个字符串中找出最长的那个串,并将其地址作为函数值返回。各字符串在主函数中输入,并放入一个字符串数组中。
请改正程序中的错误,使它能得出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构!
改错题:#include
#include
#define N 5
#define M 81
/********found********/
fun(char (*sq)[N])
{
int i;
char *sp;
sp = sq[0];
for (i=0; i
if (strlen(sp) < strlen(sq[i]))
sp = sq[i];
/********found********/
return sq;
}
main()
{
char str[N][M], *longest;
int i;
printf("Enter %d lines:\n", N);
for (i=0; i
gets(str[i]);
printf("\nThe N string :\n", N);
for (i=0; i
puts(str[i]);
longest = fun(str);
printf("\nThe longest string :\n");
puts(longest);
}
参考答案:
第一处:fun(char (*sq)[N]) 应改为 char * fun(char (*sq)[M])
第二处:return sq;应改为 return sp;
编程题:
请编写函数fun,其功能是:将两个两位数的正证书a、b合并形成一个证书放在c中。合并的方式是:将a数的十位和个位数依次放在c数的千位和十位上,b数的十位和个位数依次放在c数的百位和个位上。
例如,当a=45,b=12,调用该项函数后,c=4152。
请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
参考答案:
编程题:#include
#include
void fun (int a, int b ,long *c)
{
}
main ()
{
int a,b;
long c;
FILE *out;
printf ("Input a, b:");
scanf("%d%d", &a, &b);
fun (a, b, &c);
printf ("The result is: %d\n", c);
out=fopen ("out.dat", "w");
for (a = 10; a < 20; a++)
{
fun(a, 109-a, &c);
fprintf(out, "%d\n", c);
}
fclose (out );
}
输入fun函数:void fun (int a, int b, long*c)
{
*c=(a/10)*1000+(b/10)*100+(a%10)*10+b%10;
}
更多关注: