实验7

实验任务4:

源代码:

 1 #include<stdio.h>
 2 #include<string.h>
 3 #define N 3
 4 
 5 void write();
 6 
 7 int main(){
 8     printf("date4.txt统计结果:");
 9     printf("\n");
10     write();
11     return 0;
12 }
13 
14 void write(){
15     char *ptr[N] = {"0123456789-0123456789",
16                     "nuist2025",
17                     "cosmos galaxy"};
18     FILE *fp;
19     int i,s=0,n=0;
20     
21     fp = fopen("date4.txt","w");
22     if(!fp){
23         printf("fail to open file to write\n");
24         return;
25     }
26     
27     for(i=0;i<N;i++){
28         fputs(ptr[i],fp);
29         fputs("\n",fp);
30         
31         n++;
32         
33         for (int j = 0; ptr[i][j] != '\0'; j++) {
34             if (ptr[i][j] != ' ')
35                 s++;
36         }
37     }
38     
39     fclose(fp);
40 
41     printf("行数: %d\n", n);
42     printf("字符数(不计空白符): %d\n", s);
43 }

图片:

 

 

实验任务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     FILE *fout;
 69     fout = fopen("list_pass.txt", "w");
 70     if (!fout) {
 71         printf("fail to open file to write\n");
 72         return;
 73     }
 74 
 75     for (int i = 0; i < n; i++) {
 76         if (s[i].sum >= 60) { 
 77             fprintf(fout, "%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 
 81     fclose(fout);
 82 }
 83 
 84 
 85 int process(STU st[], int n, STU st_pass[]) {
 86     int pass_count = 0;
 87     int i;
 88 
 89     for (i = 0; i < n; i++) {
 90         st[i].sum = st[i].objective + st[i].subjective; 
 91         if(st[i].sum >= 60){ 
 92             strcpy(st[i].result, "通过");
 93             st_pass[pass_count++] = st[i];
 94         }
 95         else
 96             strcpy(st[i].result, "不通过");
 97     }
 98 
 99     return pass_count;
100 }

图片:

 

 

实验任务6:

源代码:

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<string.h>
 4 #include<time.h> 
 5 #define N 80
 6 
 7 typedef struct {
 8     long id;
 9     char name[20];
10     char class[20];
11 } STU;
12 
13 void read(STU s[], int *n);
14 void write(STU s[],int celect[],int n);
15 
16 int main(){
17     STU s[N];
18     int select[5];
19     int i,count=0;
20     int total_students = 0;
21     int selected = 0;
22     int random_index;
23     
24     srand(time(NULL));
25     
26     read(s,&total_students);
27      
28     while(count<5){
29         selected = 0;
30         random_index = rand() % total_students;
31         
32         for(i=0;i<count;i++){
33             if(select[i] == random_index){
34                 selected = 1;
35                 break;    
36             }
37         }
38     
39         if(!selected){
40             select[count] = random_index;
41             count++;
42         }
43     }
44         
45     printf("------------随机抽点名单------------\n");
46     for(i=0;i<5;i++){
47         int index = select[i];
48         printf("%ld %s %s\n", s[index].id,s[index].name,s[index].class);    
49     }
50     printf("------------保存到文件------------\n"); 
51     printf("输入文件名:");
52     write(s,select, 5);
53     printf("保存成功!");
54     
55     return 0;
56 }
57 
58 void read(STU s[], int *n){
59     int i = 0;
60     FILE *fp;
61     
62     fp = fopen("list.txt", "r");
63     if(!fp){
64         printf("fail to open fail to read\n");
65         return;
66     }
67     
68     while(fscanf(fp,"%ld %s %s", &s[i].id,s[i].name,s[i].class) == 3){
69         i++;
70         if(i>=N) break;
71     }
72     
73     
74     *n = i;
75     fclose(fp);
76 }
77 
78 void write(STU s[],int select[], int n){
79     char filename[80];
80     int i;
81     FILE *file;
82     
83     scanf("%s", filename);
84     
85     file = fopen(filename, "w");
86     if(!file){
87         printf("fail to open\n", filename);
88         return;
89     }
90     
91     for(i=0;i<n;i++){
92         int index = select[i];
93         fprintf(file,"%ld %s %s",s[i].id,s[i].name,s[i].class);
94     }
95     
96     fclose(file);
97 }

图片:

 

posted @ 2024-12-30 09:37  scjzl  阅读(16)  评论(0)    收藏  举报