2006年9月全國等級考試三級c語言上機題庫(二十六)
★題目26(無憂id 37 整數統計運算題) 已知在文件in.dat中存有若干個(個數<200)四位數字的正整數,函數readdat()讀取這若干個正整數并存入數組中。請編制函數calvalue(),其功能要求:1、求出這文件中共有多少個正整數totnum;2、求這些數右移1位后,產生的新數是偶數的數的個數totcnt,以及滿足此條件的這些數(右移前的值)的算術平均值totpjz,最后調用函數writedat()把所求的結果輸出到文件out.dat中。部分源程序存在文件prog1.c中。請勿改動主函數main()、讀函數readdat()和寫函數writedat()的內容。#include <stdio.h>#include <conio.h>#define maxnum 200 int [maxnum];int totnum=0; /*文件in.dat中共有多少個正整數*/int totcnt=0; /*符合條件的正整數的個數*/double totpjz=0.0; /*平均值*/ int readdat(void);void writedat(void); void calvalue(void){int i,data;for(i=0;i<maxnum;i++)if([i]>0) { totnum++;data=[i]>>1;if(data%2==0){totcnt++;totpjz+=[i];}}if(totcnt==0) totpjz=0;else totpjz/=totcnt;} void main(){int i;clrscr();for(i=0;i<maxnum;i++)[i]=0;if(readdat()){printf("數據文件in.dat不能打開!\007\n");return;}calvalue();printf("文件in.dat中共有正整數=%d個\n",totnum);printf("符合條件的正整數的個數=%d個\n",totcnt);printf("平均值=%.2f\n",totpjz);writedat();} int readdat(void){file *fp;int i=0; if((fp=fopen("in.dat","r"))==null) return 1;while(!feof(fp)){fscanf(fp,"%d,",&[i++]);}fclose(fp);return 0;} void writedat(void){file *fp;fp=fopen("out.dat","w");fprintf(fp,"%d\n%d\n%6.2f\n",totnum,totcnt,totpjz);fclose(fp);}