計算機(jī)等級考試二級C語言上機(jī)題22

字號:

★題目22
    請編寫函數(shù)countvalue(),它的功能是:求n以內(nèi)(不包括n)同時能被3與7整除的所有自然數(shù)之和的平方根s,并作為函數(shù)值返回,最后結(jié)果s輸出到文件out.dat中。
     例如若n為1000時,函數(shù)值應(yīng)為:s=153.909064。
     部分源程序存在文件prog1.c中。
     請勿改動主函數(shù)main()和輸入輸出數(shù)據(jù)函數(shù)progreadwrite()的內(nèi)容。
    #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("自然數(shù)之和的平方根=%f\n",countvalue(1000));
     progreadwrite();
    }
    
    progreadwrite()
    {
     file *fp,*wf;
     int i,n;
     float s;
    
     fp=fopen("in.dat","r");
     if(fp==null){
     printf("數(shù)據(jù)文件in.dat不存在!");
     return;
     }
     wf=fopen("out.dat","w");
     for(i=0;i<10;i++){
     fscanf(fp,"%d\n",&n);
     s=countvalue(n);
     fprintf(wf,"%f\n",s);
     }
    fclose(fp);
    fclose(wf);
    }