实验七
3.
1.\与 ’ 组成转义字符输出为 ‘
2. i < N 确保循环不会超过song[ i ]的最大容量
4.
1 #define _CRT_SECURE_NO_WARNINGS 2 #include <stdio.h> 3 #include <ctype.h> 4 5 int main() { 6 FILE* fp = fopen("C:\\Users\\atan\\Desktop\\实验7数据文件及部分代码_gbk\\data4.txt", "r"); 7 if (fp == NULL) { 8 printf("无法打开文件!\n"); 9 return 1; 10 } 11 12 int line_count = 0; 13 int char_count = 0; 14 int in_line = 0; 15 int prev_char = 0; 16 int ch; 17 18 while ((ch = fgetc(fp)) != EOF) { 19 if (ch == '\n') { 20 line_count++; 21 in_line = 0; 22 } 23 else if (!isspace(ch)) { 24 char_count++; 25 in_line = 1; 26 } 27 prev_char = ch; 28 } 29 30 if (prev_char != '\n' && in_line) { 31 line_count++; 32 } 33 34 fclose(fp); 35 36 printf("data4.txt统计结果:\n"); 37 printf("行数:%d\n", line_count); 38 printf("字符数(不计空白符):%d\n", char_count); 39 40 return 0; 41 }

5.
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("C:\\Users\\atan\\Downloads\\实验7数据文件及部分代码_gbk\\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 // 把通过考试的考生完整信息写入文件list_pass.txt 73 // 准考证号,姓名,客观题得分,操作题得分,总分,结果 74 void write(STU s[], int n) { 75 FILE* fp; 76 int i = 0; 77 if ((fp = fopen("C:\\Users\\atan\\Downloads\\实验7数据文件及部分代码_gbk\\examinee.txt", "w")) == NULL) { 78 printf("不能打开或创建文件"); 79 return; 80 } 81 82 83 fprintf(fp, "准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n"); 84 85 for (i = 0; i < n; i++) { 86 87 if (strcmp(s[i].result, "PASS") == 0) { 88 fprintf(fp, "%ld\t\t%s\t%.1f\t\t%.1f\t\t%.1f\t\t%s\n", 89 s[i].id, s[i].name, s[i].objective, 90 s[i].subjective, s[i].sum, s[i].result); 91 } 92 } 93 94 fclose(fp); 95 printf("已成功将通过考试的考生信息写入list_pass.txt文件\n"); 96 } 97 98 // 对考生信息进行处理:计算每位考生考试总分、结果;统计考试通过的人数 99 int process(STU st[], int n, STU st_pass[]) { 100 // 待补足 101 // xxx 102 int i, pass_cnt = 0; 103 104 for (i = 0; i < n; i++) { 105 // 计算总分 106 st[i].sum = st[i].objective + st[i].subjective; 107 108 // 判断结果 109 if (st[i].sum >= 60) { 110 strcpy(st[i].result, "PASS"); 111 // 复制到通过名单 112 st_pass[pass_cnt] = st[i]; 113 pass_cnt++; 114 } 115 else { 116 strcpy(st[i].result, "FAIL"); 117 } 118 } 119 120 return pass_cnt; // 返回通过人数 121 }

6
1 #define _CRT_SECURE_NO_WARNINGS 2 #include <stdio.h> 3 #include<stdlib.h> 4 #define N 100 5 #define M 5 6 7 int main() { 8 char s[N][N]; 9 char hit[N][N]; 10 int has_hit[N] = { 0 }; 11 FILE* fin,*fout; 12 int i, n,lucky_i; 13 char filename[80]; 14 fin = fopen("C:\\Users\\atan\\Desktop\\实验7数据文件及部分代码_gbk\\list.txt","r"); 15 if (!fin) { 16 perror("list.txt"); 17 return 1; 18 } 19 20 i = 0; 21 while ((fgets(s[i], N, fin)) != NULL) { 22 ++i; 23 24 } 25 n = i; 26 srand(time(0)); 27 for (i = 0; i < M;) { 28 lucky_i = rand() % n; 29 if (has_hit[lucky_i]) 30 continue; 31 has_hit[lucky_i] = 1; 32 strcpy(hit[i], s[lucky_i]); 33 printf("%s", s[lucky_i]); 34 ++i; 35 } 36 for (i = 0; i < M; ++i) 37 printf("%S", s[i]); 38 printf("Enter filename:"); 39 gets(filename); 40 fout = fopen(filename, "w");; 41 if (!fout) { 42 perror(filename); 43 return 1; 44 } 45 for (i = 0; i < M; ++i) 46 fprintf(fout, "%s", hit[i]); 47 fclose(fin); 48 return 0; 49 }


浙公网安备 33010602011771号