实验7
任务4
代码
1 #include <stdio.h> 2 #include <ctype.h> 3 4 int main() { 5 FILE *file; 6 int count = 0; 7 int ch; 8 9 file = fopen("data4.txt", "r"); 10 if (file == NULL) { 11 printf("Could not open file.\n"); 12 return 1; 13 } 14 15 while ((ch = fgetc(file)) != EOF) { 16 if (!isspace(ch)) { 17 ++count; 18 } 19 } 20 21 fclose(file); 22 printf("data4.txt中共包含字符数(不计空白符): %d\n", count); 23 return 0; 24 }
运行截图
任务5
代码
1 #include <stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 4 5 #define N 10 6 7 typedef struct { 8 long int id; 9 char name[20]; 10 float objective; // 客观题得分 11 float subjective; // 操作题得分 12 float sum; // 总分 13 char ans[10]; // 考试结果 14 } STU; 15 16 // 函数声明 17 void finput(STU st[], int n); 18 void foutput(STU st[], int n); 19 void output(STU st[], int n); 20 int process(STU st[], int n, STU st_pass[]); 21 22 int main() { 23 STU stu[N], stu_pass[N]; 24 int cnt; 25 double pass_rate; 26 27 printf("从文件读入%d个考生信息...\n", N); 28 finput(stu, N); 29 30 printf("\n对考生成绩进行统计...\n"); 31 cnt = process(stu, N, stu_pass); 32 33 printf("\n通过考试的名单:\n"); 34 output(stu, N); // 输出到屏幕 35 foutput(stu, N); // 输出到文件 36 37 pass_rate = 1.0 * cnt / N; 38 printf("\n本次等级考试通过率: %.2f%%\n", pass_rate*100); 39 40 return 0; 41 } 42 43 // 把通过考试的考生完整信息输出到屏幕上 44 // 准考证号,姓名,客观题得分,操作题得分,总分,结果 45 void output(STU st[], int n) { 46 int i; 47 48 printf("准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n"); 49 for (i = 0; i < n; i++) 50 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].ans); 51 } 52 53 // 从文本文件examinee.txt读入考生信息:准考证号,姓名,客观题得分,操作题得分 54 void finput(STU st[], int n) { 55 int i; 56 FILE *fin; 57 58 fin = fopen("examinee.txt", "r"); 59 if (fin == NULL) { 60 printf("fail to open file\n"); 61 exit(0); 62 } 63 64 while (!feof(fin)) { 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 69 fclose(fin); 70 } 71 72 // 把通过考试的考生完整信息写入文件list_pass.txt 73 // 准考证号,姓名,客观题得分,操作题得分,总分,结果 74 void foutput(STU s[], int n) { 75 FILE *fout; 76 int i; 77 78 // 保存到文件 79 fout = fopen("list_pass.txt", "w"); 80 if (!fout) { 81 printf("fail to open or create list_pass.txt\n"); 82 exit(0); 83 } 84 85 fprintf(fout, "准考证号\t\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n"); 86 87 for (i = 0; i < n; i++) 88 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].ans); 89 90 fclose(fout); 91 } 92 93 94 95 // 对考生信息进行处理:计算每位考生考试总分、结果;统计考试通过的人数 96 int process(STU st[], int n, STU st_pass[]) { 97 int pass_count = 0; 98 int i; 99 for (i = 0; i < n; i++) { 100 // 计算总分 101 st[i].sum = st[i].objective + st[i].subjective; 102 103 // 判断考试结果 104 if (st[i].sum >= 60) { 105 strcpy(st[i].ans, "通过"); 106 // 复制通过的考生信息到st_pass数组 107 st_pass[pass_count] = st[i]; 108 pass_count++; 109 } else { 110 strcpy(st[i].ans, "未通过"); 111 } 112 } 113 return pass_count; 114 }
运行截图
任务6
代码1
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 #include <time.h> 5 6 #define MAX_STUDENTS 80 7 #define LUCKY_COUNT 5 8 9 // 读取学生名单 10 void readStudents(char students[MAX_STUDENTS][50], int *count) { 11 FILE *file = fopen("list.txt", "r"); 12 if (!file) { 13 printf("无法打开文件 list.txt\n"); 14 exit(1); 15 } 16 17 while (!feof(file) && *count < MAX_STUDENTS) { 18 fgets(students[*count], 50, file); 19 // 去除换行符 20 students[*count][strcspn(students[*count], "\n")] = 0; 21 (*count)++; 22 } 23 24 fclose(file); 25 } 26 27 // 随机抽取学生并写入文件 28 void pickLuckyStudents(char students[MAX_STUDENTS][50], int count) { 29 FILE *file = fopen("lucky.txt", "w"); 30 if (!file) { 31 printf("无法创建文件 lucky.txt\n"); 32 exit(1); 33 } 34 35 printf("幸运学生名单:\n"); 36 for (int i = 0; i < LUCKY_COUNT; i++) { 37 int randomIndex = rand() % count; 38 printf("%s\n", students[randomIndex]); 39 fprintf(file, "%s\n", students[randomIndex]); 40 } 41 42 fclose(file); 43 } 44 45 int main() { 46 char students[MAX_STUDENTS][50]; 47 int count = 0; 48 49 srand((unsigned int)time(NULL)); // 初始化随机数发生器 50 readStudents(students, &count); 51 pickLuckyStudents(students, count); 52 53 return 0; 54 }
运行截图
代码2、
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 #include <time.h> 5 6 #define MAX_STUDENTS 80 7 #define LUCKY_COUNT 5 8 9 typedef struct { 10 long id; // 学号 11 char name[50]; // 姓名 12 char class[50]; // 班级 13 } Student; 14 15 // 读取学生名单 16 int readStudents(Student students[MAX_STUDENTS]) { 17 FILE *file = fopen("list.txt", "r"); 18 int count = 0; 19 if (!file) { 20 printf("无法打开文件 list.txt\n"); 21 exit(1); 22 } 23 24 while (fscanf(file, "%ld %s %[^\n]", &students[count].id, students[count].name, students[count].class) != EOF && count < MAX_STUDENTS) { 25 count++; 26 } 27 28 fclose(file); 29 return count; 30 } 31 32 // 比较两个学生(用于排序) 33 int compareStudents(const void *s1, const void *s2) { 34 Student *student1 = (Student *)s1; 35 Student *student2 = (Student *)s2; 36 return (int)(student1->id - student2->id); 37 } 38 39 // 随机抽取学生并写入文件 40 void pickLuckyStudents(Student students[MAX_STUDENTS], int count) { 41 Student luckyStudents[LUCKY_COUNT]; 42 int chosenIndices[MAX_STUDENTS] = {0}; 43 int i; 44 for (i = 0; i < LUCKY_COUNT; i++) { 45 int randomIndex; 46 do { 47 randomIndex = rand() % count; 48 } while (chosenIndices[randomIndex]); 49 50 chosenIndices[randomIndex] = 1; 51 luckyStudents[i] = students[randomIndex]; 52 } 53 54 // 按学号排序 55 qsort(luckyStudents, LUCKY_COUNT, sizeof(Student), compareStudents); 56 57 // 获取当前日期 58 char fileName[20]; 59 time_t now = time(NULL); 60 struct tm *t = localtime(&now); 61 sprintf(fileName, "%04d%02d%02d.txt", t->tm_year + 1900, t->tm_mon + 1, t->tm_mday); 62 63 FILE *file = fopen(fileName, "w"); 64 if (!file) { 65 printf("无法创建文件 %s\n", fileName); 66 exit(1); 67 } 68 69 printf("幸运学生名单:\n"); 70 for ( i = 0; i < LUCKY_COUNT; i++) { 71 printf("%ld %s %s\n", luckyStudents[i].id, luckyStudents[i].name, luckyStudents[i].class); 72 fprintf(file, "%ld %s %s\n", luckyStudents[i].id, luckyStudents[i].name, luckyStudents[i].class); 73 } 74 75 fclose(file); 76 } 77 78 int main() { 79 Student students[MAX_STUDENTS]; 80 srand((unsigned int)time(NULL)); // 初始化随机数发生器 81 82 int count = readStudents(students); 83 if (count > 0) { 84 pickLuckyStudents(students, count); 85 } else { 86 printf("未读取到任何学生信息。\n"); 87 } 88 89 return 0; 90 }
运行截图