|
void WriteDat(void); void CountRs(void) { int i,j,count; for(i=0;i<100;i++) { count=0; for(j=0;j<10;j++) if(xx[i][j]=='1') count++; if(count==0||count==10) continue; for(j=0;j<10;j++) if(xx[i][j]=='1') yy[j]++; } } void main() { int i; for(i=0;i<10;i++)yy[i]=0; if(ReadDat()){ printf("选票数据文件IN.DAT不能打开!\n\007"); return; } CountRs(); WriteDat(); } int ReadDat(void) { FILE *fp; int i; if((fp=fopen("IN.DAT","r")) == NULL) return 1; for(i=0;i<100;i++){ if(fgets(xx[i],11,fp)==NULL)return 1; xx[i][10]='\0'; } fclose(fp); return 0; } void WriteDat(void) { FILE *fp; int i; fp=fopen("OUT.DAT","w"); for(i=0;i<10;i++){ fprintf(fp,"%d\n",yy[i]); printf("第%d个人的选票数=%d\n",i+1,yy[i]); } fclose(fp); } ☆题目40 已知在文件IN.DAT中存有若干个(个数<200)四位数字的正整数,函数ReadDat()读取这若干个正整数并存入数组xx中。请编制函数CalValue(),其功能要求: 1、求出这文件中共有多少个正整数totNum; 2、求这些数中的各位数字之和是偶数的数的个数totCnt, 以及满足此条件的这些数的算术平均值totPjz,最后调用函数writeDat()把所求的结果输出到文件OUT.DAT中。 部分源程序存在文件prog1.c中。 请勿改动主函数main()、读函数ReadDat()和写函数writeDat()的内容。 #include #include #define MAXNUM 200 int xx[MAXNUM]; int totNum=0; /*文件IN.DAT中共有多少个正整数*/ int totCnt=0; /*符合条件的正整数的个数*/ double totPjz=0.0; /*平均值*/ int ReadDat(void); void writeDat(void); void CalValue(void) { int i,qw,bw,sw,gw; for(i=0;i { totNum++; qw=xx[i]/1000; bw=xx[i]/100%10; sw=xx[i]%100/10; gw=xx[i]%10; if((qw+bw+sw+gw)%2==0) { totCnt++; totPjz+=xx[i];} } if(totCnt==0) totPjz=0; else totPjz/=totCnt; } void main() { int i; clrscr(); for(i=0;i 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,",&xx[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); }
| |
| 应付计算机等级二级考试新大纲:公共基础知识 计算机等级考试三,四级证书 计算机等级考试一,二级证书 2006年4月1日计算机等级考试公共基础知识考 2006年全国计算机等级考试四级考试大纲 2006年全国计算机等级考试三级信息管理技术 2006年全国计算机等级考试二级Access考试大 全国计算机等级考试详细的介绍 全国计算机等级考试四级通关攻略 全国计算机等级考试上机考试应试技巧 |
| 文章评论 | |||