实验7
任务4
1.源代码
1 #define _CRT_SECURE_NO_WARNINGS 2 #include <stdio.h> 3 4 int main() 5 { 6 FILE* fp; 7 int ch; 8 int lines = 0; 9 int count = 0; 10 int lastChar = 0; 11 12 fp = fopen("D:\\...我的文件\\作业\\C语言作业\\实验7\\实验7数据文件及部分代码_gbk\\data4.txt", "r"); 13 if (fp == NULL) 14 { 15 printf("无法打开文件 data4.txt\n"); 16 return 1; 17 } 18 19 while ((ch = fgetc(fp)) != EOF) 20 { 21 if (ch == '\n') 22 lines++; 23 24 if (ch != ' ' && ch != '\t' && ch != '\n' && ch != '\r') 25 count++; 26 27 lastChar = ch; 28 } 29 30 if (lastChar != 0 && lastChar != '\n') 31 lines++; 32 33 fclose(fp); 34 35 printf("data4.txt统计结果:\n"); 36 printf("行数:%d\n", lines); 37 printf("字符数(不计空白符):%d\n", count); 38 39 return 0; 40 }
2.运行结果截图

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


任务6
1.源代码
1 #define _CRT_SECURE_NO_WARNINGS 2 #include <stdio.h> 3 #include <stdlib.h> 4 #include <time.h> 5 6 #define MAX_STUDENTS 80 7 #define WINNERS 5 8 9 typedef struct { 10 long id; // 学号 11 char name[20]; // 姓名 12 char class_name[30];// 班级 13 } Student; 14 15 int main() { 16 Student students[MAX_STUDENTS]; 17 int count = 0; // 实际读取的学生人数 18 int winners[WINNERS]; // 存储中奖者索引(不重复) 19 int i, j, flag; 20 char filename[100]; 21 FILE* fp_in, * fp_out; 22 23 // 1. 从list.txt读取所有学生信息(绝对路径) 24 fp_in = fopen("D:\\...我的文件\\作业\\C语言作业\\实验7\\实验7数据文件及部分代码_gbk\\list.txt", "r"); 25 if (fp_in == NULL) { 26 printf("无法打开文件 list.txt\n"); 27 return 1; 28 } 29 30 // 读取数据(假设格式:学号 姓名 班级) 31 while (fscanf(fp_in, "%ld %s %s", &students[count].id, students[count].name, students[count].class_name) == 3) { 32 count++; 33 if (count >= MAX_STUDENTS) break; 34 } 35 fclose(fp_in); 36 37 if (count == 0) { 38 printf("文件中没有学生信息\n"); 39 return 1; 40 } 41 42 printf("从文件读取了 %d 位学生信息\n", count); 43 44 srand((unsigned)time(NULL)); 45 46 for (i = 0; i < WINNERS; i++) { 47 do { 48 winners[i] = rand() % count; 49 flag = 0; 50 for (j = 0; j < i; j++) { 51 if (winners[j] == winners[i]) { 52 flag = 1; 53 break; 54 } 55 } 56 } while (flag); 57 } 58 59 printf("请输入保存中奖名单的文件名:"); 60 scanf("%s", filename); 61 62 fp_out = fopen(filename, "w"); 63 if (fp_out == NULL) { 64 printf("无法创建文件 %s\n", filename); 65 return 1; 66 } 67 68 printf("\n------- 中奖名单 -------\n"); 69 fprintf(fp_out, "------- 中奖名单 -------\n"); 70 for (i = 0; i < WINNERS; i++) { 71 int idx = winners[i]; 72 printf("%ld\t%s\t%s\n", students[idx].id, students[idx].name, students[idx].class_name); 73 fprintf(fp_out, "%ld\t%s\t%s\n", students[idx].id, students[idx].name, students[idx].class_name); 74 } 75 76 fclose(fp_out); 77 printf("\n中奖名单已写入文件 %s\n", filename); 78 79 return 0; 80 }
2.运行结果截图


浙公网安备 33010602011771号