《计算机等级考试四级上机编程试题及答案》由会员分享,可在线阅读,更多相关《计算机等级考试四级上机编程试题及答案(20页珍藏版)》请在金锄头文库上搜索。
1、XX年计算机等级考试四级上机编程试题及答案试题说明 :在文件IN.DAT中存有假设干个(个数200)四位数字的正整数, 函数ReadDat()是读取这假设干个正整数并存入数组xx中。请编 制函数CalValue(),其功能要求:1.求出这文件中共有多少个正 整数tot Num; 2.求出这些数中的各位数字之和是偶数的数的个数 totCnt,以及满足此条件的这些数的算术平均值totPjz,最后调用 函数WriteDat()把所求的结果输出到文件OUT2.DAT中。注意:局部源程序存放在PR0G1.C中。请勿改动主函数main()、读数据函数ReadDat()和输出数据 函数WriteDat()的
2、内容。程序 :#include#include#define MAXNUM 200int xxMAXNUM ;int tot Num = 0 ; /*文件IN.DAT中共有多少个正整数*/int totCnt = 0 ; /*符合条件的正整数的个数 */double totPjz = 0.0 ; /*平均值 */int ReadDat(void) ;void WriteDat(void) ;void CalValue(void)void main()clrscr() ;if(ReadDat() prin tf(数据文件IN.DAT不能翻开!07n);return ;CalValue() ;pr
3、in tf(文件 IN.DAT 中共有正整数=%d 个 n, tot Num); printf(符合条件的正整数的个数=%d个n, totCnt); prin tf(平均值=%.2lfn, tot Pjz);WriteDat() ;int ReadDat(void)FILE *fp ;int i = 0 ;if(fp = fopen(in.dat, r) = NULL) return 1 ; while(!feof(fp) fscanf(fp, %d, &xxi+) ;fclose(fp) ;return 0 ;void WriteDat(void)FILE *fp ;fp = fopen(O
4、UT2.DAT, w) ;fprintf(fp, %dn%dn%.2lfn, totNum, totCnt, totPjz) ; fclose(fp) ;所需数据 :2 IN.DAT 0166045,6192,1885,3580,8544,6826,5493,8415,3132,58416561,3173,9157,2895,2851,6082,5510,9610,5398,52733438,1800,6364,6892,9591,3120,8813,2106,5505,10855835,7295,6131,9405,6756,2413,6274,9262,5728,26506266,5285
5、,7703,1353,1510,2350,4325,4392,7573,82047358,6365,3135,9903,3055,5869,5893,4569,1251,2542,4362,6214,5680,8753,8443,1018,9729,8588,2797,4321,9968,5558,9311,7047,6138,4025,3572,9605,1291,6027,9661,5849,3210,2554,8604,7014,9058,6259,9503,1615,5146,7066,1029,1777,7788,9175,6099,2930,4685,8465,4274,2857,
6、6829,6226,8268,#E3 $OUT2.DAT 003|160|91|5517.16#E3219,3955,7313,6206,1631,5740,2073,9805,1189,7550,3636,4495,9643,3782,5556,4714,9658,8997,2080,5912,7618,5448,1466,7075,2166,2358,1911,2747,7068,1716,8010,7947,3685,2945,4224,1060,7787,8983,3822,2471,2941,3538,2912,3096,7421,8633,2628,7155,4307,9535,9
7、377,9415,9059,4872,6072,试题说明 :在文件IN.DAT中存有假设干个(个数200)四位数字的正整数, 函数ReadDat()编制函数CalValue(),其功能要求:1.求出这文 件中共有多少个正整数tot Num; 2.求出这些数中的各位数字之和是 奇数的数的个数totCnt,以及满足此条件的这些数的算术平均值 totPjz,最后调用函数WriteDat()把所求的结果输出到文件 OUT1.DAT 中。注意:局部源程序存放在PR0G1.C中。请勿改动主函数main()、读数据函数ReadDat()和输出数据 函数WriteDat()的内容。程序 :#include#i
8、nclude#define MAXNUM 200 int xxMAXNUM ;int tot Num = 0 ; /*文件IN.DAT中共有多少个正整数*/ int totCnt = 0 ; /*符合条件的正整数的个数 */double totPjz = 0.0 ; /*平均值 */int ReadDat(void) ;void WriteDat(void) ;void CalValue(void)void main()clrscr() ;if(ReadDat() printf (数据文件IN.DAT不能翻开!07n); return ;CalValue() ;prin tf(文件IN.DAT
9、中共有正整数=%d个n, printf(符合条件的正整数的个数=%d个n, prin tf(平均值=%.2lfn, tot Pjz);WriteDat() ;int ReadDat(void)FILE *fp ;int i = 0 ;totNum) ;totCnt) ;return 1if(fp = fopen(in.dat, r) = NULL) while(!feof(fp) fscanf(fp, %d, &xxi+) ; fclose(fp) ;return 0 ;void WriteDat(void)FILE *fp ;fp = fopen(OUT1.DAT, w) ;fprintf(
10、fp, %dn%dn%.2lfn, totNum, totCnt, totPjz) ; fclose(fp) ;所需数据 :2 IN.DAT 0166045,6192,1885,3580,8544,6826,5493,8415,3132,5841,6561,3173,9157,2895,2851,6082,5510,9610,5398,5273,3438,1800,6364,6892,9591,3120,8813,2106,5505,1085,5835,7295,6131,9405,6756,2413,6274,9262,5728,2650,6266,5285,7703,1353,1510,2
11、350,4325,4392,7573,8204,7358,6365,3135,9903,3055,3219,3955,7313,6206,1631,5869,5893,4569,1251,2542,5740,2073,9805,1189,7550,4362,6214,5680,8753,8443,3636,4495,9643,3782,5556,1018,9729,8588,2797,4321,4714,9658,8997,2080,5912,9968,5558,9311,7047,6138,7618,5448,1466,7075,2166,4025,3572,9605,1291,6027,2
12、358,1911,2747,7068,1716,9661,5849,3210,2554,8604,8010,7947,3685,2945,4224,7014,9058,6259,9503,1615,1060,7787,8983,3822,2471,5146,7066,1029,1777,7788,2941,3538,2912,3096,7421,9175,6099,2930,4685,8465,8633,2628,7155,4307,9535, #E3 $OUT1.DAT 003|160|69|5460.51#E试题说明 :在文件IN.DAT中存有假设干个(个数200)四位数字的正整数, 函数
13、ReadDat()是读取这假设干个正整数并存入数组xx中。请编 制函数CalValue(),其功能要求:1.求出这文件中共有多少个正 整数tot Num; 2.求这些数右移1位后,产生的新数是奇数的数的 个数totCnt,以及满足此条件的这些数(右移前的值)的算术平均值 totPjz,最后调用函数WriteDat()把所求的结果输出到文件 0UT3.DAT 中。注意:局部源程序存放在PROG1.C中。请勿改动主函数main()、读数据函数ReadDat()和输出数据 函数WriteDat()的内容。程序 :#include#include#define MAXNUM 200int xxMAXN
14、UM ;int tot Num = 0 ; /*文件IN.DAT中共有多少个正整数*/int totCnt = 0 ; /*符合条件的正整数的个数 */double totPjz = 0.0 ; /*平均值 */int ReadDat(void) ;void WriteDat(void) ;void CalValue(void)void main()clrscr() ;if(ReadDat() printf (数据文件IN.DAT不能翻开!07n); return ;CalValue() ;prin tf(文件IN.DAT中共有正整数=%d个n, printf(符合条件的正整数的个数=%d个n, prin tf(平均值=%.2lfn, tot Pjz);WriteDat() ;int ReadDat(void)FILE *fp ;int i = 0 ;if(fp = fopen(in.dat, r) = NULL) while(!feof(fp) fscanf(fp, %d, &xxi+) ;fclose(fp) ;return 0 ;totNum) ;totCnt) ;return 1void WriteDat(void)FILE *fp ;fp = fopen(OUT3.DAT, w) ;fprintf(fp, %