实验7
task1
源代码
1 #define _CRT_SECURE_NO_WARNINGS 2 #include<stdio.h> 3 #include<stdlib.h> 4 #define N 80 5 #define M 100 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 Book x[] = { {"《雕塑家》", "斯科特.麦克劳德"}, 21 {"《灯塔》", "克里斯多夫.夏布特"}, 22 {"《人的局限性》", "塞缪尔.约翰生"}, 23 {"《永不停步:玛格丽特.阿特伍德传》", "罗斯玛丽.沙利文"}, 24 {"《大地之上》", "罗欣顿·米斯特里"}, 25 {"《上学记》", "何兆武"}, 26 {"《命运》", "蔡崇达"} }; 27 int n, i; 28 FILE* fp; 29 n = sizeof(x) / sizeof(x[0]); 30 fp = fopen("date1.txt", "w"); 31 if (fp == NULL) { 32 printf("fail to open file to write \n"); 33 return; 34 } 35 for (i = 0; i < n; ++i) 36 fprintf(fp, "%-40s %-20s\n", x[i].name, x[i].author); 37 fclose(fp); 38 39 } 40 void read() { 41 Book x[N]; 42 int i, n; 43 FILE* fp; 44 fp = fopen("date1.txt", "r"); 45 if (fp == NULL) { 46 printf("fail to oprn file to read\n"); 47 return; 48 } 49 i = 0; 50 while (fscanf(fp, "%s%s", x[i].name, x[i].author) != EOF) 51 ++i; 52 n = i; 53 for (i = 0; i < n; i++) 54 printf("%d.%-40s%-20s\n", i + 1, x[i].name, x[i].author); 55 fclose(fp); 56 57 }
运行结果


task2
1 #define _CRT_SECURE_NO_WARNINGS 2 #include<stdio.h> 3 #define N 80 4 #define M 100 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("\n测试2:从二进制文件读取图书信息,打印输出到屏幕\n"); 15 read(); 16 return 0; 17 } 18 void write() { 19 Book x[] = { {"《雕塑家》", "斯科特.麦克劳德"}, 20 {"《灯塔》", "克里斯多夫.夏布特"}, 21 {"《人的局限性》", "塞缪尔.约翰生"}, 22 {"《永不停步:玛格丽特.阿特伍德传》", "罗斯玛丽.沙利文"}, 23 {"《大地之上》", "罗欣顿·米斯特里"}, 24 {"《上学记》", "何兆武"}, 25 {"《命运》", "蔡崇达"} }; 26 int n, i; 27 FILE* fp; 28 n = sizeof(x) / sizeof(x[0]); 29 fp = fopen("date2.dat", "wb"); 30 if (fp == NULL) { 31 printf("fail to open file to read\n "); 32 return; 33 } 34 fwrite(x, sizeof(Book), n, fp); 35 fclose(fp); 36 } 37 38 void read() { 39 Book x[M]; 40 int i, n; 41 FILE* fp; 42 fp = fopen("date2.dat", "rb"); 43 if (fp == NULL) { 44 printf("fail to open file to read\n"); 45 return; 46 } 47 i = 0; 48 while (fread(&x[i], sizeof(Book), 1, fp) == 1) 49 ++i; 50 n = i; 51 for (i = 0; i < n; ++i) 52 printf("%d。%-40s%-20s\n", i + 1, x[i].name, x[i].author); 53 fclose(fp); 54 55 56 }
运行结果


用记事本打开不可看
task3
原代码
1 #define _CRT_SECURE_NO_WARNINGS 2 #include<stdio.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 void write() { 18 char* ptr[] = { "Working\'s Blues", 19 "Everything Will Flow", 20 "Streets of London", 21 "Perfect Day", 22 "Philadelphia" }; 23 int i, n; 24 FILE* fp; 25 fp = fopen("date3.txt", "w"); 26 if (fp == NULL) { 27 printf("fail to to write \n"); 28 return; 29 30 } 31 n = sizeof(ptr) / sizeof(ptr[0]); 32 for (i = 0; i < n; i++) { 33 fputs(ptr[i], fp); 34 fputs("\n", fp); 35 } 36 fclose(fp); 37 } 38 void read_str() { 39 char song[N][M]; 40 int i, n; 41 FILE* fp; 42 fp = fopen("date3.txt", "r"); 43 if (fp == NULL) { 44 printf("fail to open file to read"); 45 return; 46 } 47 i = 0; 48 while (i < N && (fgets(song[i], M, fp) != NULL)) 49 ++i; 50 n = i; 51 for (i = 0; i < n; i++) 52 printf("%d.%s", i + 1, song[i]); 53 fclose(fp); 54 } 55 void read_char() { 56 int ch; 57 FILE* fp; 58 fp = fopen("date3.txt", "r"); 59 if (fp == NULL) { 60 printf("fail to open file to read \n"); 61 return; 62 } 63 while ((ch = fgetc(fp)) != EOF) 64 putchar(ch); 65 fclose(fp); 66 67 }
运行结果


在c语言中有部分字符是需要转义的\'是一个整体,用来在“”中输出‘。
song数组中之定义了N个,所以为了防止超过出数组的范围就设置了这个要求
task4
源代码
1 #define _CRT_SECURE_NO_WARNINGS 2 #include<stdio.h> 3 #include<string.h> 4 //void write(); 5 int main() { 6 printf("date4.txt统计结果:\n"); 7 char* ptr[] = { "0123456789-0123456789", 8 "nuist2025", 9 "cosmos galaxy" }; 10 int i, n; 11 FILE* fp; 12 fp = fopen("date7.txt", "w"); 13 if (fp == NULL) { 14 printf("fail to open file to write\n"); 15 return; 16 } 17 int sum = 0; 18 int count = 0; 19 n = sizeof(ptr) / sizeof(ptr[0]); 20 for (i = 0; i < n; i++) { 21 fputs(ptr[i], fp); 22 char* ch = ptr[i]; 23 while (*ch != '\0') { 24 if (*ch == ' ') { 25 count++; 26 } 27 ch++; 28 } 29 sum += strlen(ptr[i]); 30 fputs("\n", fp); 31 } 32 sum = sum - count; 33 fclose(fp); 34 printf("行数:\t %d\n",n); 35 printf("字符数(不计空白符):\t %d", sum); 36 return 0; 37 }
运行结果

task5
源代码
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 write(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].result); 51 } 52 53 // 从文本文件examinee.txt读入考生信息:准考证号,姓名,客观题得分,操作题得分 54 void read(STU st[], int n) { 55 int i; 56 FILE* fin; 57 58 fin = fopen("examinee.txt", "r"); 59 if (!fin) { 60 printf("fail to open file\n"); 61 return; 62 } 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 67 fclose(fin); 68 } 69 70 // 把通过考试的考生完整信息写入文件list_pass.txt 71 // 准考证号,姓名,客观题得分,操作题得分,总分,结果 72 void write(STU s[], int n) { 73 int i; 74 FILE* ptr; 75 ptr = fopen("examinee.txt", "r"); 76 if (ptr == NULL) { 77 printf("fail to open file to write\n"); 78 return; 79 } 80 fprintf(ptr, "准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n"); 81 for (i = 0; i < n; i++) { 82 fprintf(ptr, "%ld %s %f %f", &s[i].id, s[i].name, &s[i].objective, &s[i].subjective); 83 } 84 fclose(ptr); 85 } 86 87 // 对考生信息进行处理:计算每位考生考试总分、结果;统计考试通过的人数 88 int process(STU st[], int n, STU st_pass[]) { 89 int i, pass_count=0; 90 for (i = 0; i < n; i++) { 91 st[i].sum = st[i].subjective + st[i].objective; 92 if (st[i].sum >= 60) { 93 strcpy(st[i].result, "合格"); 94 st_pass[pass_count++] = st[i]; 95 96 } 97 else 98 strcpy(st[i].result, "不合格"); 99 } 100 return pass_count; 101 102 103 }
运行结果


task6
源代码
1 #define _CRT_SECURE_NO_WARNINGS 2 #include <stdio.h> 3 #include <stdlib.h> 4 #include <time.h> 5 #include<string.h> 6 #define N 100 7 int isflag(int* flag, int i, int num) { 8 for (int j = 0; j < i; j++) { 9 if (flag[j] == num) { 10 return 0; 11 } 12 } 13 return 1; 14 } 15 16 int main() { 17 FILE* fp; 18 srand(time(NULL)); 19 int n = 5, count = 0, flag[5] = { 0 }; 20 char s[N][N]; 21 char t[5][N]; 22 fp = fopen("C:\\Users\\徐彬\\Desktop\\实验7数据文件及部分代码_gbk\\list.txt", "r"); 23 if (fp == NULL) { 24 printf("File could not be opened\n"); 25 exit(1); 26 } 27 for (int i = 0; fgets(s[i], N, fp) != NULL; i++) { 28 count++; 29 30 } 31 fclose(fp); 32 for (int i = 0; i < 5;) { 33 int randNum = rand() % count; 34 flag[i] = randNum; 35 if (isflag(flag, i, randNum) == 0) { continue; } 36 if (isflag(flag, i, randNum) == 1) { 37 38 strcpy(t[i], s[randNum]); 39 i++; 40 } 41 42 } 43 char str[N]; 44 char str2[N] = "C:\\Users\\徐彬\\Desktop\\实验7数据文件及部分代码_gbk\\"; 45 scanf("%s", str); 46 strcat(str2, str); 47 fp = fopen(str2, "w"); 48 if (fp == NULL) { 49 printf("File could not be opened\n"); 50 } 51 for (int i = 0; i < 5; i++) { 52 fprintf(fp, "%s\n", t[i]); 53 } 54 fclose(fp); 55 56 }
运行结果


浙公网安备 33010602011771号