实验七
任务四
1 #include <stdio.h> 2 #define MAX 1000 3 #include <stdlib.h> 4 int main(){ 5 6 FILE *fp; 7 char line[MAX]; 8 int lines = 0; 9 int chars = 0; 10 int i; 11 12 fp =fopen("data4.txt","r"); 13 if(fp == NULL){ 14 printf("无法打开文件 data4.txt\n"); 15 return 1; 16 } 17 18 while (fgets(line, sizeof(line), fp) != NULL){ 19 lines++; 20 21 22 for( i = 0; line[i] != '\0'; i++){ 23 char c = line[i]; 24 25 if (c != ' ' && c != '\n' && c != '\t') { 26 chars++; 27 } 28 } 29 } 30 31 fclose(fp); 32 33 printf("data4.txt统计结果: \n"); 34 printf("行数:%d\n", lines); 35 printf("字符数(不及空白符):%d\n", chars); 36 system("pause"); 37 return 0; 38 }

任务五
1 #include <stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 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 system("pause"); 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, 67 &st[i].subjective); 68 69 fclose(fin); 70 } 71 72 // 对考生信息进行处理:计算每位考生考试总分、结果;统计并返回通过考试的人数 73 int process(STU st[], int n, STU st_pass[]){ 74 int i; 75 int cnt = 0; 76 for(i = 0; i < n; i++) 77 st[i].sum = st[i].objective + st[i].subjective; 78 for(i = 0; i < n; i++) 79 if(st[i].sum >= 60){ 80 strcpy(st[i].result, "通过"); 81 st_pass[cnt] = st[i]; 82 cnt++; 83 }else { 84 strcpy(st[i].result, "不通过"); 85 } 86 87 return cnt; 88 89 } 90 91 92 // 把通过考试的考生完整信息写入文件list_pass.txt 93 // 准考证号,姓名,客观题得分,操作题得分,总分,结果 94 void write(STU st[], int n){ 95 int i; 96 FILE *fp; 97 fp = fopen("list_pass.txt","w"); 98 if(fp == NULL){ 99 printf("无法创建 list_pass.txt\n"); 100 return ; 101 } 102 103 for(i = 0; i < n; i++) { 104 fprintf(fp,"%ld %s %.2f %.2f %.2f %s\n", 105 st[i].id, st[i].name, 106 st[i].objective, st[i].subjective, 107 st[i].sum, st[i].result); 108 } 109 fclose(fp); 110 }
运行截图

任务六
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <time.h> 4 #include <string.h> 5 6 #define N 80 7 #define K 5 8 #define LEN 20 9 10 typedef struct { 11 long id; 12 char name[LEN]; 13 char cls[30]; 14 } stu; 15 16 void read(stu s[], int *n); 17 void pick(stu s[], int n, stu w[], int k); 18 void sort(stu w[], int k); 19 void output(stu w[], int k); 20 void save(stu w[], int k, const char *fname); 21 22 int main() { 23 char fname[100]; 24 stu all[N], win[K]; 25 int cnt = 0; 26 27 read(all, &cnt); 28 if (cnt == 0) { 29 printf("文件读取失败或无数据\n"); 30 return 1; 31 } 32 printf("成功读取 %d 名学生\n", cnt); 33 34 srand((unsigned)time(NULL)); 35 pick(all, cnt, win, K); 36 37 sort(win, K); 38 39 output(win, K); 40 printf("输入文件名:"); 41 scanf("%s", fname); 42 save(win, K, fname); 43 system("pause"); 44 return 0; 45 } 46 47 void read(stu s[], int *n) { 48 int i = 0; 49 FILE *fp = fopen("list.txt", "r"); 50 if (!fp) { 51 *n = 0; 52 return; 53 } 54 while (fscanf(fp, "%ld %s %s", &s[i].id, s[i].name, s[i].cls) == 3 && i < N) 55 i++; 56 *n = i; 57 fclose(fp); // 建议加上关闭文件 58 } 59 60 void pick(stu s[], int n, stu w[], int k) { 61 int flag[N] = {0}, cnt = 0; 62 while (cnt < k) { 63 int idx = rand() % n; 64 if (!flag[idx]) { 65 flag[idx] = 1; 66 w[cnt++] = s[idx]; 67 } 68 } 69 } 70 71 void sort(stu w[], int k) { 72 int i, j; 73 stu t; 74 for (i = 0; i < k - 1; i++) 75 for (j = 0; j < k - 1 - i; j++) 76 if (w[j].id > w[j + 1].id) { 77 t = w[j]; 78 w[j] = w[j + 1]; 79 w[j + 1] = t; 80 } 81 } 82 83 void output(stu w[], int k) { 84 int i; 85 printf("\n中奖名单(学号 姓名 班级):\n"); 86 for (i = 0; i < k; i++) 87 printf("%ld %s %s\n", w[i].id, w[i].name, w[i].cls); 88 printf("\n"); 89 } 90 91 void save(stu w[], int k, const char *fname) { 92 int i; 93 FILE *fp = fopen(fname, "w"); 94 if (!fp) { 95 printf("文件创建失败\n"); 96 return; 97 } 98 for (i = 0; i < k; i++) 99 fprintf(fp, "%ld %s %s\n", w[i].id, w[i].name, w[i].cls); 100 fclose(fp); 101 }
运行截图


浙公网安备 33010602011771号