实验7
任务1
截图

任务2
截图
任务3
截图

1. "Working's Blues"输出无反斜杠原因
代码中字符串是 \"working's Blues\" ,单引号 ' 是普通字符,不需要转义;
\' 才是转义单引号写法,源码里没有写反斜杠,因此文件、控制台都不会显示 \ 。
2. while(i < N && fgets(...) != NULL) 中 i < N 的作用
任务4
源代码
1 #include <stdio.h> 2 #include <ctype.h> // 用于isspace函数 3 4 int main() { 5 FILE *fp; 6 int ch; 7 int line_count = 0; // 行数 8 int char_count = 0; // 非空白字符数 9 10 // 打开文件 11 fp = fopen("data4.txt", "r"); 12 if (fp == NULL) { 13 printf("无法打开文件!\n"); 14 return 1; 15 } 16 17 // 逐个读取字符 18 while ((ch = fgetc(fp)) != EOF) { 19 if (ch == '\n') { 20 line_count++; // 遇到换行符,行数+1 21 } 22 // 统计非空白字符(空格、回车、tab都不算) 23 if (!isspace(ch)) { 24 char_count++; 25 } 26 } 27 28 // 处理最后一行(如果文件最后一行没有换行符) 29 if (char_count > 0 && ch != '\n') { 30 line_count++; 31 } 32 33 printf("data4.txt统计结果:\n"); 34 printf("行数: %d\n", line_count); 35 printf("字符数(不计空白符): %d\n", char_count); 36 37 fclose(fp); 38 return 0; 39 }
截图

任务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 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 41 return 0; 42 } 43 44 // 把所有考生完整信息输出到屏幕上 45 // 准考证号,姓名,客观题得分\操作题得分\总分\结果 46 void output(STU st[], int n) { 47 int i; 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", 51 st[i].id, st[i].name, 52 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 fin = fopen("examinee.txt", "r"); 60 if (!fin) { 61 printf("fail to open file\n"); 62 return; 63 } 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 fclose(fin); 67 } 68 69 // 对考生信息进行处理:计算每位考生考试总分、结果;统计并返回通过考试的人数 70 int process(STU st[], int n, STU st_pass[]) { 71 int i, pass_cnt = 0; 72 for(i = 0; i < n; i++){ 73 // 计算总分 74 st[i].sum = st[i].objective + st[i].subjective; 75 // 判断是否通过 76 if(st[i].sum >= 60){ 77 strcpy(st[i].result, "通过"); 78 // 存入通过名单数组 79 st_pass[pass_cnt] = st[i]; 80 pass_cnt++; 81 }else{ 82 strcpy(st[i].result, "不通过"); 83 } 84 } 85 return pass_cnt; 86 } 87 88 // 把通过考试的考生完整信息写入list_pass.txt 89 // 准考证号,姓名,客观题得分,操作题得分,总分,结果 90 void write(STU st[], int n) { 91 int i; 92 FILE *fp; 93 fp = fopen("list_pass.txt", "w"); 94 if(fp == NULL){ 95 printf("fail to open write file\n"); 96 return; 97 } 98 // 写入表头 99 fprintf(fp, "准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n"); 100 for(i = 0; i < n; i++){ 101 fprintf(fp, "%ld\t\t%s\t%.2f\t\t%.2f\t\t%.2f\t\t%s\n", 102 st[i].id, st[i].name, st[i].objective, st[i].subjective, st[i].sum, st[i].result); 103 } 104 fclose(fp); 105 }
截图



任务6
源代码
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <time.h> 4 #include <string.h> 5 6 #define TOTAL 80 // 总人数80 7 #define WIN_NUM 5 // 抽取5名中奖 8 9 // 学生结构体 10 typedef struct { 11 char id[12]; // 学号 12 char name[16]; // 姓名 13 char cls[32]; // 班级 14 } Student; 15 16 // 读取全部学生到数组,返回总人数 17 int readAll(Student stu[]) { 18 FILE *fp = fopen("list.txt", "r"); 19 if(fp == NULL) { 20 printf("无法打开list.txt\n"); 21 exit(1); 22 } 23 int cnt = 0; 24 while(fscanf(fp, "%s %s %s", stu[cnt].id, stu[cnt].name, stu[cnt].cls) != EOF) { 25 cnt++; 26 } 27 fclose(fp); 28 return cnt; 29 } 30 31 // 冒泡排序:按学号升序 32 void sortWin(Student win[], int n) { 33 int i,j; 34 Student tmp; 35 for(i=0; i<n-1; i++){ 36 for(j=0; j<n-1-i; j++){ 37 if(strcmp(win[j].id, win[j+1].id) > 0) { 38 tmp = win[j]; 39 win[j] = win[j+1]; 40 win[j+1] = tmp; 41 } 42 } 43 } 44 } 45 46 // 随机不重复抽取WIN_NUM人 47 void randomPick(Student all[], int total, Student win[]) { 48 int flag[TOTAL] = {0}; // 标记是否已抽取 49 srand((unsigned)time(NULL)); 50 int count = 0; 51 while(count < WIN_NUM) { 52 int idx = rand() % total; 53 if(flag[idx] == 0) { 54 flag[idx] = 1; 55 win[count++] = all[idx]; 56 } 57 } 58 } 59 60 // 获取系统日期字符串:YYYYMMDD 61 void getDateStr(char fname[]) { 62 time_t now = time(NULL); 63 struct tm *t = localtime(&now); 64 strftime(fname, 32, "%Y%m%d.txt", t); 65 } 66 67 // 打印中奖名单到屏幕 68 void printWin(Student win[], int n) { 69 printf("----------------中奖名单----------------\n"); 70 for(int i=0; i<n; i++) { 71 printf("%s\t%s\t%s\n", win[i].id, win[i].name, win[i].cls); 72 } 73 printf("----------------------------------------\n"); 74 } 75 76 // 写入文件 77 void saveToFile(Student win[], int n, char filename[]) { 78 FILE *fp = fopen(filename, "w"); 79 if(fp == NULL) { 80 printf("文件创建失败\n"); 81 return; 82 } 83 for(int i=0; i<n; i++) { 84 fprintf(fp, "%s\t%s\t%s\n", win[i].id, win[i].name, win[i].cls); 85 } 86 fclose(fp); 87 printf("保存到文件:%s\n保存成功!\n", filename); 88 } 89 90 int main() { 91 Student allStu[TOTAL]; 92 Student winStu[WIN_NUM]; 93 char fileName[32]; 94 95 // 1.读取全体学生 96 int total = readAll(allStu); 97 // 2.随机抽取5人 98 randomPick(allStu, total, winStu); 99 // 3.中奖名单按学号升序排序 100 sortWin(winStu, WIN_NUM); 101 // 4.控制台输出 102 printWin(winStu, WIN_NUM); 103 // 5.获取系统日期作为文件名 104 getDateStr(fileName); 105 // 6.写入文件 106 saveToFile(winStu, WIN_NUM, fileName); 107 108 return 0; 109 }
截图




浙公网安备 33010602011771号