1、三级数据库技术-501 及答案解析(总分:100.00,做题时间:90 分钟)一、设计与应用题(总题数:1,分数:100.00)1.已知在 in.dat 中存有若干个(个数200)四位数字的正整数,函数 ReadDat()读取这若干个正整数并存入数组 xx 中。请编制函数 CalValue(),其功能要求:1求出该文件中共有多少个正整数 totNum;2求这些正整数右移 1 位二进制位后,产生的新数是奇数的数的个数 totCnt,以及满足此条件的这些正整数(右移前的值)的算术平均值 totPjz。最后 main()函数调用函数 WriteDat()把所求的结果输出到文件out.dat 中。 请
2、勿改动数据文件 in.dat 中的任何数据,主函数 main()、读函数 ReadDat()和输出函数 WriteDat()的内容。 #include stdio.h #define MAXNUM 200 int xxMAXNUM; int totNum=0; /文件 in.dat 中共有多少个正整数 int totCnt=0; /符合条件的正整数的个数 double totPjz=0.0; /平均值 int ReadDat(void); void WriteDat(void); void CalValue(void) void main() int i; for(i=0; iMAXNUM;
3、i+) xxi=0; if(ReadDat() printf(“数据文件 in.dat 不能打开!007n“); return; CalValue(); printf(“文件 in.dat 中共有正整数=%d 个n“, totNum); printf(“符合条件的正整数的个数=%d 个n“, totCnt); printf(“平均值=%.2lfn“, totPjz); WriteDat(); /*读取这若干个正整数并存入数组 xx 中*/ int ReadDat(void) FILE *fp; int i=0; if(fp=fopen(“in.dat“, “r“)=NULL) return 1
4、; while(!feof(fp) fscanf(fp, “%d, “, fclose(fp); return 0; /*把计算结果存入文件 out.dat 中*/ void WriteDat(void) FILE *fp; fp=fopen(“out.dat“, “w“); fprintf(fp, “%dn%dn%.2lfn“, totNum, totCnt, totPjz); fclose(fp); (分数:100.00)_三级数据库技术-501 答案解析(总分:100.00,做题时间:90 分钟)一、设计与应用题(总题数:1,分数:100.00)1.已知在 in.dat 中存有若干个(个
5、数200)四位数字的正整数,函数 ReadDat()读取这若干个正整数并存入数组 xx 中。请编制函数 CalValue(),其功能要求:1求出该文件中共有多少个正整数 totNum;2求这些正整数右移 1 位二进制位后,产生的新数是奇数的数的个数 totCnt,以及满足此条件的这些正整数(右移前的值)的算术平均值 totPjz。最后 main()函数调用函数 WriteDat()把所求的结果输出到文件out.dat 中。 请勿改动数据文件 in.dat 中的任何数据,主函数 main()、读函数 ReadDat()和输出函数 WriteDat()的内容。 #include stdio.h #
6、define MAXNUM 200 int xxMAXNUM; int totNum=0; /文件 in.dat 中共有多少个正整数 int totCnt=0; /符合条件的正整数的个数 double totPjz=0.0; /平均值 int ReadDat(void); void WriteDat(void); void CalValue(void) void main() int i; for(i=0; iMAXNUM; i+) xxi=0; if(ReadDat() printf(“数据文件 in.dat 不能打开!007n“); return; CalValue(); printf(“
7、文件 in.dat 中共有正整数=%d 个n“, totNum); printf(“符合条件的正整数的个数=%d 个n“, totCnt); printf(“平均值=%.2lfn“, totPjz); WriteDat(); /*读取这若干个正整数并存入数组 xx 中*/ int ReadDat(void) FILE *fp; int i=0; if(fp=fopen(“in.dat“, “r“)=NULL) return 1; while(!feof(fp) fscanf(fp, “%d, “, fclose(fp); return 0; /*把计算结果存入文件 out.dat 中*/ vo
8、id WriteDat(void) FILE *fp; fp=fopen(“out.dat“, “w“); fprintf(fp, “%dn%dn%.2lfn“, totNum, totCnt, totPjz); fclose(fp); (分数:100.00)_正确答案:()解析:int i, j; long he=0; for(i=0; iMAXNUM; i+) if(xxi) totNum+; for(i=0; itotNum; i+) j=(xxi1); if(j%2) /如果 j 是奇数 totCnt+; he+=xxi; totPjz=(double)he/totCnt; 解析 读取正数,统计个数,右移后为奇数的个数及平均值。 通过审题可以发现仅有一个不同点,即参与平均值计算的元素是数组 xxi右移一位之后为奇数的元素,参考答案的第 9 条语句。
copyright@ 2008-2019 麦多课文库(www.mydoc123.com)网站版权所有
备案/许可证编号:苏ICP备17064731号-1