实验七
task4.c
1 #include <stdio.h> 2 3 int main() 4 { 5 FILE *fp; 6 int ch; 7 int line = 0; 8 int char_cnt = 0; 9 int has_content = 0; 10 11 fp = fopen("data4.txt", "r"); 12 if(fp == NULL) 13 { 14 printf("打开文件失败\n"); 15 return 1; 16 } 17 18 while((ch = fgetc(fp)) != EOF) 19 { 20 has_content = 1; 21 if(ch == ' ' || ch == '\n' || ch == '\t') 22 { 23 if(ch == '\n') 24 line++; 25 continue; 26 } 27 char_cnt++; 28 } 29 30 if(has_content) 31 line++; 32 33 fclose(fp); 34 35 printf("data4.txt统计结果:\n"); 36 printf("行数: %d\n", line); 37 printf("字符数(不计空白符): %d\n", char_cnt); 38 39 return 0; 40 }


task5.c
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 // 函数声明 16 void read(STU st[], int n); 17 void write(STU st[], int n); 18 void output(STU st[], int n); 19 int process(STU st[], int n, STU st_pass[]); 20 21 int main() { 22 STU stu[N], stu_pass[N]; 23 int cnt; 24 double pass_rate; 25 26 printf("从文件读入%d个考生信息: 已完成\n", N); 27 read(stu, N); 28 29 printf("\n对考生成绩进行统计: 已完成\n"); 30 cnt = process(stu, N, stu_pass); 31 32 printf("\n所有考生完整信息:\n"); 33 output(stu, N); 34 35 printf("\n通过考试的名单写入文件: 已完成!\n"); 36 write(stu_pass, cnt); 37 38 pass_rate = 1.0 * cnt / N; 39 printf("\n本次等级考试通过率: %.2f%%\n", pass_rate*100); 40 41 return 0; 42 } 43 44 // 把所有考生完整信息输出到屏幕上 45 // 准考证号,姓名,客观题得分,操作题得分,总分,结果 46 void output(STU st[], int n) { 47 int i; 48 49 printf("准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n"); 50 for (i = 0; i < n; i++) 51 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); 52 } 53 54 // 从文本文件examinee.txt读入考生信息:准考证号,姓名,客观题得分,操作题得分 55 void read(STU st[], int n) { 56 int i; 57 FILE *fin; 58 59 fin = fopen("examinee.txt", "r"); 60 if (!fin) { 61 printf("fail to open file\n"); 62 return; 63 } 64 65 for (i = 0; i < n; i++) 66 fscanf(fin, "%ld %s %f %f", &st[i].id, st[i].name, &st[i].objective, &st[i].subjective); 67 68 fclose(fin); 69 } 70 71 // 对考生信息进行处理:计算每位考生考试总分、结果;统计并返回通过考试的人数 72 int process(STU st[], int n, STU st_pass[]) { 73 int i, cnt = 0; 74 for(i = 0; i < n; i++) 75 { 76 st[i].sum = st[i].objective + st[i].subjective; 77 if(st[i].sum >= 60) 78 { 79 strcpy(st[i].result, "通过"); 80 st_pass[cnt] = st[i]; 81 cnt++; 82 } 83 else 84 { 85 strcpy(st[i].result, "不通过"); 86 } 87 } 88 return cnt; 89 } 90 91 // 把通过考试的考生完整信息写入文件list_pass.txt 92 // 准考证号,姓名,客观题得分,操作题得分,总分,结果 93 void write(STU st[], int n) { 94 FILE *fp; 95 int i; 96 fp = fopen("list_pass.txt", "w"); 97 if(!fp) 98 { 99 printf("fail to open write file\n"); 100 return; 101 } 102 fprintf(fp,"准考证号\t姓名\t客观题得分\t操作题得分\t总分\t结果\n"); 103 for(i = 0; i < n; i++) 104 { 105 fprintf(fp,"%ld\t%s\t%.2f\t%.2f\t%.2f\t%s\n", 106 st[i].id, st[i].name, st[i].objective, st[i].subjective, st[i].sum, st[i].result); 107 } 108 fclose(fp); 109 }

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

选做:
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <time.h> 4 #include <string.h> 5 6 #define N 80 7 #define WIN 5 8 9 typedef struct { 10 char id[20]; 11 char name[20]; 12 char cls[30]; 13 } Stu; 14 15 // 按学号升序排序 16 void sortStu(Stu arr[], int n) 17 { 18 int i, j; 19 Stu tmp; 20 for (i = 0; i < n - 1; i++) 21 { 22 for (j = 0; j < n - i - 1; j++) 23 { 24 if (strcmp(arr[j].id, arr[j+1].id) > 0) 25 { 26 tmp = arr[j]; 27 arr[j] = arr[j+1]; 28 arr[j+1] = tmp; 29 } 30 } 31 } 32 } 33 34 int main() 35 { 36 Stu all[N]; 37 int flag[N] = {0}; 38 Stu winner[WIN]; 39 int i, cnt = 0, rnd; 40 char filename[50]; 41 FILE *fp; 42 time_t now = time(NULL); 43 struct tm *t = localtime(&now); 44 45 46 strftime(filename, sizeof(filename), "%Y%m%d.txt", t); 47 48 49 fp = fopen("list.txt", "r"); 50 if (!fp) 51 { 52 printf("打开list.txt失败\n"); 53 return 1; 54 } 55 for (i = 0; i < N; i++) 56 { 57 fscanf(fp, "%s %s %s", all[i].id, all[i].name, all[i].cls); 58 } 59 fclose(fp); 60 61 62 srand((unsigned)time(NULL)); 63 while (cnt < WIN) 64 { 65 rnd = rand() % N; 66 if (flag[rnd] == 0) 67 { 68 flag[rnd] = 1; 69 winner[cnt++] = all[rnd]; 70 } 71 } 72 73 74 sortStu(winner, WIN); 75 76 77 printf("----------------%s中奖名单----------------\n", filename); 78 for (i = 0; i < WIN; i++) 79 { 80 printf("%s\t%s\t%s\n", winner[i].id, winner[i].name, winner[i].cls); 81 } 82 83 84 fp = fopen(filename, "w"); 85 for (i = 0; i < WIN; i++) 86 { 87 fprintf(fp, "%s\t%s\t%s\n", winner[i].id, winner[i].name, winner[i].cls); 88 } 89 fclose(fp); 90 printf("文件保存成功!\n"); 91 92 return 0; 93 }

浙公网安备 33010602011771号