实验7
实验任务4
1 #define _CRT_SECURE_NO_WARNINGS 2 #include <stdio.h> 3 int main() { 4 FILE* fp; 5 fp = fopen("data4.txt", "r"); 6 if (!fp) { 7 printf("fail to open\n"); 8 return; 9 } 10 int line = 1; 11 int count = 0; 12 int a; 13 while ((a = fgetc(fp)) != EOF) { 14 if (a == '\n') 15 line++; 16 else if (a != ' ' && a != '\t') 17 count++; 18 } 19 fclose(fp); 20 printf("data4.txt统计结果:\n"); 21 printf("行数:%d\n", line); 22 printf("字符数:%d\n", count); 23 return 0; 24 }

实验任务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("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 FILE* fout; 74 fout = fopen("list_pass.txt", "w"); 75 if (!fout) { 76 printf("fail to open file\n"); 77 return; 78 } 79 for (int i = 0; i < n; ++i) { 80 fprintf(fout, "%ld %s %.2f %.2f %.2f %s\n", s[i].id, s[i].name, s[i].objective, s[i].subjective, s[i].sum, s[i].result); 81 } 82 fclose(fout); 83 } 84 85 // 对考生信息进行处理:计算每位考生考试总分、结果;统计考试通过的人数 86 int process(STU st[], int n, STU st_pass[]) { 87 int count = 0; 88 for (int i = 0; i < n; ++i) { 89 st[i].sum = st[i].objective + st[i].subjective; 90 if (st[i].sum >= 60) 91 { 92 strcpy(st[i].result, "通过"); 93 st_pass[count++] = st[i]; 94 } 95 else 96 strcpy(st[i].result, "不通过"); 97 98 } 99 return count; 100 }

实验任务6
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 main() { 8 9 char s[N][N]; 10 char win[N][N]; 11 FILE *fp,*fout; 12 int n,j=0; 13 int x; 14 int a[N] = { 0 }; 15 char filename[N]; 16 17 fp = fopen("list.txt", "r"); 18 if (!fp) { 19 perror("list.txt"); 20 return 1; 21 } 22 while ((fgets(s[j], N, fp)) != NULL) 23 j++; 24 n = j; 25 srand(time(NULL)); 26 for (int i = 0; i < 5; ) { 27 int id = rand() % n; 28 if (!a[id]) 29 { 30 strcpy(win[i], s[id]); 31 a[id] = 1; 32 i++; 33 } 34 } 35 for (int i = 0; i < 5; i++) 36 { 37 printf("%s\n", win[i]); 38 } 39 printf("enter filename:"); 40 gets(filename); 41 fout = fopen(filename, "w"); 42 if (!fout) { 43 printf("fail to open\n"); 44 } 45 for (int i = 0; i < 5; ++i) { 46 fprintf(fout, "%s\n", win[i]); 47 } 48 fclose(fp); 49 return 0; 50 }


浙公网安备 33010602011771号