实验7 文件应用编程
实验任务4
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 int main() { 5 FILE *fp; 6 char ch; 7 int lines = 0, characters = 0; 8 9 fp = fopen("data4.txt", "r"); 10 if (fp == NULL) { 11 printf("无法打开文件!\n"); 12 return 1; 13 } 14 15 while ((ch = fgetc(fp))!= EOF) { 16 if (ch!= '\n' && ch!= '\t' && ch!= ' ') { 17 characters++; 18 } 19 if (ch == '\n') { 20 lines++; 21 } 22 } 23 fclose(fp); 24 lines++; 25 26 printf("data4.txt统计结果:\n"); 27 printf("行数:%d\n", lines); 28 printf("字符数(不计空白符):%d\n", characters); 29 30 return 0; 31 }
实验任务5
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 write(stu, N); // 输出考试通过的考生信息到文件 35 36 pass_rate = 1.0 * cnt / N; 37 printf("\n本次等级考试通过率: %.2f%%\n", pass_rate*100); 38 39 return 0; 40 } 41 42 // 把所有考生完整信息输出到屏幕上 43 // 准考证号,姓名,客观题得分,操作题得分,总分,结果 44 void output(STU st[], int n) { 45 int i; 46 47 printf("准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n"); 48 for (i = 0; i < n; i++) 49 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); 50 } 51 52 // 从文本文件examinee.txt读入考生信息:准考证号,姓名,客观题得分,操作题得分 53 void read(STU st[], int n) { 54 int i; 55 FILE *fin; 56 57 fin = fopen("examinee.txt", "r"); 58 if (!fin) { 59 printf("fail to open file\n"); 60 return; 61 } 62 63 while (!feof(fin)) { 64 for (i = 0; i < n; i++) 65 fscanf(fin, "%ld %s %f %f", &st[i].id, st[i].name, &st[i].objective, &st[i].subjective); 66 } 67 68 fclose(fin); 69 } 70 71 // 把通过考试的考生完整信息写入文件list_pass.txt 72 // 准考证号,姓名,客观题得分,操作题得分,总分,结果 73 void write(STU st[], int n) { 74 FILE *fout; 75 fout = fopen("list_pass.txt", "w"); 76 if (!fout) { 77 printf("fail to open file\n"); 78 return; 79 } 80 int i; 81 fprintf(fout, "准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n"); 82 for (i = 0; i < n; i++) { 83 if (st[i].sum >= 60) { 84 fprintf(fout, "%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); 85 } 86 } 87 fclose(fout); 88 } 89 90 91 // 对考生信息进行处理:计算每位考生考试总分、结果;统计考试通过的人数 92 int process(STU st[], int n, STU st_pass[]) { 93 int cnt = 0; 94 int i, j = 0; 95 for (i = 0; i < n; i++) { 96 st[i].sum = st[i].objective + st[i].subjective; 97 if (st[i].sum >= 60) { 98 strcpy(st[i].result, "通过"); 99 st_pass[j] = st[i]; 100 j++; 101 cnt++; 102 } else { 103 strcpy(st[i].result, "未通过"); 104 } 105 } 106 return cnt; 107 }
实验任务6
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <time.h> 4 5 #define STUDENT_NUM 80 6 7 // 学生结构体 8 typedef struct { 9 int id; 10 char name[20]; 11 char class[20]; 12 } Student; 13 14 // 从文件中读取学生信息 15 void read(Student students[]); 16 17 // 随机抽取学生信息 18 void randomSelect(Student students[], Student selected[], int num); 19 20 // 在屏幕上显示学生信息 21 void display(Student selected[], int num); 22 23 // 将学生信息写入文件 24 void writeStudents(Student selected[], int num, char *filename); 25 26 int main() { 27 Student students[STUDENT_NUM]; 28 Student selected[5]; 29 char filename[50]; 30 31 32 read(students); 33 34 randomSelect(students, selected, 5); 35 36 display(selected, 5); 37 38 printf("请输入文件名:"); 39 scanf("%s", filename); 40 writeStudents(selected, 5, filename); 41 42 return 0; 43 } 44 45 void read(Student students[]) { 46 FILE *fp; 47 int i; 48 fp = fopen("list.txt", "r"); 49 if (fp == NULL) { 50 printf("无法打开文件list.txt\n"); 51 exit(1); 52 } 53 for (i = 0; i < STUDENT_NUM; i++) { 54 fscanf(fp, "%d %s %s", &students[i].id, students[i].name, students[i].class); 55 } 56 fclose(fp); 57 } 58 59 void randomSelect(Student students[], Student selected[], int num) { 60 int selectedIndex[STUDENT_NUM] = {0}; 61 srand((unsigned int)time(NULL)); 62 int i; 63 for (i = 0; i < num; i++) { 64 int index; 65 do { 66 index = rand() % STUDENT_NUM; 67 } while (selectedIndex[index] == 1); 68 selectedIndex[index] = 1; 69 selected[i] = students[index]; 70 } 71 } 72 73 void display(Student selected[], int num) { 74 printf("随机抽取的学生信息:\n"); 75 int i; 76 for (i = 0; i < num; i++) { 77 printf("学号:%d,姓名:%s,班级:%s\n", selected[i].id, selected[i].name, selected[i].class); 78 } 79 } 80 81 void writeStudents(Student selected[], int num, char *filename) { 82 FILE *fp; 83 int i; 84 fp = fopen(filename, "w"); 85 if (fp == NULL) { 86 printf("无法打开文件%s\n", filename); 87 exit(1); 88 } 89 fprintf(fp, "随机抽取的学生信息:\n"); 90 for (i = 0; i < num; i++) { 91 fprintf(fp, "学号:%d,姓名:%s,班级:%s\n", selected[i].id, selected[i].name, selected[i].class); 92 } 93 fclose(fp); 94 }