实验七

task4

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <ctype.h>
 4 
 5 int main() {
 6     int lineCount = 0;
 7     int charCount = 0;
 8     char line[81];
 9     FILE *fp;
10 
11     fp = fopen("d:\\data4.txt", "r");
12     if (!fp) {
13         printf("fail to open file to read\n");
14         system("pause");
15         return 1;
16     }
17 
18     while (fgets(line, sizeof(line), fp) != NULL) {
19         lineCount++;
20         for (int i = 0; line[i] != '\0'; i++) {
21             if (!isspace((unsigned char)line[i])) {
22                 charCount++;
23             }
24         }
25     }
26 
27     fclose(fp);
28 
29     printf("data4.txt统计结果:\n");
30     printf("行数:\t\t%10d\n", lineCount);
31     printf("字符数(不计空白符):%4d\n", charCount);
32     
33     system("pause");
34     return 0;
35 }
View Code

 

task5

  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 void outputPass(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     outputPass(stu_pass, cnt);  // 修改:输出通过考试的学生
 35 
 36     printf("\n所有考生完整信息:\n");
 37     output(stu, N);  // 新增:输出所有考生信息
 38 
 39     write(stu_pass, cnt);  // 修改:只写入通过考试的学生
 40 
 41     pass_rate = 1.0 * cnt / N;
 42     printf("\n本次等级考试通过率: %.2f%%\n", pass_rate*100);
 43     system("pause");
 44     return 0;
 45 }
 46 
 47 // 输出所有考生完整信息
 48 void output(STU st[], int n) {
 49     printf("准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n");
 50     for (int i = 0; i < n; i++)
 51         printf("%ld\t\t%s\t%.2f\t\t%.2f\t\t%.2f\t\t%s\n", 
 52                st[i].id, st[i].name, st[i].objective, 
 53                st[i].subjective, st[i].sum, st[i].result);
 54 }
 55 
 56 // 输出通过考试的考生信息
 57 void outputPass(STU st[], int n) {
 58     printf("准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n");
 59     for (int i = 0; i < n; i++)
 60         printf("%ld\t\t%s\t%.2f\t\t%.2f\t\t%.2f\t\t%s\n", 
 61                st[i].id, st[i].name, st[i].objective, 
 62                st[i].subjective, st[i].sum, st[i].result);
 63 }
 64 
 65 // 从文本文件examinee.txt读入考生信息
 66 void read(STU st[], int n) {
 67     FILE *fin = fopen("examinee.txt", "r");
 68     if (!fin) {
 69         printf("无法打开文件!\n");
 70         exit(1);  // 修改:使用exit终止程序
 71     }
 72 
 73     for (int i = 0; i < n && fscanf(fin, "%ld %s %f %f", 
 74            &st[i].id, st[i].name, &st[i].objective, &st[i].subjective) == 4; i++);
 75 
 76     fclose(fin);
 77 }
 78 
 79 // 把通过考试的考生完整信息写入文件list_pass.txt
 80 void write(STU s[], int n) {
 81     FILE *fp = fopen("list_pass.txt", "w");
 82     if (!fp) {
 83         printf("无法创建输出文件!\n");
 84         return;
 85     }
 86 
 87     fprintf(fp, "准考证号  姓名  客观题得分  操作题得分  总分  结果\n");
 88     for (int i = 0; i < n; i++)
 89         fprintf(fp, "%-10ld%-10s%-15.2f%-15.2f%-15.2f%-10s\n", 
 90                s[i].id, s[i].name, s[i].objective, 
 91                s[i].subjective, s[i].sum, s[i].result);
 92 
 93     fclose(fp);
 94 }
 95 
 96 // 对考生信息进行处理
 97 int process(STU st[], int n, STU st_pass[]) {
 98     int m = 0;
 99     for (int i = 0; i < n; i++) {
100         st[i].sum = st[i].objective + st[i].subjective;
101         if (st[i].sum >= 60) {
102             strcpy(st[i].result, "通过");
103             st_pass[m++] = st[i];
104         } else {
105             strcpy(st[i].result, "不通过");
106         }
107     }
108     return m;
109 }
View Code

 

task6

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <time.h>
 4 
 5 #define N 80
 6 
 7 typedef struct{
 8     int id;
 9     char name[N];
10     char ban[N];
11 }Student;
12 
13 void random(Student x1[],Student x2[],int n);
14 void output(Student x[],int n);
15 void read(Student x[],int n);
16 void write(Student x[],int n,char ming[]);
17 
18 int main(){
19     Student stu[N],luck[N];
20     char ming[N];
21     read(stu,N);
22     random(stu,luck,N);
23     printf("-----------中奖名单------------\n");
24     output(luck,5);
25     printf("-----------保存到文件----------\n");
26     printf("输入文件名:");
27     scanf("%s",&ming);
28     write(stu,5,ming);
29     system("pause");
30 }
31 
32 void read(Student x[],int n)
33 {
34     FILE *fin;
35     int i;
36     fin=fopen("list.txt","r");
37     while(!feof(fin)){
38         for(i=0;i<n;i++)
39             fscanf(fin,"%d %s %s",&x[i].id,x[i].name,x[i].ban);}
40     fclose(fin);
41 }
42 void random(Student x1[],Student x2[],int n)
43 {
44     int m,i;
45     srand(time(NULL));
46     for(i=0;i<5;i++){
47         m=rand()%(n-1+1);
48         x2[i]=x1[m];}
49 }
50 void output(Student x[],int n)
51 {
52     int i;
53     for(i=0;i<n;i++)
54         printf("%-15d%-10s%-30s\n",x[i].id,x[i].name,x[i].ban);
55 }
56 void write(Student x[],int n,char ming[])
57 {
58     FILE *fp;
59     int i;
60     fp=fopen(ming,"w");
61     for(i=0;i<n;i++)
62         fprintf(fp,"%-15d%-10s%-30s\n",x[i].id,x[i].name,x[i].ban);
63     fclose(fp);
64     printf("保存成功!\n");
65 }
View Code

 

posted @ 2025-06-08 23:05  西嘻哈  阅读(13)  评论(0)    收藏  举报