实验7
任务4:
代码:
1 #include <stdio.h> 2 #include <ctype.h> 3 4 int main() { 5 FILE *fp; 6 char ch; 7 int lines = 0; 8 int characters = 0; 9 10 fp = fopen("data4.txt", "r"); 11 if (fp == NULL) { 12 perror("Error opening file"); 13 return 1; 14 } 15 16 while ((ch = fgetc(fp))!= EOF) { 17 if (ch == '\n') { 18 lines++; 19 } 20 if (!isspace(ch)) { 21 characters++; 22 } 23 } 24 // 处理文件末尾没有换行符的情况 25 if (characters > 0) { 26 lines++; 27 } 28 29 fclose(fp); 30 31 printf("data4.txt统计结果:\n"); 32 printf("行数:%d\n", lines); 33 printf("字符数(不计空白符):%d\n", characters); 34 35 return 0; 36 }
截图:
任务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 s[], int n) { 74 int i; 75 FILE *fout; 76 fout = fopen("list_pass.txt", "w"); 77 if (!fout) { 78 printf("fail to open file\n"); 79 return; 80 } 81 fprintf(fout, "准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n"); 82 for (i = 0; i < n; i++) { 83 if (strcmp(s[i].result, "Pass") == 0) { 84 fprintf(fout, "%ld\t\t%s\t%.2f\t\t%.2f\t\t%.2f\t\t%s\n", s[i].id, s[i].name, s[i].objective, s[i].subjective, s[i].sum, s[i].result); 85 } 86 } 87 fclose(fout); 88 } 89 90 91 // 对考生信息进行处理:计算每位考生考试总分、结果;统计考试通过的人数 92 int process(STU st[], int n, STU st_pass[]) { 93 int i, cnt = 0; 94 for (i = 0; i < n; i++) { 95 st[i].sum = st[i].objective + st[i].subjective; 96 if (st[i].sum >= 60) { 97 strcpy(st[i].result, "通过"); 98 st_pass[cnt] = st[i]; 99 cnt++; 100 } else { 101 strcpy(st[i].result, "不通过"); 102 } 103 } 104 return cnt; 105 }
截图:
任务6:
代码:
1 #define _CRT_SECURE_NO_WARNINGS 2 #include <stdio.h> 3 #include <stdlib.h> 4 #include <time.h> 5 6 #define STUDENT_NUM 80 7 #define SELECT_NUM 5 8 9 typedef struct { 10 int id; 11 char name[20]; 12 char class_name[50]; 13 } Student; 14 15 int main(void) { 16 Student students[STUDENT_NUM]; 17 Student winners[SELECT_NUM]; 18 int flag[STUDENT_NUM] = {0}; 19 int i; 20 int index; 21 char filename[100]; 22 FILE *fin; 23 FILE *fout; 24 25 fin = fopen("list.txt", "r"); 26 if (fin == NULL) { 27 printf("无法打开文件list.txt\n"); 28 return 1; 29 } 30 31 for (i = 0; i < STUDENT_NUM; i++) { 32 if (fscanf(fin, "%d %19s %49s", 33 &students[i].id, students[i].name, students[i].class_name) != 3) { 34 printf("list.txt数据不足或格式错误\n"); 35 fclose(fin); 36 return 1; 37 } 38 } 39 fclose(fin); 40 41 srand((unsigned int)time(NULL)); 42 43 for (i = 0; i < SELECT_NUM; i++) { 44 do { 45 index = rand() % STUDENT_NUM; 46 } while (flag[index] == 1); 47 48 flag[index] = 1; 49 winners[i] = students[index]; 50 } 51 52 printf("随机抽点的学生信息:\n"); 53 for (i = 0; i < SELECT_NUM; i++) { 54 printf("学号:%d,姓名:%s,班级:%s\n", 55 winners[i].id, winners[i].name, winners[i].class_name); 56 } 57 58 printf("请输入文件名:"); 59 scanf("%99s", filename); 60 61 fout = fopen(filename, "w"); 62 if (fout == NULL) { 63 printf("无法打开文件%s\n", filename); 64 return 1; 65 } 66 67 fprintf(fout, "随机抽点的学生信息:\n"); 68 for (i = 0; i < SELECT_NUM; i++) { 69 fprintf(fout, "学号:%d,姓名:%s,班级:%s\n", 70 winners[i].id, winners[i].name, winners[i].class_name); 71 } 72 73 fclose(fout); 74 75 return 0; 76 }
截图:


浙公网安备 33010602011771号