实验七
实验任务4
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<string.h> 4 int main() 5 { 6 FILE *fp; 7 int line=0,len=0; 8 char ch[1000]; 9 char c; 10 fp=fopen("C:\\Users\\wenov\\Desktop\\data4.txt","r"); 11 while(fgets(ch,sizeof(ch),fp)!=NULL) 12 { 13 line++; 14 } 15 rewind(fp); 16 while((c=fgetc(fp))!=EOF) 17 { 18 if(c!=' ') 19 len++; 20 } 21 len=len-(line-1); 22 fclose(fp); 23 printf("data4.txt统计结果:\n"); 24 printf("行数:\t%d\n",line); 25 printf("字符数(不计空白符):\t%d",len); 26 return 0; 27 }
实验结论

实验任务5
1 #include <stdio.h> 2 #include <string.h> 3 #define N 10 4 typedef struct { 5 long id; // 准考证号 6 char name[20]; // 姓名 7 float objective; // 客观题得分 8 float subjective; // 操作题得分 9 float sum; // 总分 10 char result[10]; // 考试结果 11 } STU; 12 // 函数声明 13 void read(STU st[], int n); 14 void write(STU st[], int n); 15 void output(STU st[], int n); 16 int process(STU st[], int n, STU st_pass[]); 17 int main() { 18 STU stu[N], stu_pass[N]; 19 int cnt; 20 double pass_rate; 21 printf("从文件读入%d个考生信息...\n", N); 22 read(stu, N); 23 printf("\n对考生成绩进行统计...\n"); 24 cnt = process(stu, N, stu_pass); 25 printf("\n通过考试的名单:\n"); 26 output(stu, N); // 输出所有考生完整信息到屏幕 27 write(stu, N); // 输出考试通过的考生信息到文件 28 pass_rate = 1.0 * cnt / N; 29 printf("\n本次等级考试通过率: %.2f%%\n", pass_rate*100); 30 return 0; 31 } 32 // 把所有考生完整信息输出到屏幕上 33 // 准考证号,姓名,客观题得分,操作题得分,总分,结果 34 void output(STU st[], int n) { 35 int i; 36 printf("准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n"); 37 for (i = 0; i < n; i++) 38 printf("%ld\t\t%s\t%.2f\t\t%.2f\t\t%.2f\t\t%s\n", st[i].id, st[i].name, 39 st[i].objective, st[i].subjective, st[i].sum, st[i].result); 40 } 41 // 从文本文件examinee.txt读入考生信息:准考证号,姓名,客观题得分,操作题得分 42 void read(STU st[], int n) { 43 int i; 44 FILE *fin; 45 fin = fopen("examinee.txt", "r"); 46 if (!fin) { 47 printf("fail to open file\n"); 48 return; 49 } 50 for (i = 0; i < n; i++) 51 fscanf(fin, "%ld %s %f %f", &st[i].id, st[i].name, &st[i].objective, 52 &st[i].subjective); 53 fclose(fin); 54 } 55 // 把通过考试的考生完整信息写入文件list_pass.txt 56 // 准考证号,姓名,客观题得分,操作题得分,总分,结果 57 void write(STU s[], int n) { 58 // 待补足 59 int i; 60 FILE *fout; 61 fout = fopen("list_pass.txt","w"); 62 if(!fout){ 63 printf("fail to creat file\n"); 64 return; 65 } 66 fprintf(fout,"准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n"); 67 for(i=0;i<n;i++){ 68 fprintf(fout, "%ld\t\t%s\t%.2f\t\t%.2f\t\t%.2f\t\t%s\n", 69 s[i].id, 70 s[i].name, 71 s[i].objective, 72 s[i].subjective, 73 s[i].sum, 74 s[i].result); 75 } 76 fclose(fout); 77 printf("通过名单已成功写入文件 list_pass.txt\n"); 78 } 79 // 对考生信息进行处理:计算每位考生考试总分、结果;统计考试通过的人数 80 int process(STU st[], int n, STU st_pass[]) { 81 // 待补足 82 int i; 83 int pass_count=0; 84 for(i=0;i<n;i++){ 85 st[i].sum=st[i].objective+st[i].subjective; 86 if(st[i].sum>=60){ 87 strcpy(st[i].result,"通过"); 88 st_pass[pass_count]=st[i]; 89 pass_count++; 90 } 91 else{ 92 strcpy(st[i].result,"未通过"); 93 } 94 return pass_count; 95 } 96 }
实验结论

实验任务6
1 #include <stdio.h> 2 #include<stdlib.h> 3 #include<time.h> 4 #include<string.h> 5 #define N 100 6 #define M 5 7 int main(){ 8 char s[N][N]; 9 char hit[M][N]; 10 int has_hit[N]={0}; 11 FILE *fin,*fout; 12 int i,n,lucky_i,cnt=0; 13 char filename[80]; 14 fin = fopen("list.txt","r"); 15 if(!fin){ 16 perror("list.txt"); 17 return 1; 18 19 } 20 21 i=0; 22 while(fgets(s[i],N ,fin)!=NULL){ 23 24 ++i;} 25 n=i; 26 27 srand(time(0)); 28 cnt=0; 29 for(i=0;i<M;++i){ 30 lucky_i=rand()%n; 31 if(has_hit[lucky_i]) 32 continue; 33 has_hit[lucky_i]=1; 34 strcpy(hit[i],s[lucky_i]); 35 36 cnt++; 37 38 } 39 printf("中奖名单:\n"); 40 for(i=0;i<M;++i) 41 printf("%s",hit[i]); 42 printf("Enter fileName:"); 43 gets(filename); 44 fout=fopen(filename,"w"); 45 if(!fout){ 46 perror(filename); 47 return 1; 48 } 49 for(i=0;i<M;++i) 50 fprintf(fout,"%s",hit[i]); 51 fclose(fin); 52 fclose(fout); 53 return 0; 54 }
实验结论


浙公网安备 33010602011771号