实验7
实验1
源代码
1 #include <stdio.h> 2 #include <string.h> 3 #define N 80 4 #define M 100 5 // 定义图书结构体 6 typedef struct { 7 char name[N]; // 书名 8 char author[N]; // 作者 9 } Book; 10 void write(); 11 void read(); 12 int main() { 13 printf("测试1:把图书信息写入文本文件\n"); 14 write(); 15 printf("\n测试2:从文本文件读取图书信息,打印输出到屏幕\n"); 16 read(); 17 return 0; 18 } 19 void write() { 20 // 初始化图书数据数组(修正原数据的笔误,匹配预期输出) 21 Book x[] = { 22 {"《雕塑家》", "斯科特·麦克劳德"}, 23 {"《灯塔》", "克里斯多夫·夏布特"}, 24 {"《人的局限性》", "塞缪尔·约翰生"}, 25 {"《永不停步:玛格丽特·阿特伍德传》", "罗斯玛丽·沙利文"}, 26 {"《大地之上》", "罗欣顿·米斯特里"}, 27 {"《上学记》", "何兆武"}, 28 {"《命运》", "蔡崇达"} 29 }; 30 int n, i; 31 FILE *fp; 32 // 计算数组元素个数 33 n = sizeof(x) / sizeof(x[0]); 34 // 以写模式打开文件 35 fp = fopen("data1.txt", "w"); 36 if (fp == NULL) { 37 printf("fail to open file to write\n"); 38 return; 39 } 40 // 格式化写入文件(%-40s左对齐占40位,%-20s左对齐占20位) 41 for (i = 0; i < n; ++i) { 42 fprintf(fp, "%-40s %-20s\n", x[i].name, x[i].author); 43 } 44 fclose(fp); 45 } 46 void read() { 47 Book x[M]; // 定义结构体数组存储读取的图书信息 48 int i = 0, n; 49 FILE *fp; 50 // 以读模式打开文件 51 fp = fopen("data1.txt", "r"); 52 if (fp == NULL) { 53 printf("fail to open file to read\n"); 54 return; 55 } 56 // 从文件读取信息到结构体数组(注意格式与写入一致) 57 // 因书名含空格,改用fgets读取,再处理换行符 58 char temp_name[N], temp_author[N]; 59 while (fscanf(fp, "%[^\n]s %[^\n]s", temp_name, temp_author) != EOF) { 60 strcpy(x[i].name, temp_name); 61 strcpy(x[i].author, temp_author); 62 i++; 63 // 跳过换行符 64 fgetc(fp); 65 } 66 n = i; // 读取的图书数量 67 // 打印输出到屏幕 68 for (i = 0; i < n; ++i) { 69 printf("%d. %-40s %-20s\n", i + 1, x[i].name, x[i].author); 70 } 71 fclose(fp); 72 }
实验结果
实验2
1 #include <stdio.h> 2 #define N 80 3 #define M 100 4 // 定义图书结构体 5 typedef struct { 6 char name[N]; // 书名 7 char author[N]; // 作者 8 } Book; 9 void write(); 10 void read(); 11 int main() { 12 printf("测试1:把图书信息以数据块方式写入二进制文件\n"); 13 write(); 14 printf("测试2:从二进制文件读取图书信息,打印输出到屏幕\n"); 15 read(); 16 return 0; 17 } 18 void write() { 19 // 初始化图书数据(与预期输出完全一致) 20 Book x[] = { 21 {"《雕塑家》", "斯科特.麦克劳德"}, 22 {"《灯塔》", "克里斯多夫.夏布特"}, 23 {"《人的局限性》", "塞缪尔.约翰生"}, 24 {"《永不停步:玛格丽特.阿特伍德传》", "罗斯玛丽.沙利文"}, 25 {"《大地之上》", "罗欣顿·米斯特里"}, 26 {"《上学记》", "何兆武"}, 27 {"《命运》", "蔡崇达"} 28 }; 29 int n; 30 FILE *fp; 31 // 计算图书数据条数 32 n = sizeof(x) / sizeof(x[0]); 33 // 以二进制写模式打开文件 34 fp = fopen("data2.dat", "wb"); 35 if (fp == NULL) { 36 printf("fail to open file to write\n"); 37 return; 38 } 39 // 一次性写入n条数据(按实验说明要求) 40 fwrite(x, sizeof(Book), n, fp); 41 fclose(fp); 42 } 43 void read() { 44 Book x[M]; 45 int i = 0, n; 46 FILE *fp; 47 // 以二进制读模式打开文件 48 fp = fopen("data2.dat", "rb"); 49 if (fp == NULL) { 50 printf("fail to open file to read\n"); 51 return; 52 } 53 // 逐条读取数据(事先未知条数,按实验说明要求) 54 while (fread(&x[i], sizeof(Book), 1, fp) == 1) { 55 i++; 56 } 57 n = i; // 读取到的图书总条数 58 // 按预期格式输出(序号、书名、作者分行/分列显示) 59 for (i = 0; i < n; ++i) { 60 printf("%d. %-40s %s\n", i+1, x[i].name, x[i].author); 61 } 62 fclose(fp); 63 }
源代码
实验结果
实验3
源代码
1 #include <stdio.h> 2 #include <string.h> 3 #define N 100 // 最大读取字符串行数 4 #define M 80 // 每个字符串的最大长度 5 void write(); 6 void read_str(); 7 void read_char(); 8 int main() { 9 printf("测试1:把一组字符信息以字符串方式写入文本文件\n"); 10 write(); 11 printf("\n测试2:从文件以字符串方式读取,输出到屏幕\n"); 12 read_str(); 13 printf("\n测试3:从文件以单个字符方式读取,输出到屏幕\n"); 14 read_char(); 15 return 0; 16 } 17 // 字符串方式写入文件(fputs) 18 void write() { 19 // 注意:原实验里的Working's Blues无反斜杠,若要显示\需写Working\\'s Blues 20 char *ptr[] = { 21 "Working's Blues", 22 "Everything Will Flow", 23 "Streets of London", 24 "Perfect Day", 25 "Philadelphia" 26 }; 27 int i, n; 28 FILE *fp; 29 // 打开文本文件(写模式) 30 fp = fopen("data3.txt", "w"); 31 if (fp == NULL) { 32 printf("fail to open file to write\n"); 33 return; 34 } 35 // 计算字符串数组元素个数 36 n = sizeof(ptr) / sizeof(ptr[0]); 37 // 逐行写入字符串,每行末尾加换行符 38 for (i = 0; i < n; ++i) { 39 fputs(ptr[i], fp); 40 fputs("\n", fp); 41 } 42 fclose(fp); 43 } 44 // 字符串方式读取文件(fgets) 45 void read_str() { 46 char songs[N][M]; // 存储读取的字符串 47 int i, n; 48 FILE *fp; 49 // 打开文本文件(读模式) 50 fp = fopen("data3.txt", "r"); 51 if (fp == NULL) { 52 printf("fail to open file to read\n"); 53 return; 54 } 55 i = 0; 56 // i < N 防止数组越界,fgets返回NULL表示文件读取完毕 57 while (i < N && fgets(songs[i], M, fp) != NULL) { 58 // 去除fgets读取的换行符(避免输出时重复换行) 59 songs[i][strcspn(songs[i], "\n")] = '\0'; 60 i++; 61 } 62 n = i; // 实际读取的字符串行数 63 // 按序号输出读取的内容 64 for (i = 0; i < n; ++i) { 65 printf("%d. %s\n", i + 1, songs[i]); 66 } 67 fclose(fp); 68 } 69 // 单个字符方式读取文件(fgetc) 70 void read_char() { 71 int ch; // 用int接收fgetc返回值(EOF是int类型) 72 FILE *fp; 73 // 打开文本文件(读模式) 74 fp = fopen("data3.txt", "r"); 75 if (fp == NULL) { 76 printf("fail to open file to read\n"); 77 return; 78 } 79 // 逐字符读取,直到文件末尾(EOF) 80 while ((ch = fgetc(fp)) != EOF) { 81 putchar(ch); // 输出单个字符 82 } 83 fclose(fp); 84 }
实验结果
问题回答:1要把\变成\\.2防止数组越界
实验4
源代码
1 #include <stdio.h> 2 #include <ctype.h> // 用于判断空白字符 3 int main() { 4 FILE *fp; 5 int ch; 6 int line_count = 0; // 行数 7 int char_count = 0; // 非空白字符数 8 int is_first_line = 1; // 标记是否是第一行(处理最后一行无换行的情况) 9 // 打开文件 10 fp = fopen("data4.txt", "r"); 11 if (fp == NULL) { 12 printf("文件打开失败!\n"); 13 return 1; 14 } 15 // 逐字符读取文件 16 while ((ch = fgetc(fp)) != EOF) { 17 // 统计行数:遇到换行符则行数+1 18 if (ch == '\n') { 19 line_count++; 20 is_first_line = 1; 21 } else { 22 // 非换行符时,标记当前行已开始 23 if (is_first_line) { 24 is_first_line = 0; 25 } 26 // 统计非空白字符(排除空格、制表符、换行符) 27 if (!isspace(ch)) { 28 char_count++; 29 } 30 } 31 } 32 // 处理最后一行无换行符的情况 33 if (!is_first_line) { 34 line_count++; 35 } 36 // 输出结果 37 printf("行数:%d\n", line_count); 38 printf("字符数(不计空白符):%d\n", char_count); 39 // 关闭文件 40 fclose(fp); 41 return 0; 42 }
实验结果
实验5
源代码
1 #include <stdio.h> 2 #include <string.h> 3 #define N 10 // 考生最大人数 4 // 定义考生结构体 5 typedef struct { 6 long id; // 准考证号 7 char name[20]; // 姓名 8 float objective; // 客观题得分 9 float subjective; // 操作题得分 10 float total; // 总得分 11 char result[10]; // 考试结果(通过/未通过) 12 } STU; 13 // 从文件读取考生信息到结构体数组 14 void read(STU st[], int n) { 15 int i; 16 FILE *fin; 17 fin = fopen("examinee.txt", "r"); 18 if (!fin) { 19 printf("fail to open file\n"); 20 return; 21 } 22 for (i = 0; i < n; i++) { 23 fscanf(fin, "%ld %s %f %f", &st[i].id, st[i].name, &st[i].objective, &st[i].subjective); 24 } 25 fclose(fin); 26 } 27 // 处理考生成绩:计算总分、判定结果,筛选通过考生并返回通过人数 28 int process(STU st[], int n, STU st_pass[]) { 29 int i, pass_cnt = 0; 30 for (i = 0; i < n; i++) { 31 // 计算总分 32 st[i].total = st[i].objective + st[i].subjective; 33 // 判断考试结果 34 if (st[i].total >= 60) { 35 strcpy(st[i].result, "通过"); 36 st_pass[pass_cnt++] = st[i]; // 存入通过考生数组 37 } else { 38 strcpy(st[i].result, "不通过"); 39 } 40 } 41 return pass_cnt; // 返回通过人数 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", 50 st[i].id, st[i].name, st[i].objective, st[i].subjective, st[i].total, st[i].result); 51 } 52 } 53 // 将通过考生信息写入list_pass.txt 54 void write(STU s[], int n) { 55 int i; 56 FILE *fout = fopen("list_pass.txt", "w"); 57 if (!fout) { 58 printf("fail to open list_pass.txt\n"); 59 return; 60 } 61 // 写入文件表头 62 fprintf(fout, "准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n"); 63 for (i = 0; i < n; i++) { 64 fprintf(fout, "%ld\t\t%s\t%.2f\t\t%.2f\t\t%.2f\t\t%s\n", 65 s[i].id, s[i].name, s[i].objective, s[i].subjective, s[i].total, s[i].result); 66 } 67 fclose(fout); 68 } 69 int main() { 70 STU st[N], st_pass[N]; 71 int cnt; // 通过考试的人数 72 double pass_rate; // 通过率 73 printf("从文件读入10个考生信息...\n\n"); 74 read(st, N); 75 printf("对考生成绩进行统计...\n\n"); 76 cnt = process(st, N, st_pass); 77 printf("通过考试的名单:\n"); 78 output(st, N); // 输出所有考生信息 79 write(st_pass, cnt); // 将通过考生写入文件 80 // 计算通过率 81 pass_rate = (1.0 * cnt / N) * 100; 82 printf("\n本次等级考试通过率:%.2f%%\n", pass_rate); 83 return 0; 84 }
实验结果
实验6
源代码
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 #include <time.h> 5 #define STUDENT_NUM 80 // 总学生数 6 #define SELECT_NUM 5 // 抽取人数 7 // 定义学生结构体 8 typedef struct { 9 char id[20]; // 学号 10 char name[20]; // 姓名 11 char cls[30]; // 班级 12 } Student; 13 // 冒泡排序:按学号升序排列抽取的学生 14 void sortByID(Student s[], int n) { 15 int i, j; 16 Student temp; 17 for (i = 0; i < n - 1; i++) { 18 for (j = 0; j < n - 1 - i; j++) { 19 // 字符串比较学号,交换顺序 20 if (strcmp(s[j].id, s[j+1].id) > 0) { 21 temp = s[j]; 22 s[j] = s[j+1]; 23 s[j+1] = temp; 24 } 25 } 26 } 27 } 28 int main() { 29 Student all_stu[STUDENT_NUM]; // 存储所有学生信息 30 Student select_stu[SELECT_NUM];// 存储抽取的学生信息 31 int flag[STUDENT_NUM] = {0}; // 标记是否被抽取,0=未抽,1=已抽 32 int i, rand_idx, cnt = 0; 33 FILE *fp_in, *fp_out; 34 char filename[50]; // 存储文件名 35 time_t t = time(NULL); 36 struct tm *tm = localtime(&t); // 获取系统本地时间 37 // 1. 读取list.txt中的学生信息 38 fp_in = fopen("list.txt", "r"); 39 if (fp_in == NULL) { 40 printf("无法打开list.txt文件!\n"); 41 return 1; 42 } 43 for (i = 0; i < STUDENT_NUM; i++) { 44 fscanf(fp_in, "%s %s %[^\n]", all_stu[i].id, all_stu[i].name, all_stu[i].cls); 45 } 46 fclose(fp_in); 47 // 2. 初始化随机数种子 48 srand((unsigned int)time(NULL)); 49 // 3. 随机抽取5位不重复的学生 50 while (cnt < SELECT_NUM) { 51 rand_idx = rand() % STUDENT_NUM; // 生成0~79的随机索引 52 if (flag[rand_idx] == 0) { // 未被抽取则选中 53 select_stu[cnt] = all_stu[rand_idx]; 54 flag[rand_idx] = 1; // 标记为已抽取 55 cnt++; 56 } 57 } 58 // 4. 选做:按学号排序抽取的学生 59 sortByID(select_stu, SELECT_NUM); 60 // 5. 选做:用系统日期生成文件名(格式:YYYYMMDD.txt) 61 sprintf(filename, "%d%02d%02d.txt", tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday); 62 // 6. 屏幕输出中奖名单 63 printf("---------%s中奖名单---------\n", filename); 64 for (i = 0; i < SELECT_NUM; i++) { 65 printf("%s\t%s\t%s\n", select_stu[i].id, select_stu[i].name, select_stu[i].cls); 66 } 67 // 7. 写入指定文件 68 fp_out = fopen(filename, "w"); 69 if (fp_out == NULL) { 70 printf("文件创建失败!\n"); 71 return 1; 72 } 73 for (i = 0; i < SELECT_NUM; i++) { 74 fprintf(fp_out, "%s\t%s\t%s\n", select_stu[i].id, select_stu[i].name, select_stu[i].cls); 75 } 76 fclose(fp_out); 77 printf("文件保存成功!\n"); 78 return 0; 79 }

浙公网安备 33010602011771号