2000年9月机试试题
1./* 程序PROG1.C的功能是:选出100至1000之间所有个位数字与十位数字之和 被10除所得余数恰是百位数字的素数(如293)。计算并输出上述这些素数的个 数cnt以及这些素数值的和sum。 请考生编写函数countValue( )实现程序的要求,最后调用函数writeDAT( )把 结果cnt和sum输出到文件out6.DAT中。 注意:部分源程序存放在PROG1.C中。 请勿改动主函数main( )和输出数据函数writeDAT( )的内容。 */ #include <stdio.h> 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) ; }
2. /* 编写函数sumValue( ),它的功能是:计算正整数n的所有因子(1和n除外)之 和作为函数值返回。 例如:n=20时,函数值为21。 函数ReadWrite( )是实现从文件in9.dat中读取两个字符串,并调用函数 sumValue(),最后把结果输出到文件out9.dat中。 注意:部分源程序存在文件PROG1.C中,请勿改动主函数main()和其它函数 中的任何内容,仅在函数sumValue()的花括号中填入你编写的若干语句。 */ #include <conio.h> #include <stdio.h> 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) ; }<此文共有7页 上一页 1 2 3 4 5 6 7 下一页 |