一、填空题:函数fun的功能是:统计长整数n的各个位上出现数字1、2、3的次数,并通过外部(全局)变量c1,c2,c3返回主函数。例如:当n=123114350时,结果应该为:
c1=3 c2=1 c3=2。
请在程序的下划线处填入正确的内容并把下划线删除,使程序得出正确的结果。
注意:源程序存放在考生文件夹下BLANK1.C中。
不得增行或删行,也不得更改程序的结构!
给定源程序:
#include
int c1,c2,c3;
void fun(long n)
{c1 = c2 = c3 = 0;
while (n) {
/**********found**********/
switch(___1___)
{
/**********found**********/
case 1: c1++;___2___;
/**********found**********/
case 2: c2++;___3___;
case 3: c3++;
}
n /= 10;
}
}
main()
{long n=123114350L;
fun(n);
printf("\nThe result :\n");
printf("n=%ld c1=%d c2=%d c3=%d\n",n,c1, c2,c3);
}
解题答案:
/**********第一空**********/
switch(n)
/**********第二空**********/
case 1: c1++;break;
/**********第三空**********/
case 2: c2++;break;
****************************************** [NextPage] 二、改错题:给定程序MODI1.C中函数fun的功能是: 统计一个无符号整数中各位数字值为零的个数, 通过形参传回主函数;并把该整数中各位上最大的数字值作为函数值返回。例如, 若输入无符号整数30800, 则数字值为零的个数为3, 各位上数字值最大的是8。
请改正函数fun中指定部位的错误, 使它能得出正确的结果。
注意: 不要改动main函数, 不得增行或删行, 也不得更改程序的结构!
给定源程序:
#include
int fun(unsigned n, int *zero)
{int count=0,max=0,t;
do
{t=n;
/**************found**************/
if(t=0)
count++;
if(max n=n/10; }while(n); /**************found**************/ zero=count; return max; } main() {unsigned n; int zero,max; printf("\nInput n(unsigned): "); scanf("%d",&n); max = fun(n,&zero); printf("\nThe result: max=%d zero=%d\n",max, zero); } 解题答案: /**************found**************/ if(t==0) /**************found**************/ *zero=count; ****************************************** [NextPage] 三、程序题:请编写函数fun, 其功能是: 计算并输出下列多项式的值: 1 1 1 S = 1 + ── + ─── + … + ───── 1*2 1*2*3 1*2*3*…50 例如, 在主函数中从键盘给n输入50后,输出为:s=1.718282。 注意: 要求n的值大于1但不大于100。 部分源程序在文件PROG1.C中。 请勿改动主函数main和其他函数中的任何内容, 仅在函数fun的花括号中填入 你编写的若干语句。 给定源程序: #include double fun(int n) { } NONO() {/* 请在此函数内打开文件,输入测试数据,调用 fun 函数,输出数据,关闭文件。 */ FILE *rf, *wf ; int n, i ; double s ; rf = fopen("in.dat","r"); wf = fopen("out.dat","w"); for(i = 0 ; i < 10 ; i++) { fscanf(rf, "%d", &n); s = fun(n); fprintf(wf, "%lf\n", s); } fclose(rf); fclose(wf); } main() {int n; double s; printf("\nInput n: "); scanf("%d",&n); s=fun(n); printf("\n\ns=%f\n\n",s); NONO(); } 参考答案: double fun(int n) { double s=0, t=1; int i ; for(i=1; i<=n; i++) { t *= i; s += 1./t; } return s; }
| 广告合作:400-664-0084 全国热线:400-664-0084 Copyright 2010 - 2017 www.my8848.com 珠峰网 粤ICP备15066211号 珠峰网 版权所有 All Rights Reserved
|