实验7
task4.c
源代码
1 #include<stdio.h> 2 3 void read_h(); 4 void read_z(); 5 6 int main(){ 7 printf("data4.txt统计结果:\n"); 8 printf("行数:"); 9 read_h(); 10 printf("字符数(不计算空白符):"); 11 read_z(); 12 return 0; 13 } 14 15 16 17 void read_h(){ 18 FILE *fp; 19 int t=0; 20 char ch; 21 22 fp=fopen("data4.txt","r"); 23 if(!fp) { 24 printf("fail to open file to read\n"); 25 return; 26 } 27 while(!feof(fp)) { 28 ch = fgetc(fp); 29 if(ch =='\n'||ch==EOF) 30 t++; 31 } 32 printf("%d\n",t); 33 fclose(fp); 34 } 35 36 void read_z(){ 37 FILE *fp; 38 int t=0; 39 char ch; 40 41 fp=fopen("data4.txt","r"); 42 if(!fp) { 43 printf("fail to open file to read\n"); 44 return; 45 } 46 while(!feof(fp)) { 47 ch = fgetc(fp); 48 if(ch != '\n'&&ch!=' '&&ch!=' '&&ch!=EOF) 49 t++; 50 } 51 printf("%d",t); 52 fclose(fp); 53 }
运行结果

task5.c
源代码
1 ocess(stu, N, stu_pass); 2 3 printf("\n通过考试的名单:\n"); 4 output(stu, N); // 输出所有考生完整信息到屏幕 5 write(stu, N); // 输出考试通过的考生信息到文件 6 7 pass_rate = 1.0 * cnt / N; 8 printf("\n本次等级考试通过率: %.2f%%\n", pass_rate*100); 9 10 return 0; 11 } 12 13 // 把所有考生完整信息输出到屏幕上 14 // 准考证号,姓名,客观题得分,操作题得分,总分,结果 15 void output(STU st[], int n) { 16 int i; 17 18 printf("准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n"); 19 for (i = 0; i < n; i++) 20 printf("%ld\t\t%s\t%.2f\t\t%.2f\t\t%.2f\t\t%s\n", st[i].id, st[i].name, st[i].objective, st[i].subjective, st[i].sum, st[i].result); 21 } 22 23 // 从文本文件examinee.txt读入考生信息:准考证号,姓名,客观题得分,操作题得分 24 void read(STU st[], int n) { 25 int i; 26 FILE *fin; 27 28 fin = fopen("examinee.txt", "r"); 29 if (!fin) { 30 printf("fail to open file\n"); 31 return; 32 } 33 34 while (!feof(fin)) { 35 for (i = 0; i < n; i++) 36 fscanf(fin, "%ld %s %f %f", &st[i].id, st[i].name, &st[i].objective, &st[i].subjective); 37 } 38 39 fclose(fin); 40 } 41 42 // 把通过考试的考生完整信息写入文件list_pass.txt 43 // 准考证号,姓名,客观题得分,操作题得分,总分,结果 44 void write(STU s[], int n) { 45 FILE *fp; 46 int i; 47 48 fp=fopen("list_pass.txt","w"); 49 if(!fp) { 50 printf("fail to open file to write\n"); 51 return; 52 } 53 fprintf(fp,"准考证号 姓名 客观题得分 操作题得分 总分 结果\n"); 54 for (i = 0; i < N; i++) 55 if(s[i].sum>=60) 56 fprintf(fp,"%-20ld%-10s\t%-20.2f%-20.2f%-20.2f%s\n", s[i].id, s[i].name, s[i].objective, s[i].subjective, s[i].sum, s[i].result); 57 58 59 fclose(fp); 60 } 61 62 // 对考生信息进行处理:计算每位考生考试总分、结果;统计考试通过的人数 63 int process(STU st[], int n, STU st_pass[]) { 64 int count=0,i; 65 for(i=0;i<n;i++){ 66 st[i].sum=st[i].objective+st[i].subjective; 67 if(st[i].sum>=60){ 68 strcpy(st[i].result,"通过"); 69 st_pass[count++]=st[i]; 70 } 71 else 72 strcpy(st[i].result,"不通过"); 73 } 74 return count; 75 }
运行结果


task6.c
源代码
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<time.h> 4 #define N 80 5 #define M 5 6 typedef struct{ 7 long id; 8 char name[20]; 9 char cname[20]; 10 int sign; 11 }stu; 12 13 void read(stu s[],int n); 14 void choose(stu s[],stu ss[],int n,int m); 15 void save(stu ss[],int m,char fname[]); 16 17 int main(){ 18 stu s[N],ss[M]; 19 int i; 20 char fname[50]; 21 for(i=0;i<N;i++) 22 s[i].sign=0; 23 printf("------------随机抽点名单-----------\n"); 24 read(s,N); 25 choose(s,ss,N,M); 26 for(i=0;i<M;i++) 27 printf("%-16ld%-8s%s\n",ss[i].id,ss[i].name,ss[i].cname); 28 printf("\n------------保存到文件-----------\n"); 29 printf("输入文件名:"); 30 gets(fname); 31 save(ss,M,fname); 32 printf("保存成功"); 33 system("pause"); 34 return 0; 35 } 36 37 void read(stu s[],int n){ 38 FILE *fp; 39 int i; 40 if((fp=fopen("list.txt","r"))==NULL){ 41 printf("fail to open file\n"); 42 return; 43 } 44 for(i=0;i<n;i++) 45 fscanf(fp,"%ld %s %s",&s[i].id,s[i].name,s[i].cname); 46 fclose(fp); 47 } 48 49 void choose(stu s[],stu ss[],int n,int m){ 50 int i,random; 51 srand(time(NULL)); 52 for(i=0;i<m;i++){ 53 do{ 54 random=rand()%80; 55 }while(s[random].sign==1); 56 s[random].sign=1; 57 ss[i]=s[random]; 58 } 59 } 60 61 void save(stu ss[],int m,char fname[]){ 62 FILE *fp; 63 int i; 64 if((fp=fopen(fname,"w"))==NULL){ 65 printf("fail to open file\n"); 66 return; 67 } 68 for(i=0;i<m;i++) 69 fprintf(fp,"%-16ld%-8s%s\n",ss[i].id,ss[i].name,ss[i].cname); 70 fclose(fp); 71 }
运行结果



浙公网安备 33010602011771号