1、三级数据库技术机试-62 及答案解析(总分:100.00,做题时间:90 分钟)一、上机题(总题数:1,分数:100.00)1.已知在文件 in.dat中存有若干个(个数200)四位非零整数,函数 readdat()读取这若干个整数并存入数组 xx中。请编制函数 calvflue(),其功能要求:求出该文件中共有多少个正整数 totnum;求这些数右移 16 位,产生的一系列新数中含至少一个完全平方数(某个整数的平方)的个数 totcnt,以及满足此条件的这些数(右移前的值)的算术平均值 totpjz,最后调用函数 writedat()把所求的结果输出到文件out.dat中。部分源程序已给出。
2、请勿改动主函数 main()、读数据函数 readdat()和输出数据函数 writedat()的内容。#includestdio.h#includeconio.h#define MAXNUM 200int xxMAXNUM;iht totnum=0;iht totcnt=0;double totpjz=0.0;int readdat(void);void writedat(void);void calvalue(void)void main ()int i;clrscr ();for (i=0; iif (readdat ()printf(“Cant open the data file i
3、n.dat!/007/n“);return;calvalue ();printf ( “totnum=%d/n“, totnum);printf ( “totcnt=%d/n“ , totcnt );printf ( “totpj z=%. 21f/n“, totpj z);writedat ();int readdat (void)FILE *fp;int i=0;if( (fp=fopen (“in.dar“, “r“) )=NULL) return 1;while ( ! feof (fp)fscanf (fp, “%d, “, fclose (fp);return 0;void wri
4、tedat (void)FILE *fp;fp=fopen ( “out. dar“, “w“ )fprint f ( fp, “%d/n%d/n%. 21 f/n“, totnum, totcnt, totpjz);fclose (fp);(分数:100.00)_三级数据库技术机试-62 答案解析(总分:100.00,做题时间:90 分钟)一、上机题(总题数:1,分数:100.00)1.已知在文件 in.dat中存有若干个(个数200)四位非零整数,函数 readdat()读取这若干个整数并存入数组 xx中。请编制函数 calvflue(),其功能要求:求出该文件中共有多少个正整数 totn
5、um;求这些数右移 16 位,产生的一系列新数中含至少一个完全平方数(某个整数的平方)的个数 totcnt,以及满足此条件的这些数(右移前的值)的算术平均值 totpjz,最后调用函数 writedat()把所求的结果输出到文件out.dat中。部分源程序已给出。请勿改动主函数 main()、读数据函数 readdat()和输出数据函数 writedat()的内容。#includestdio.h#includeconio.h#define MAXNUM 200int xxMAXNUM;iht totnum=0;iht totcnt=0;double totpjz=0.0;int readdat
6、(void);void writedat(void);void calvalue(void)void main ()int i;clrscr ();for (i=0; iif (readdat ()printf(“Cant open the data file in.dat!/007/n“);return;calvalue ();printf ( “totnum=%d/n“, totnum);printf ( “totcnt=%d/n“ , totcnt );printf ( “totpj z=%. 21f/n“, totpj z);writedat ();int readdat (void)
7、FILE *fp;int i=0;if( (fp=fopen (“in.dar“, “r“) )=NULL) return 1;while ( ! feof (fp)fscanf (fp, “%d, “, fclose (fp);return 0;void writedat (void)FILE *fp;fp=fopen ( “out. dar“, “w“ )fprint f ( fp, “%d/n%d/n%. 21 f/n“, totnum, totcnt, totpjz);fclose (fp);(分数:100.00)_正确答案:(提示:类型:整数移位运算。关键点:(1)数据结束判断:分析
8、 main()和 readdat(),得到 xxi为有效数据的条件是 0=xxiMAXNUM 且xxi!=0。(2)移位运算:,注意运算符优先级。(3)完全平方数判断:可以开平方再平方与原数比较,也可以枚举出 10000以内所有四位完全平方数再查表,解答中采用第一种,需要加上头文件 mathh。解答:#include math.hvoid calvalue(void)int i, j, data, sqt;for(i=0;iMAXNUM;+)if(!xxi) break; /*数据结束*/if(xxi0) totnum+; /*计数*/for (j=1; j=6; j+) data=(xxi); /*移位*/sqt =(int) (sqrt ( (double) data) +0.5); /*求平方根并四舍五入取整*/if(sqt*sqt = data) /*完全平方数*/totcnt+;totpjz+=xxi; /*计数,求和*/break;totpjz/=totcnt;)解析: