1、三级数据库技术机试-200 及答案解析(总分:100.00,做题时间:90 分钟)一、上机题(总题数:1,分数:100.00)1.已知在文件 IN.dat 中存有若干个(少于 200 个)四位数字的正整数,函数 ReadDat()读取这若干个正整数并存入数组 number 中。请编写函数 CalValue(),其功能要求是:求出文件中共有的正整数个数totNum;求这些数右移 1 位后,产生的新数是奇数的数的个数 totCnt 以及满足此条件的这些数(右移前的值)的算术平均值 totAve。最后调用函数 writeDat()把所求的结果输出到 OUT.dat 文件中。注意:部分源程序已经给出。
2、请勿改动主函数 main()、读函数 ReadDat()和写函数 writeDat()的内容。#include stdio.h#include conio. h#define MAXNUM 200int number MAXNUM;int totNum = 0; /* 文件 IN.dst 中共有的正整数个数*/int totCnt = 0; /* 符合条件的正整数的个数*/double totAve = 0.0; /* 平均值 */int ReadDat (void);void writeDat(void);void CalValue(void)void main ()int i;for (i
3、=0; iMAXNUM; i+)number i = 0;if (ReadDat()printf (“ 数据文件 IN.dst 不能打开! /007/n“);return;CalValue ();printf(“ 文件 IN.dst 中共有的正整数个数=%d 个/n“, totNum);printf (“ 符合条件的正整数的个数 =%d 个/n“, totCnt);printf(“平均值=%.2f/n“, totAve);writeDat ();int ReadDat (void)FILE *fp;int i = 0;if (fp = fopen(“IN.dat“, “r“) = NULL)r
4、eturn 1;while (!feof(fp)fscanf(fp, “%d,“, 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, totAve);fclose (fp);(分数:100.00)_三级数据库技术机试-200 答案解析(总分:100.00,做题时间:90 分钟)一、上机题(总题数:1,分数:100.00)1.已知在文件 IN.dat 中存有若干个(少于 200 个)四位数字的正整数,
5、函数 ReadDat()读取这若干个正整数并存入数组 number 中。请编写函数 CalValue(),其功能要求是:求出文件中共有的正整数个数totNum;求这些数右移 1 位后,产生的新数是奇数的数的个数 totCnt 以及满足此条件的这些数(右移前的值)的算术平均值 totAve。最后调用函数 writeDat()把所求的结果输出到 OUT.dat 文件中。注意:部分源程序已经给出。请勿改动主函数 main()、读函数 ReadDat()和写函数 writeDat()的内容。#include stdio.h#include conio. h#define MAXNUM 200int n
6、umber MAXNUM;int totNum = 0; /* 文件 IN.dst 中共有的正整数个数*/int totCnt = 0; /* 符合条件的正整数的个数*/double totAve = 0.0; /* 平均值 */int ReadDat (void);void writeDat(void);void CalValue(void)void main ()int i;for (i=0; iMAXNUM; i+)number i = 0;if (ReadDat()printf (“ 数据文件 IN.dst 不能打开! /007/n“);return;CalValue ();print
7、f(“ 文件 IN.dst 中共有的正整数个数=%d 个/n“, totNum);printf (“ 符合条件的正整数的个数 =%d 个/n“, totCnt);printf(“平均值=%.2f/n“, totAve);writeDat ();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;void writeDat (void)FILE *fp;fp = fopen(
8、“OUT.dat“, “w“);fprintf(fp, “%d/n%d/n%6.2f/n“, totNum, totCnt, totAve);fclose (fp);(分数:100.00)_正确答案:(void CalValue(void) int i, data;for (i=0; iMAXNUM; i+) if (!numberi)break;if (numberi 0)totNum+;data = numberi1;if (data%2) totCnt+;totAve += numberi;totAve /= totCnt;)解析:解题思路 在 for 循环语句中,自变量 i 从 0 递增到 MAXNUM 对数组 number 中的每个数进行判断,如果 numberi的值大于 0,说明 numberi的值为正整数,统计正整数个数的变量 totNum 加 1;然后numberi右移 1 位得到 data 的值;再对 data 的值除 2 求余数,如果其值为 1,则统计变量 totCnt 加1,同时把 numberi的值加到 totAve 上,得到这些数的和。退出循环后,用 totAve 的值除以 totCnt,就得到了这些数的算术平均值 totAve。