实验7
task4
1 #include <stdio.h> 2 #include <string.h> 3 #define N 5 4 #define M 80 5 int main() { 6 char songs[N][M]; 7 8 FILE* fp; 9 fp = fopen("data1.txt", "r"); 10 if (fp == NULL) { 11 printf("fail to open file\n"); 12 return 1; 13 } 14 int t = 0; 15 char h; 16 while (1) { 17 h = fgetc(fp); 18 if (h == EOF) { 19 break; 20 } 21 else if (h == ' ' || h == '\n') { 22 continue; 23 } 24 else { 25 t++; 26 } 27 } 28 29 printf("文件内字符数为%d\n", t); 30 31 fclose(fp); 32 return 0; 33 }
task5
1 #include <stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 4 5 #define N 10 6 typedef struct { 7 long int id; 8 char name[20]; 9 float objective; // 客观题得分 10 float subjective; // 操作题得分 11 float sum; // 总分 12 char ans[10]; // 考试结果 13 } STU; 14 // 函数声明 15 void finput(STU st[], int n); 16 void foutput(STU st[], int n); 17 void output(STU st[], int n); 18 int process(STU st[], int n, STU st_pass[]); 19 int main() { 20 STU stu[N], stu_pass[N]; 21 int cnt; 22 double pass_rate; 23 printf("从文件读入%d个考生信息...\n", N); 24 finput(stu, N); 25 printf("\n对考生成绩进行统计...\n"); 26 cnt = process(stu, N, stu_pass); 27 printf("\n通过考试的名单:\n"); 28 output(stu, N); // 输出到屏幕 29 foutput(stu, N); // 输出到文件 30 pass_rate = 1.0 * cnt / N; 31 printf("\n本次等级考试通过率: %.2f%%\n", pass_rate * 100); 32 return 0; 33 } 34 // 把通过考试的考生完整信息输出到屏幕上 35 // 准考证号,姓名,客观题得分,操作题得分,总分,结果 36 void output(STU st[], int n) { 37 int i; 38 printf("准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n"); 39 for (i = 0; i < n; i++) 40 printf("%ld\t\t%s\t%.2f\t\t%.2f\t\t%.2f\t\t%s\n", st[i].id, 41 st[i].name, st[i].objective, st[i].subjective, st[i].sum, st[i].ans); 42 } 43 // 从文本文件examinee.txt读入考生信息:准考证号,姓名,客观题得分,操作题得分 44 void finput(STU st[], int n) { 45 int i; 46 FILE* fin; 47 fin = fopen("examinee.txt", "r"); 48 if (fin == NULL) { 49 printf("fail to open file\n"); 50 exit(0); 51 } 52 while (!feof(fin)) { 53 for (i = 0; i < n; i++) 54 fscanf(fin, "%ld %s %f %f", &st[i].id, st[i].name, 55 &st[i].objective, &st[i].subjective); 56 } 57 fclose(fin); 58 } 59 // 把通过考试的考生完整信息写入文件list_pass.txt 60 // 准考证号,姓名,客观题得分,操作题得分,总分,结果 61 void foutput(STU s[], int n) { 62 FILE* fout; 63 int i; 64 // 保存到文件 65 fout = fopen("list_pass.txt", "w"); 66 if (!fout) { 67 printf("fail to open or create list_pass.txt\n"); 68 exit(0); 69 } 70 fprintf(fout, "准考证号\t\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n"); 71 for (i = 0; i < n; i++) 72 fprintf(fout, "%ld\t\t%s\t%.2f\t\t%.2f\t\t%.2f\t\t%s\n", s[i].id, 73 s[i].name, s[i].objective, s[i].subjective, s[i].sum, s[i].ans); 74 fclose(fout); 75 } 76 // 对考生信息进行处理:计算每位考生考试总分、结果;统计考试通过的人数 77 int process(STU st[], int n, STU st_pass[]) { 78 FILE* fscore; 79 int cnt = 0; 80 fscore = fopen("list_pass.txt", "r"); 81 for (int i = 0; i < n; i++) { 82 st[i].sum = st[i].objective + st[i].subjective; 83 } 84 85 for (int i = 0; i < n; i++) { 86 if (st[i].sum >= 60) { 87 strcpy(st[i].ans, "pass"); 88 cnt++; 89 } 90 else { 91 strcpy(st[i].ans, "fail"); 92 } 93 } 94 95 return cnt; 96 }
task6
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 #define N 80 5 6 typedef struct { 7 int id; 8 char name[20]; 9 char class[100]; 10 }info; 11 12 13 void finput(info stu[], int n) { 14 15 FILE* fp; 16 fp = fopen("list.txt", "r"); 17 while (!feof(fp)) { 18 for (int i = 0; i < N; i++) { 19 fscanf(fp, "%d%s%s", &stu[i].id, stu[i].name, stu[i].class); 20 } 21 } 22 fclose(fp); 23 } 24 25 void output(info stu[], int n) { 26 for (int i = 0; i < n; i++) 27 printf("%d\t%s\t%s\n", stu[i].id, stu[i].name, stu[i].class); 28 } 29 30 void translate(info stu[], info sts[], int n) { 31 srand((unsigned)time(NULL)); 32 int judge[80] = { 0 }; 33 for (int i = 0; i < 5; i++) { 34 int j = rand() % 80 + 1; 35 if (judge[j] == 1) { 36 i--; 37 continue; 38 } 39 sts[i].id = stu[j].id; 40 strcpy(sts[i].name, stu[j].name); 41 strcpy(sts[i].class, stu[j].class); 42 judge[j] = 1; 43 } 44 } 45 46 void sort(info sts[], int n) { 47 for (int i = 0; i < 5; i++) { 48 for (int j = i; j < 5; j++) { 49 if (sts[i].id > sts[j].id) { 50 int l = sts[i].id; 51 sts[i].id = sts[j].id; 52 sts[j].id = l; 53 char p[100]; 54 strcpy(p, sts[i].name); 55 strcpy(sts[i].name, sts[j].name); 56 strcpy(sts[j].name, p); 57 } 58 } 59 } 60 } 61 62 int main() 63 { 64 info stu[N], sts[5]; 65 finput(stu, N); 66 output(stu, N); 67 printf("\n"); 68 translate(stu, sts, N); 69 sort(sts, 5); 70 output(sts, 5); 71 72 return 0; 73 }