实验7
实验任务四
源代码task4.c
1 #include <stdio.h> 2 #include <ctype.h> 3 #include <stdlib.h> 4 int main() { 5 FILE *fp; 6 char filename[] = "data4.txt"; 7 char ch; 8 int lines = 0; 9 int char_count = 0; 10 int last_was_newline = 1; // 标记上一行是否以换行结束,用于行数统计 11 12 // 打开文件 13 fp = fopen(filename, "r"); 14 if (fp == NULL) { 15 printf("无法打开文件 %s\n", filename); 16 return 1; 17 } 18 19 // 逐字符读取 20 while ((ch = fgetc(fp)) != EOF) { 21 // 统计行数:遇到换行符则行数+1 22 if (ch == '\n') { 23 lines++; 24 last_was_newline = 1; 25 } else { 26 last_was_newline = 0; 27 // 统计非空白字符(空格、回车、制表符不计) 28 if (!isspace(ch)) { 29 char_count++; 30 } 31 } 32 } 33 34 // 如果文件最后一行没有换行符,也要算作一行 35 if (!last_was_newline) { 36 lines++; 37 } 38 39 fclose(fp); 40 41 // 输出结果 42 printf("data4.txt统计结果:\n"); 43 printf("行数: %d\n", lines); 44 printf("字符数(不计空白符): %d\n", char_count); 45 system("pause"); 46 return 0; 47 }
运行结果截图

实验任务五
源代码task5.c
1 #include <stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 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 system("pause"); 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 // 待补足 74 // xxx 75 int i, count = 0; 76 77 for (i = 0; i < n; i++) { 78 // 计算总分:客观题 + 操作题 79 st[i].sum = st[i].objective + st[i].subjective; 80 81 // 判断考试结果:总分>=60为通过,否则为未通过 82 if (st[i].sum >= 60) { 83 strcpy(st[i].result, "通过"); 84 // 将通过的学生信息存入st_pass数组 85 st_pass[count] = st[i]; 86 count++; 87 } else { 88 strcpy(st[i].result, "未通过"); 89 } 90 } 91 92 return count; 93 } 94 95 // 把通过考试的考生完整信息写入文件list_pass.txt 96 // 准考证号,姓名,客观题得分,操作题得分,总分,结果 97 void write(STU st[], int n) { 98 // 待补足 99 // xxx 100 int i; 101 FILE *fout; 102 fout = fopen("list_pass.txt", "w"); 103 if (!fout) { 104 printf("文件打开失败!\n"); 105 return; 106 } 107 // 写入表头 108 fprintf(fout, "准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n"); 109 for (i = 0; i < n; i++) { 110 fprintf(fout, "%ld\t%s\t%.2f\t\t%.2f\t\t%.2f\t%s\n", 111 st[i].id, st[i].name, st[i].objective, 112 st[i].subjective, st[i].sum, st[i].result); 113 } 114 fclose(fout); 115 }
运行结果截图


实验任务六
源代码task6.c
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 #include <time.h> 5 6 #define MAX_STUDENTS 100 // 最大学生数(80人足够) 7 #define MAX_LINE_LEN 256 // 每行最大长度 8 #define WINNER_COUNT 5 // 中奖人数 9 10 int main() { 11 FILE *fp; 12 char students[MAX_STUDENTS][MAX_LINE_LEN]; // 存储所有学生信息 13 int winner_indices[WINNER_COUNT]; // 存储中奖者索引 14 int total = 0; 15 char filename[100]; 16 int i, j, random_index; 17 int duplicate; 18 19 // 1. 打开文件 list.txt 20 fp = fopen("list.txt", "r"); 21 if (fp == NULL) { 22 printf("错误:找不到 list.txt 文件!\n"); 23 24 return 1; 25 } 26 27 // 2. 读取所有学生信息 28 while (fgets(students[total], MAX_LINE_LEN, fp) != NULL) { 29 // 去除末尾的换行符 30 size_t len = strlen(students[total]); 31 if (len > 0 && students[total][len - 1] == '\n') { 32 students[total][len - 1] = '\0'; 33 } 34 total++; 35 if (total >= MAX_STUDENTS) break; 36 } 37 fclose(fp); 38 39 // 检查学生人数是否足够 40 if (total < WINNER_COUNT) { 41 printf("错误:学生总数仅%d人,不足%d人!\n", total, WINNER_COUNT); 42 return 1; 43 } 44 45 // 3. 随机抽取5位不重复的中奖者 46 srand((unsigned int)time(NULL)); // 设置随机数种子 47 48 for (i = 0; i < WINNER_COUNT; i++) { 49 do { 50 random_index = rand() % total; 51 duplicate = 0; 52 // 检查是否已经被抽中 53 for (j = 0; j < i; j++) { 54 if (winner_indices[j] == random_index) { 55 duplicate = 1; 56 break; 57 } 58 } 59 } while (duplicate); 60 winner_indices[i] = random_index; 61 } 62 63 // 4. 输出到屏幕 64 printf("\n--- 中奖名单 ---\n"); 65 for (i = 0; i < WINNER_COUNT; i++) { 66 printf("%s\n", students[winner_indices[i]]); 67 } 68 69 // 5. 获取文件名并写入文件 70 printf("\n保存到文件\n"); 71 printf("输入文件名:"); 72 scanf("%s", filename); 73 74 fp = fopen(filename, "w"); 75 if (fp == NULL) { 76 printf("文件保存失败!\n"); 77 return 1; 78 } 79 80 for (i = 0; i < WINNER_COUNT; i++) { 81 fprintf(fp, "%s\n", students[winner_indices[i]]); 82 } 83 fclose(fp); 84 85 printf("保存成功!\n"); 86 system("pause"); 87 return 0; 88 }

选做
源代码
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 #include <time.h> 5 6 #define MAX_STUDENTS 100 7 #define MAX_LINE_LEN 256 8 #define WINNER_COUNT 5 9 10 typedef struct { 11 char id[20]; 12 char name[50]; 13 char class_info[100]; 14 } Student; 15 16 int main() { 17 FILE *fp; 18 Student students[MAX_STUDENTS]; 19 Student winners[WINNER_COUNT]; 20 int total = 0; 21 char line[MAX_LINE_LEN]; 22 int i, j, random_index; 23 int flag[MAX_STUDENTS] = {0}; 24 time_t now; 25 struct tm *timeinfo; 26 char filename[100]; 27 28 fp = fopen("list.txt", "r"); 29 if (fp == NULL) { 30 printf("错误:找不到 list.txt 文件!\n"); 31 system("pause"); 32 return 1; 33 } 34 35 while (fgets(line, MAX_LINE_LEN, fp) != NULL) { 36 size_t len = strlen(line); 37 if (len > 0 && line[len - 1] == '\n') { 38 line[len - 1] = '\0'; 39 } 40 41 char temp_id[20], temp_name[50], temp_class[100]; 42 if (sscanf(line, "%s %s %[^\n]", temp_id, temp_name, temp_class) == 3) { 43 strcpy(students[total].id, temp_id); 44 strcpy(students[total].name, temp_name); 45 strcpy(students[total].class_info, temp_class); 46 total++; 47 } 48 if (total >= MAX_STUDENTS) break; 49 } 50 fclose(fp); 51 52 if (total < WINNER_COUNT) { 53 printf("错误:学生总数仅%d人,不足%d人!\n", total, WINNER_COUNT); 54 system("pause"); 55 return 1; 56 } 57 58 srand((unsigned int)time(NULL)); 59 for (i = 0; i < WINNER_COUNT; i++) { 60 do { 61 random_index = rand() % total; 62 } while (flag[random_index] == 1); 63 flag[random_index] = 1; 64 winners[i] = students[random_index]; 65 } 66 67 // 按学号升序排序 68 for (i = 0; i < WINNER_COUNT - 1; i++) { 69 for (j = 0; j < WINNER_COUNT - 1 - i; j++) { 70 if (strcmp(winners[j].id, winners[j + 1].id) > 0) { 71 Student temp = winners[j]; 72 winners[j] = winners[j + 1]; 73 winners[j + 1] = temp; 74 } 75 } 76 } 77 78 time(&now); 79 timeinfo = localtime(&now); 80 strftime(filename, sizeof(filename), "%Y%m%d.txt", timeinfo); 81 82 // 输出到屏幕(固定宽度对齐) 83 printf("\n---%s中奖名单---\n", filename); 84 for (i = 0; i < WINNER_COUNT; i++) { 85 printf("%-15s%-15s%s\n", winners[i].id, winners[i].name, winners[i].class_info); 86 } 87 88 // 写入文件(固定宽度对齐) 89 fp = fopen(filename, "w"); 90 if (fp == NULL) { 91 printf("文件保存失败!\n"); 92 system("pause"); 93 return 1; 94 } 95 96 fprintf(fp, "---%s中奖名单---\n", filename); 97 for (i = 0; i < WINNER_COUNT; i++) { 98 fprintf(fp, "%-15s%-15s%s\n", winners[i].id, winners[i].name, winners[i].class_info); 99 } 100 fclose(fp); 101 102 printf("\n文件保存成功!\n"); 103 system("pause"); 104 return 0; 105 }
运行结果截图


浙公网安备 33010602011771号