1、三级网络技术机试-100 及答案解析(总分:100.00,做题时间:90 分钟)一、上机题(总题数:1,分数:100.00)1.文件 IN65DAT 中存有若干个(个数200)4 位数字的正整数,函数 ReadDat()是读取这若干个正整数并存入数组 xx 中。请编制函数 CalValue(),其功能要求是:(1)求出这个文件中共有多少个正整数totNum。(2)求出这些数中的各位数字之和是奇数的个数 totCnt,以及满足此条件的这些数的算术平均值 totpjz。最后调用函数 WriteDat()把所有结果输出到文件 OUT65.DAT 中。注意:部分源程序已给出。请勿改动主函数 main(
2、)、读函数 ReadDat()和写函数 WriteDat()的内容。#includestdio. h#includeconio. h#define MAXNUM 200int xx MAXNUM;int totNum=0;int totCnt-0;double totPjz=0.0;int ReadDat (void);void WriteDat(void);void CalValue (void)void main()int i;clrscr ( );for (i=0; iMAXNUM; i+)xxi=0;if (ReadDat ()printf (“数据文件 IN65.DAT 不能打开!/
3、007/n“);return;CalValue ();printf (“文件 IN65. DAT 中共有正整数=%d 个/n“, totNum);printf (“符合条件的正整数的个数=%d 个 n“, totCnt);printf (“平均值=%.21f/n“, totPj z);WriteDat ();int ReadDat (void)FILE *fp;int i=0;if ( (fp=fopen (“IN65. DAT“, “r“) ) =NULL)return 1;while ( ! feof (fp)fscanf (fp, “%d“, fclose (fp);return 0;v
4、oid WriteDat(void)FILE *fp;fp=fopen ( “OUT65. DAT“, “w“ );fprintf (fp, “%d/n%d/n%.2,1f/n“, totNum, totCnt, totPj z);fclose (fp);(分数:100.00)_三级网络技术机试-100 答案解析(总分:100.00,做题时间:90 分钟)一、上机题(总题数:1,分数:100.00)1.文件 IN65DAT 中存有若干个(个数200)4 位数字的正整数,函数 ReadDat()是读取这若干个正整数并存入数组 xx 中。请编制函数 CalValue(),其功能要求是:(1)求出这
5、个文件中共有多少个正整数totNum。(2)求出这些数中的各位数字之和是奇数的个数 totCnt,以及满足此条件的这些数的算术平均值 totpjz。最后调用函数 WriteDat()把所有结果输出到文件 OUT65.DAT 中。注意:部分源程序已给出。请勿改动主函数 main()、读函数 ReadDat()和写函数 WriteDat()的内容。#includestdio. h#includeconio. h#define MAXNUM 200int xx MAXNUM;int totNum=0;int totCnt-0;double totPjz=0.0;int ReadDat (void);
6、void WriteDat(void);void CalValue (void)void main()int i;clrscr ( );for (i=0; iMAXNUM; i+)xxi=0;if (ReadDat ()printf (“数据文件 IN65.DAT 不能打开!/007/n“);return;CalValue ();printf (“文件 IN65. DAT 中共有正整数=%d 个/n“, totNum);printf (“符合条件的正整数的个数=%d 个 n“, totCnt);printf (“平均值=%.21f/n“, totPj z);WriteDat ();int Re
7、adDat (void)FILE *fp;int i=0;if ( (fp=fopen (“IN65. DAT“, “r“) ) =NULL)return 1;while ( ! feof (fp)fscanf (fp, “%d“, fclose (fp);return 0;void WriteDat(void)FILE *fp;fp=fopen ( “OUT65. DAT“, “w“ );fprintf (fp, “%d/n%d/n%.2,1f/n“, totNum, totCnt, totPj z);fclose (fp);(分数:100.00)_正确答案:(void CalValue (
8、void) int i, thou, hun, ten, data;for (i=O; i if (!xxi) coutinue;if (xxi0) totNum+; /*求正整数的个数*/thou=xx i/1000; /*求四位数的千位数字*/hun=xx i %1000/100; /*求四位数的百位数字*/ten=xx i %100/10; /*求四位数的十位数字*/data=xx i %10; /*求四位数的个位数字*/if (thou+hun+ten+data) %2) totCnt+; /*求各位数字之和是奇数的个数*/tot. Pj z+=xx i; /*求满足条件的数的总和*/totPj z/=totCnt; /*求满足条件的数的平均值*/)解析:解析本题考查的知识点如下:(1)“%”与叩的使用。(2)数组结束的判断和强行退出一层循环结构。在本题中,并没有给出确切的数据个数,是以数据的最大个数定义的数组。在主函数中,给所有的数组成员赋初值为 0,而从文件中读取的数据是正整数,所以只要数组的某个元素为 0,则说明数组存的数据已经结束。此时就可以结束循环结构。这里要借助运算符“%”与“/”将 4 位数的各位上的数拆成独立的数字,然后就可以根据题意要求判断。