实验七
实验四
1 #define _CRT_SECURE_NO_WARNINGS 1 2 #include <stdio.h> 3 #include <ctype.h> 4 #include <stdlib.h> 5 6 int main() 7 { 8 FILE* fp = fopen("data4.txt", "r"); 9 if (fp == NULL) 10 { 11 printf("打开data4.txt失败!\n"); 12 system("pause"); 13 return 1; 14 } 15 int ch; 16 int line_cnt = 0; 17 int char_cnt = 0; 18 int last_line_break = 1; 19 20 while ((ch = fgetc(fp)) != EOF) 21 { 22 if (ch == '\n') 23 { 24 line_cnt++; 25 last_line_break = 1; 26 } 27 else 28 { 29 last_line_break = 0; 30 if (!isspace(ch)) 31 { 32 char_cnt++; 33 } 34 } 35 } 36 fclose(fp); 37 38 printf("data4.txt统计结果:\n"); 39 printf("行数: %d\n", line_cnt); 40 printf("字符数(不计空白符): %d\n", char_cnt); 41 42 system("pause"); 43 return 0; 44 }

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

实验六
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <time.h> 4 typedef struct { 5 long id; 6 char name[20]; 7 char cls[40]; 8 } Stu; 9 10 int main() 11 { 12 Stu all[100], win[5]; 13 int flag[100] = {0}, n = 0, i, k = 0; 14 char fname[50]; 15 FILE *fp = fopen("list.txt", "r"); 16 while(fscanf(fp, "%ld %s %[^\n]", &all[n].id, all[n].name, all[n].cls) == 3) 17 n++; 18 fclose(fp); 19 srand((unsigned)time(NULL)); 20 while(k < 5) 21 { 22 int r = rand() % n; 23 if(!flag[r]) 24 { 25 flag[r] = 1; 26 win[k++] = all[r]; 27 } 28 } 29 printf("中奖名单:\n"); 30 for(i = 0; i < 5; i++) 31 printf("%ld %s %s\n", win[i].id, win[i].name, win[i].cls); 32 printf("请输入保存文件名:"); 33 scanf("%s", fname); 34 FILE *fw = fopen(fname, "w"); 35 for(i = 0; i < 5; i++) 36 fprintf(fw, "%ld %s %s\n", win[i].id, win[i].name, win[i].cls); 37 fclose(fw); 38 printf("保存成功!\n"); 39 system("pause"); 40 return 0; 41 }

五、实验总结
学会了随机无重复抽取逻辑理解和交互式文件存储实现,也学习了统计文件中行数和字符数。
浙公网安备 33010602011771号