注意:源程序存放在考生文件夹下的BLANK1.C中。不得增行或删行,也不得更改程序的结构!
给定源程序:
#include
#include
double fun(double x)
{double f, t; int n;
f = 1.0 + x;
/**********found**********/
t = ___1___;
n = 1;
do {
n++;
/**********found**********/
t *= (-1.0)*x/___2___;
f += t;
}
/**********found**********/
while (___3___ >= 1e-6);
return f;
}
main()
{double x, y;
x=2.5;
y = fun(x);
printf("\nThe result is :\n");
printf("x=%-12.6f y=%-12.6f\n", x, y);
}
解题答案:
/**********第一空**********/
t = x;
/**********第二空**********/
t *= (-1.0)*x/n ;
/**********第三空**********/
while (fabs(t)>= 1e-6);
****************************************** [NextPage] 二、改错题:给定程序MODI1.C中函数fun的功能是: 求整数x的y次方的低3位值。例如,整
数5的6次方为15625, 此值的低3位值为625。
请改正函数fun中指定部位的错误, 使它能得出正确的结果。
注意: 不要改动main函数, 不得增行或删行, 也不得更改程序的结构!
给定源程序:
#include
long fun(int x,int y,long *p)
{int i;
long t=1;
/**************found**************/
for(i=1; i
t=t*x;
*p=t;
/**************found**************/
t=t/1000;
return t;
}
main()
{long t,r; int x,y;
printf("\nInput x and y: "); scanf("%ld%ld",&x,&y);
t=fun(x,y,&r);
printf("\n\nx=%d, y=%d, r=%ld, last=%ld\n\n",x, y,r,t);
}
解题答案:
/**************found**************/
for(i=1; i<=y; i++)
/**************found**************/
t=t%1000;
******************************************
[NextPage] 三、程序题:例如, 在主函数中从键盘给x输入0.21后,输出为:s=1.100000。
注意: 部分源程序在文件PROG1.C中。
请勿改动主函数main和其他函数中的任何内容, 仅在函数fun的
花括号中填入你编写的若干语句。
给定源程序:
#include
#include
double fun(double x)
{
}
NONO()
{/* 请在此函数内打开文件,输入测试数据,调用 fun 函数,输出数据,关闭文件。 */
FILE *rf, *wf ; int i ; double s, x ;
rf = fopen("in.dat","r");
wf = fopen("out.dat","w");
for(i = 0 ; i < 10 ; i++) {
fscanf(rf, "%lf", &x);
s = fun(x);
fprintf(wf, "%lf\n", s);
}
fclose(rf); fclose(wf);
}
main()
{double x,s;
printf("Input x: "); scanf("%lf",&x);
s=fun(x);
printf("s=%f\n",s);
NONO();
}
参考答案:
double fun(double x)
{
int n=1; /* 循环计数*/
double sn=1; /* 累计数*/
double xn=1,xn1=0; /*x的n值,以及x的n-1值;*/
while(fabs(xn-xn1)>=0.000001)/*绝对值是否满足条件*/
{
xn=xn*x*(0.5-n+1)/n; /*表达式分解以后xn=(xn-1)*x*(0.5-n+1)/n*/
n+=1;
sn+=xn; /*sn累加上xn*/
}
return(sn);
}
| 广告合作:400-664-0084 全国热线:400-664-0084 Copyright 2010 - 2017 www.my8848.com 珠峰网 粤ICP备15066211号 珠峰网 版权所有 All Rights Reserved
|