10./*PROG10 已知在文件IN.DAT中存有若干个(个数<200)四位数字的正整数,函数ReadDat()是读取这若干个正整数并存入数组XX中。请编制函数CalValue(),其功能要求: 1、求出这文件中共有多少个正整数totNum; 2、求出这些数中的各位数字之和是奇数的数的个数totCnt,以及满足此条件的这些数的算术平均值totpjZ,最后调用函数WriteDat()把所求的结果输出到文件OUT1.DAT中。 注意:部分部分源程序存放在PROG10.C中。 请勿改动主函数main(),读数据函数ReacdDat()和输出数据函数WriteDat()的内容。*/ #include #include #define MAXNUM 200 int xx[MAXNUM] ; int totNum = 0 ; /* 文件IN.DAT中共有多少个正整数 */ int totCnt = 0 ; /* 符合条件的正整数的个数 */ double totPjz = 0.0 ; /* 平均值 */ int ReadDat(void) ; void WriteDat(void) ; void CalValue(void) { int i; int qw,bw,sw,gw; double sum=0; for ( i=0 ; iif (xx[i] > 0) { totNum ++ ; qw = xx[i] / 1000; bw = xx[i] / 100 - qw * 10; sw = xx[i] / 10 - qw * 100- bw * 10; gw = xx[i] % 10; if ( ( (qw+bw+sw+gw) % 2)!=0) { totCnt ++ ; sum = sum + xx[i]; } } totPjz = sum / totCnt; } void main() { clrscr() ; if(ReadDat()) { printf("数据文件IN.DAT不能打开!\007\n") ; return ; } CalValue() ; printf("文件IN.DAT中共有正整数=%d个\n", totNum) ; printf("符合条件的正整数的个数=%d个\n", totCnt) ; printf("平均值=%.2lf\n", totPjz) ; WriteDat() ; } int ReadDat(void) { FILE *fp ; int i = 0 ; if((fp = fopen("in.dat", "r")) == NULL) return 1 ; while(!feof(fp)) { fscanf(fp, "%d,", &xx[i++]) ; } fclose(fp) ; return 0 ; } void WriteDat(void) { FILE *fp ; fp = fopen("OUT1.DAT", "w") ; fprintf(fp, "%d\n%d\n%.2lf\n", totNum, totCnt, totPjz) ; fclose(fp) ; } 11./* 程序PROG1.C的功能是:选出100至1000之间所有个位数字与十位数字之和被10除所得余数恰是百位数字的素数(如293)。计算并输出上述这些素数的个数cnt以及这些素数值的和sum。请考生编写函数countValue( )实现程序的要求,最后调用函数writeDAT( )把结果cnt和sum输出到文件out6.DAT中。 注意:部分源程序存放在PROG1.C中。 请勿改动主函数main( )和输出数据函数writeDAT( )的内容。 */ #include int cnt, sum ; void countValue() { } void main() { cnt = sum = 0 ; countValue() ; printf("素数的个数=%d\n", cnt) ; printf("满足条件素数值的和=%d", sum) ; writeDAT() ; } writeDAT() { FILE *fp ; fp = fopen("OUT6.DAT", "w") ; fprintf(fp, "%d\n%d\n", cnt, sum) ; fclose(fp) ; }
12. /* 编写函数sumValue( ),它的功能是:计算正整数n的所有因子(1和n除外)之和作为函数值返回。例如:n=20时,函数值为21。函数ReadWrite( )是实现从文件in9.dat中读取两个字符串,并调用函数sumValue(),最后把结果输出到文件out9.dat中。 注意:部分源程序存在文件PROG1.C中,请勿改动主函数main()和其它函数中的任何内容,仅在函数sumValue()的花括号中填入你编写的若干语句。 */ #include #include int sumValue(int n) { } main() { clrscr() ; printf("%d\n", sumValue(20)) ; ReadWrite() ; } ReadWrite() { FILE *fp, *wf ; int i, n, s ; fp = fopen("in9.dat","r") ; if(fp == NULL) { printf("数据文件in9.dat不存在!") ; return ; } wf = fopen("out9.dat","w") ; for(i = 0 ; i < 10 ; i++) { fscanf(fp, "%d", &n) ; s = sumValue(n) ; fprintf(wf, "%d\n", s) ; } fclose(fp) ; fclose(wf) ; }
|