实验7
task4
1 #define _CRT_SECURE_NO_WARNINGS 2 #include <stdio.h> 3 4 int main() { 5 FILE* fp; 6 int ch; 7 int lines = 0; 8 int chars = 0; 9 10 fp = fopen("data4.txt", "r"); 11 if (!fp ) { 12 printf("无法打开 data4.txt\n"); 13 return 1; 14 } 15 16 while ((ch = fgetc(fp)) != EOF) { 17 if (ch == '\n') { 18 lines++; 19 } 20 if (ch != ' ' && ch != '\n' && ch != '\t') { 21 chars++; 22 } 23 } 24 25 fclose(fp); 26 27 28 if (chars > 0) { 29 lines++; 30 } 31 32 printf("data4.txt统计结果:\n"); 33 printf("行数: %d\n", lines); 34 printf("字符数(不计空白符): %d\n", chars); 35 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* fout; 75 fout = fopen("list_pass.txt", "w"); 76 if (!fout) { 77 printf("fail to open file\n"); 78 return; 79 } 80 fprintf(fout, "准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n"); 81 for (i = 0; i < n; i++) { 82 if (s[i].sum >= 60) { 83 fprintf(fout, "%ld\t\t%s\t%.2f\t\t%.2f\t\t%.2f\t\t%s\n", s[i].id, s[i].name, s[i].objective, s[i].subjective, s[i].sum, s[i].result); 84 } 85 } 86 fclose(fout); 87 88 } 89 90 // 对考生信息进行处理:计算每位考生考试总分、结果;统计考试通过的人数 91 int process(STU st[], int n, STU st_pass[]) { 92 int i, cnt = 0; 93 for (i = 0; i < n; i++) { 94 st[i].sum = st[i].objective + st[i].subjective; 95 if (st[i].sum >= 60) { 96 strcpy(st[i].result, "通过"); 97 st_pass[cnt++] = st[i]; 98 } 99 else { 100 strcpy(st[i].result, "不通过"); 101 } 102 } 103 return cnt; 104 105 }


task6
1 #define _CRT_SECURE_NO_WARNINGS 2 #include <stdio.h> 3 #include <stdlib.h> 4 #include <time.h> 5 6 #define N 80 7 #define WIN 5 8 9 struct Student { 10 char id[20]; 11 char name[20]; 12 char cls[50]; 13 }; 14 15 int main() { 16 struct Student stu[N]; 17 int chosen[WIN]; 18 int used[N] = { 0 }; 19 int i; 20 FILE* fp; 21 char outFile[50]; 22 23 fp = fopen("list.txt", "r"); 24 if (fp == NULL) { 25 printf("无法打开 list.txt 文件!\n"); 26 return 1; 27 } 28 29 for (i = 0; i < N; i++) { 30 fscanf(fp, "%s %s %s", 31 stu[i].id, stu[i].name, stu[i].cls); 32 } 33 fclose(fp); 34 35 srand((unsigned int)time(NULL)); 36 37 i = 0; 38 while (i < WIN) { 39 int r = rand() % N; 40 if (!used[r]) { 41 chosen[i] = r; 42 used[r] = 1; 43 i++; 44 } 45 } 46 47 printf("------------中奖名单------------\n"); 48 for (i = 0; i < WIN; i++) { 49 printf("%s %s %s\n", 50 stu[chosen[i]].id, 51 stu[chosen[i]].name, 52 stu[chosen[i]].cls); 53 } 54 55 printf("\n------------保存到文件------------\n"); 56 printf("输入文件名:"); 57 scanf("%s", outFile); 58 59 fp = fopen(outFile, "w"); 60 if (fp == NULL) { 61 printf("文件创建失败!\n"); 62 return 1; 63 } 64 65 for (i = 0; i < WIN; i++) { 66 fprintf(fp, "%s %s %s\n", 67 stu[chosen[i]].id, 68 stu[chosen[i]].name, 69 stu[chosen[i]].cls); 70 } 71 fclose(fp); 72 73 printf("保存成功!\n"); 74 return 0; 75 }


浙公网安备 33010602011771号