|
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) ; }
3./* 请编写一个函数changeStr(char *s),函数的功能是把s串中所有的字 母改写成该字母的下一个字母,字母z改写成字母a。大写仍为大写字母, 小写字母仍为小写字母,其它的字符不变。 函数ReadWrite()实现从文件in2.dat中读取两个字符串,并调用函 数changestr(),最后把结果输出到文件out2.dat中。 注意:部分源程序存在文件PROG1.C中。请勿改动主函数main()和其 它函数中的任何内容,仅在函数changeStr()的花括号中填入你编写的若干语名。*/ #include <conio.h> #include <string.h> #include <stdio.h> #include <ctype.h> #define N 81 changeStr ( char *s ) {
}
main( ) { char a[N] ; clrscr( ) ; printf ( "Enter a string : " ) ; gets ( a ) ; printf ( "The original string is : " ) ; puts( a ) ; changeStr ( a ) ; printf ( "The string after modified : ") ; puts ( a ) ; ReadWrite( ) ; }
ReadWrite( ) { int i ; char a[N] ; FILE *rf, *wf ;
rf = fopen("in2.dat", "r") ; wf = fopen("out2.dat", "w") ; for(i = 0 ; i < 10 ; i++) { fscanf(rf, "%s", a) ; changeStr(a) ; fprintf(wf, "%s\n", a) ; } fclose(rf) ; fclose(wf) ; }
4./* 程序PROG1.C的功能是:利用以下所示的简单迭代方法求方程: cos(x)-x=0的一个实根。 Xn+1=cos(Xn) 迭代步骤如下: (1) 取x1初步值为0.0; (2) x0=x1,把x1,把x1的值赋给x0; (3) x1=cos(x0),求出一个新的x1; (4) 若x0-x1的绝对值小于0.000001,执行步骤(5),否则执行步骤(2); (5) 所求x1就是方程cos(x)-x=0的一个实根,作为函数值返回。 请编写函数countValue()实现程序的要求,最后调用函数writeDAT()把 结果输出到文件out4.dat中。 注意:部分源程序存在文件PROG1.C中,请勿改动主函数main()和输出数 据函数WriteDAT()的内容。 */ #include <conio.h> #include <math.h> #include <stdio.h> float countValue() {
}
main() { clrscr(); printf("实根=%f\n", countValue()); printf(" %f\n",cos(countValue())-countValue()); writeDAT(); }
writeDAT() { FILE *wf ;
wf=fopen("out4.dat","w") ; fprintf(wf, "%f\n", countValue()) ; fclose(wf) ; }
5./* 已知在文件IN.DAT中存有若干个(个数<200)四位数字的正整数, 函数ReadDAT()读取这些正整数并存入数组xx中。 请编制函数CalValue()其功能要求是:1.求出这个文件中共有多 少个正整数totNum;2.求出这些数中的各位数字之和是奇数的数的个 数totCnt,以及不满足此条件的所有数的算术平均值totPjz,最后调 用函数WriteDAT()把所求的结果输出到文件OUT8.DAT中。 注意:部分源程序存放在PROG1.C中。 请勿改动主函数main(),读数据函数ReadDAT()和输出数据函数 WriteDAT()的内容。 */ #include <stdio.h> #include <conio.h> #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) {
}
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("OUT8.DAT", "w") ; fprintf(fp, "%d\n%d\n%.2lf\n", totNum, totCnt, totPjz) ; fclose(fp) ; }
6./* 编写一个函数findstr(),该函数统计一个长度为2的子字符串在另一个 字符串中出现的次数。例如,假定输入的字符串为“asd asasdfg asd as zx67 asd mklo”,子字符串为“as”,则输出6。 函数ReadWrite()实现从文件in1.dat中读取两个字符串,并调用函数 findStr(),最后把结果输出到文件out1.dat中。 注意:部分源程序存在文件PROG1.C中。请勿改动主函数main()和其它 函数中的任何内容,仅在函数findStr()的花括号中填入你编写的若干语句。*/ #include <stdio.h> #include <string.h> #include <conio.h>
int findStr(char *str,char *substr) {
}
main() { char str[81], substr[3] ; int n ;
clrscr() ; gets(str) ; gets(substr) ; puts(str) ; puts(substr) ; n=findStr(str, substr) ; printf("n=%d\n", n) ; ReadWrite() ; }
ReadWrite() { char str[81], substr[3], ch; int n, len, i = 0; FILE *rf, *wf ;
rf = fopen("in1.dat", "r") ; wf = fopen("out1.dat", "w") ; while(i < 5) { fgets(str, 80, rf) ; fgets(substr, 10, rf) ; len = strlen(substr) - 1 ; ch = substr[len] ; if(ch == '\n' || ch == 0x1a) substr[len] = 0 ; n=findStr(str, substr); fprintf(wf, "%d\n", n) ; i++ ; } fclose(rf) ; fclose(wf) ; }
7./* 请编写函数Void countValue(int *a,int *n),它的功能是: 求出1到1000之内能被7或11整除但不能同时被7和11整除的所有整数, 并放在数组a中,然后通过n返回这些数的个数。 注意:部分源程序存入在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) ; }
8./* 请编写一个函数changestr(char *s),函数的功能是把s串中所有的字 符前移一个位置,串中的第一个字符移到最后。 例如:s串中原有的字符串为:Mn.123xyZ,则调用该函数后,s串中的 内容为:n.123xyZM。 函数ReadWrite()实现从文件in3.dat中读取两个字符串,并调用函数 changeStr(),最后把结果输出到文件out3.dat中。 注意:部分源程序存放在文件PROG1.C中。 请勿改动主函数main()和其它函数中的任何内容,仅在函数 changeStr()的花括号中填入你编写的若干语句。*/ #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) ; }
9./* 请编制函数CalValue(),其功能要求是:1.求出这个文件中共有多 少个正整数totNum;2.求这些数中的十位数位置上的数字2、4和8的数的 个数totCnt;以及不满足此条件的所有数的平均值totPjz,最后调用函 数WriteDAT()把所求的结果输出到文件out7.DAT中。 注意:部分源程序存放在PROG1.C中。 请勿改动主函数main(),读数据函数ReadDAT()和输出数据函数 WriteDAT()的内容。*/ #include <stdio.h> #include <conio.h> #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) {
}
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("OUT7.DAT", "w") ; fprintf(fp, "%d\n%d\n%.2lf\n", totNum, totCnt, totPjz) ; fclose(fp) ; }
10./* 已知在文件IN.BAT中存有N个实数(N<200),函数ReadDAT()读取 这N个实数并存入数组XX中。 请编制函数CalValue(),其功能要求是:1.求出这N个实数的平 均值aver;2.分别求出这N个实数的整数部分值的平均值averint以 及其小数部分值的平均值averdel,最后调用函数WriteDAT()把所求 的结果输出到文件out10.DAT中。 注意:部分源程序存放在文件PROG1.C中。 请勿改动主函数main(),读数据函数ReadDAT()和输出数据函数 WreitDAT的内容。*/ #include <stdio.h> #include <conio.h> #define MAXNUM 200
float xx[MAXNUM] ; int N = 0 ; /* 文件IN.DAT中共有多少个实数 */ double aver = 0.0 ; /* 平均值 */ double averint = 0.0 ; /* 整数部分值的平均值 */ double averdec = 0.0 ; /* 小数部分值的平均值 */ int ReadDat(void) ; void WriteDat(void) ;
void CalValue(void) {
}
void main() { clrscr() ; if(ReadDat()) { printf("数据文件IN.DAT不能打开!\007\n") ; return ; } CalValue() ; printf("文件IN.DAT中共有实数%d个\n", N) ; printf("平均值=%.2lf\n", aver) ; printf("整数部分值的平均值=%.2lf\n", averint) ; printf("小数部分值的平均值=%.2lf\n", averdec) ; WriteDat() ; }
int ReadDat(void) { FILE *fp ; int j ;
if((fp = fopen("in.dat", "r")) == NULL) return 1 ; while(!feof(fp)) { fscanf(fp, "%f,", &xx[N]) ; if(xx[N] > 0.001) N++ ; } fclose(fp) ; return 0 ; }
void WriteDat(void) { FILE *fp ;
fp = fopen("OUT10.DAT", "w") ; fprintf(fp, "%d\n%.2lf\n%.2lf\n%.2lf\n", N, aver, averint, averdec) ; fclose(fp) ; }
|