2022年C语言文件 .pdf
42C10. 已知数据文件 中存放有 1100之间各自然数n 的平方根, (文件中每行只存储一个数值数据) 。编程查找当n=7时其平方根(7 的平方根等于2.6458)在文件 中的位置(即记录号) ,并向文件t2.dat 输出该记录号。#include stdio.h #include stdlib.h #include math.h void main() FILE *fp,*fp1; double num; int i=0; if(fp=(fopen(d:12345678dat2.dat,rb+)=NULL) printf(cannot open the file!n); exit(0); if(fp1=(fopen(d:12345678t2.dat,wb+)=NULL) printf(cannot open the file!n); exit(0); while(feof(fp)=0) fscanf(fp,%6lfn,&num); i+; if(num=2.6458) fprintf(fp1,the number is:%d,i); printf(the num is:%d,i); break; fclose(fp); fclose(fp1); /the number is:8834C2. 已知在正文文件da1.dat中,每个纪录只有两项数据,第一项为一整数表示学生的学号, 第二项为形如xx.x 的一个实数, 试统计计算并向文件t2.dat 输出全部学生的平均成绩V 与 90 分以上 (含 90 分)的学生人数N 。#include stdio.h #include stdlib.h struct stu int num; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 1 页,共 5 页 - - - - - - - - - float score; stud; void main() FILE *fp,*fp1; int N=0,n=0; float sum=0,V=0; if(fp=fopen(D:12345678da1.dat,rb+)=NULL) printf(cannot open the file!n); exit(0); if(fp1=fopen(D:12345678t2.dat,wb+)=NULL) printf(cannot open the file!n); exit(0); while(feof(fp)=0) fscanf(fp,%4d %5fn,&stud.num,&stud.score); printf(%4d %5.1fn,stud.num,stud.score); sum+=stud.score; n+; if(stud.score=90) N+; V=sum/n; fprintf(fp1,average=%5.1f,90=%d,V,N); fclose(fp); fclose(fp1); /average= 65.5,90=7235C3. 已知在正文文件ch1.dat中, 每个记录的数据是有一个由字母组成的字符个数不多于 10 个的字符串,如:absolute。试查找文件中从第150 个记录到第250 个记录间出现的第一个最大的字符串,并向文件t2.dat 输出查找结果和该字符串中的字符个数n。36C4. 已知在正文文件ch1.dat 中,每个记录的数据是有一个由字母组成的字符个数不多于 10 个的字符串,如:absolute。统计在该文件中只有4 个字符的字符串的个数n1 和字符串的最后一个字符是f 的字符串的个数n2,并将统计结果存入文件t2.dat 中。/ n1=54,n2=1738C6. 已知在正文文件da1.dat中,每个纪录只有两项数据,第一项为一整数表示学生的学名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 2 页,共 5 页 - - - - - - - - - 号,第二项为形如xx.x 的一个实数,试统计计算并向文件t2.dat 输出成绩排序在前20 名的学生的平均成绩V。#include stdio.h #include stdlib.h void main() FILE *fp,*fp1; int i=0,j=0,k=0,num1000; float temp=0,sum=0,V=0,score1000; if(fp=fopen(D:12345678da1.dat,rb+)=NULL) printf(cannot open the file!n); exit(0); if(fp1=fopen(D:12345678t2.dat,wb+)=NULL) printf(cannot open the file!n); exit(0); while(feof(fp)=0) fscanf(fp,%4d %5fn,&numi,&scorei); printf(%d %fn,numi,scorei); i+; k=i; for(i=0;ik;i+) for(j=i+1;jk;j+) if(scoreiscorej) temp=scorei; scorei=scorej; scorej=temp; for(i=0;i20;i+) sum+=scorei; V=sum/20; fprintf(fp1,average=%5.1f,V); fclose(fp); fclose(fp1); /average= 99.1 38C6. 已知在正文文件da1.dat中,每个纪录只有两项数据,第一项为一整数表示学生的学名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 3 页,共 5 页 - - - - - - - - - 号,第二项为形如xx.x 的一个实数,试统计计算并向文件t2.dat 输出成绩排序在后20 名的学生的平均成绩V。/average= 31.935C3. 已知在正文文件ch1.dat中, 每个记录的数据是有一个由字母组成的字符个数不多于10 个的字符串,如:absolute。试查找文件中从第50 个记录到第150 个记录间出现的第一个最大的字符串,并向文件t2.dat 输出查找结果和该字符串中的字符个数n。/str=xpkaglnbzz,num=1043C11. 已知数据文件 中已存储有50 个学生的学号(整型)与一门课程的成绩 (实型), 编程将该文件中50个学生的学号与成绩分别赋给整型数组num和实型数组grade,计算并输出第21-30名学生的平均分。#include stdio.h #include stdlib.h struct stu int num; float grade; stud; void main() FILE *fp; int num50,i; float grade50,avg=0; if(fp=fopen(D:12345678dat3.dat,rb+)=NULL) printf(cannot open the file!n); exit(0); for(i=0;i19&i30) avg+=gradei; printf(avg=%.2f,avg/10); fclose(fp); /avg=68.70 42C10. 已知数据文件 中存放有 1100之间各自然数n 的平方根, (文件中每行只存储一个数值数据) 。编程查找当n=5时其平方根(5 的平方根等于2.2361)在文件名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 4 页,共 5 页 - - - - - - - - - 中的位置(即记录号) ,并向文件t2.dat 输出该记录号。/the number is:2543C11. 已知数据文件 中已存储有50 个学生的学号(整型)与一门课程的成绩 (实型), 编程将该文件中50个学生的学号与成绩分别赋给整型数组num和实型数组grade,计算并输出第11-20名学生的平均分。/avg=79.30 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 5 页,共 5 页 - - - - - - - - -