实验7
实验任务4:
源代码:
1 #include <stdio.h> 2 #include <string.h> 3 4 int main() { 5 FILE *fp; 6 char line[100]; 7 int a = 0; 8 int s = 0; 9 int i; 10 11 fp = fopen("data4.txt", "r"); 12 if (fp == NULL) { 13 printf("fail to open file to write\n"); 14 return; 15 } 16 17 while (fgets(line, sizeof(line), fp)!= NULL) { 18 a++; 19 int len = strlen(line); 20 for (i = 0; i < len; i++) { 21 if (line[i]!= '\n' && line[i]!= '\t' && line[i]!= ' ') { 22 s++; 23 } 24 } 25 } 26 27 fclose(fp); 28 29 printf("data4.txt统计结果:\n"); 30 printf("行数:%d\n", a); 31 printf("字符数(不计空白符):%d\n", s); 32 33 return 0; 34 }
试验任务5:
源代码:
1 #include <stdio.h> 2 #include <string.h> 3 4 #define N 10 5 6 typedef struct { 7 long id; 8 char name[20]; 9 float objective; 10 float subjective; 11 float sum; 12 char result[10]; 13 } STU; 14 15 void read(STU st[], int n); 16 void write(STU st[], int n); 17 void output(STU st[], int n); 18 int process(STU st[], int n, STU st_pass[]); 19 20 int main() { 21 STU stu[N], stu_pass[N]; 22 int cnt; 23 double pass_rate; 24 25 printf("从文件读入%d个考生信息...\n", N); 26 read(stu, N); 27 28 printf("\n对考生成绩进行统计...\n"); 29 cnt = process(stu, N, stu_pass); 30 31 printf("\n通过考试的名单:\n"); 32 output(stu, N); 33 write(stu, N); 34 35 pass_rate = 1.0 * cnt / N; 36 printf("\n本次等级考试通过率: %.2f%%\n", pass_rate*100); 37 38 return 0; 39 } 40 41 void output(STU st[], int n) { 42 int i; 43 44 printf("准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n"); 45 for (i = 0; i < n; i++) 46 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); 47 } 48 49 void read(STU st[], int n) { 50 int i; 51 FILE *fin; 52 53 fin = fopen("examinee.txt", "r"); 54 if (!fin) { 55 printf("fail to open file\n"); 56 return; 57 } 58 59 while (!feof(fin)) { 60 for (i = 0; i < n; i++) 61 fscanf(fin, "%ld %s %f %f", &st[i].id, st[i].name, &st[i].objective, &st[i].subjective); 62 } 63 64 fclose(fin); 65 } 66 67 void write(STU s[], int n) { 68 int i; 69 FILE *fp; 70 fp = fopen("list_pass.txt", "w"); 71 if (!fp) { 72 printf("fail to open file\n"); 73 return; 74 } 75 for (i = 0; i < n; i++) { 76 if (strcmp(s[i].result, "通过") == 0) { 77 fprintf(fp, "%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); 78 } 79 } 80 fclose(fp); 81 } 82 83 int process(STU st[], int n, STU st_pass[]) { 84 int count=0,i; 85 for (i = 0; i < n; i++) { 86 st[i].sum = st[i].objective + st[i].subjective; 87 if (st[i].sum> 60) { 88 strcpy(st[i].result, "通过"); 89 st_pass[count] = st[i]; 90 count++; 91 } else { 92 strcpy(st[i].result, "未通过"); 93 } 94 } 95 return count; 96 }
试验任务6:
源代码:
1 #include <stdio.h> 2 #include <time.h> 3 #include <string.h> 4 5 #define M 80 6 #define N 5 7 8 typedef struct { 9 char id[10]; 10 char name[20]; 11 char class[20]; 12 } STU; 13 14 void read(STU student[]) { 15 FILE *fp; 16 int i; 17 fp = fopen("list.txt", "r"); 18 if (fp == NULL) { 19 printf("fail to open file to read\n"); 20 return; 21 } 22 for (i = 0; i < M; i++) { 23 fscanf(fp, "%s %s %s", student[i].id, student[i].name, student[i].class); 24 } 25 fclose(fp); 26 } 27 28 void process(STU student[], STU select[], int n) { 29 int x[M] = {0}; 30 int i; 31 srand((unsigned int)time(NULL)); 32 for (i = 0; i < n; i++) { 33 int a; 34 do { 35 a = rand() % M; 36 } while (x[a] == 1); 37 x[a] = 1; 38 select[i] = student[a]; 39 } 40 } 41 42 void output(STU select[], int n) { 43 printf("被抽中的学生信息:\n"); 44 int i; 45 for (i = 0; i < n; i++) { 46 printf("%-20s %-10s %-20s\n", select[i].id, select[i].name, select[i].class); 47 } 48 } 49 50 void write(STU select[], int n) { 51 FILE *fp; 52 fp = fopen("20241222.txt", "w"); 53 if (fp == NULL) { 54 printf("fail to open file to read\n"); 55 return; 56 } 57 int i; 58 for (i = 0; i < n; i++) { 59 fprintf(fp, "%s %s %s\n", select[i].id, select[i].name, select[i].class); 60 } 61 fclose(fp); 62 } 63 64 int main() { 65 STU student[M]; 66 STU select[N]; 67 68 read(student); 69 70 process(student, select, M); 71 72 output(select, N); 73 74 write(select, N); 75 76 return 0; 77 }