计算机等级考试二级C语言上机题22

全国等级考试资料网 2019-01-23 14:13:13 41
★题目22
请编写函数countvalue(),它的功能是:求n以内(不包括n)同时能被3与7整除的所有自然数之和的平方根s,并作为函数值返回,最后结果s输出到文件out.dat中。
 例如若n为1000时,函数值应为:s=153.909064。
 部分源程序存在文件prog1.c中。
 请勿改动主函数main()和输入输出数据函数progreadwrite()的内容。
#include 
#include 
#include 

double countvalue(int n)
{ int i;
 double s=0.0;
 for(i=1;i if(i%21==0) s+=i;
 return sqrt(s);
}

main()
{
 clrscr();
 printf("自然数之和的平方根=%f ",countvalue(1000));
 progreadwrite();
}

progreadwrite()
{
 file *fp,*wf;
 int i,n;
 float s;

 fp=fopen("in.dat","r");
 if(fp==null){
 printf("数据文件in.dat不存在!");
 return;
 }
 wf=fopen("out.dat","w");
 for(i=0;i<10;i++){
 fscanf(fp,"%d ",&n);
 s=countvalue(n);
 fprintf(wf,"%f ",s);
 }
fclose(fp);
fclose(wf);
} 相关资料

相关阅读