实验7

 实验任务4 

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

屏幕截图 2025-12-30 203936

实验任务5

  1 #include <stdio.h>
  2 #include <string.h>
  3 #define N 10
  4 
  5 typedef struct {
  6     long id;            // 准考证号
  7     char name[20];      // 姓名
  8     float objective;    // 客观题得分
  9     float subjective;   // 操作题得分
 10     float sum;          // 总分
 11     char result[10];    // 考试结果
 12 } STU;
 13 
 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 // 把所有考生完整信息输出到屏幕上
 42 // 准考证号,姓名,客观题得分,操作题得分,总分,结果
 43 void output(STU st[], int n) {
 44     int i;
 45     
 46     printf("准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n");
 47     for (i = 0; i < n; i++)
 48         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);
 49 }
 50 
 51 // 从文本文件examinee.txt读入考生信息:准考证号,姓名,客观题得分,操作题得分
 52 void read(STU st[], int n) {
 53     int i;
 54     FILE *fin;
 55 
 56     fin = fopen("examinee.txt", "r");
 57     if (!fin) {
 58         printf("fail to open file\n");
 59         return;
 60     }
 61 
 62     for (i = 0; i < n; i++)
 63         fscanf(fin, "%ld %s %f %f", &st[i].id, st[i].name, &st[i].objective, &st[i].subjective);
 64 
 65     fclose(fin);
 66 }
 67 
 68 // 把通过考试的考生完整信息写入文件list_pass.txt
 69 // 准考证号,姓名,客观题得分,操作题得分,总分,结果
 70 void write(STU s[], int n) {
 71     int i;
 72     FILE *fp=fopen("list_pass.txt", "w");
 73     
 74     if(fp == NULL){
 75         perror("文件打开失败");
 76         return; 
 77     }
 78     
 79     for (i=0; i<n; ++i){
 80         if(s[i].sum >=60)
 81             fprintf(fp, "%ld %s %.1f %.1f %.1f %s\n", &s[i].id, &s[i].name, &s[i].objective, &s[i].subjective, &s[i].sum, &s[i].result);
 82     }
 83     fclose(fp);
 84     fp = NULL;    
 85 }
 86 
 87 // 对考生信息进行处理:计算每位考生考试总分、结果;统计考试通过的人数
 88 int process(STU st[], int n, STU st_pass[]) {
 89     int i, count=0;
 90     
 91     for (i=0; i<n; ++i){
 92         st[i].sum = st[i].objective + st[i].subjective;
 93         
 94         if(st[i].sum >=60){
 95             strcpy(st[i].result, "通过");
 96             st_pass[count++]=st[i];
 97         }
 98         else
 99             strcpy(st[i].result, "不通过");
100     }
101     return count;
102 }
实验任务5

屏幕截图 2025-12-30 212019

 

 

 

实验任务6

 1 #include <stdio.h>
 2 #include <time.h>
 3 #include <string.h>
 4 #include <stdlib.h>
 5 #define N 80
 6 
 7 int main(){
 8     char s[N][N];
 9     char filename[N];
10     FILE *fp;
11     int k=0, count=0;
12     int flag[N]={0}; 
13     
14     fp = fopen("list.txt", "r");
15     if(!fp){
16         perror("list.txt");
17         return 1;
18     }
19     
20     while(fgets(s[k], N, fp)!=NULL&&k<N){
21         k++;
22     }
23     fclose(fp);
24     
25     printf("----------中奖名单----------\n"); 
26 
27     srand((unsigned int)time(NULL));
28     while(count<5){
29         int n=rand()%k;
30         if(flag[n]==0){
31             printf("%s",s[n]);
32             count++;
33             flag[n]=1;
34         }
35     }
36 
37     printf("\n----------保存到文件----------\n");
38     printf("输入文件名:");
39     
40     scanf("%s", filename);
41     fp=fopen(filename, "w");
42     if(!fp){
43         perror("文件保存失败\n");
44         return 1;
45     } 
46     for(int i=0; i<N; i++){
47         if(flag[i]==1)
48             fputs(s[i], fp);
49     }
50     fclose(fp);
51     printf("文件保存成功\n"); 
52     
53     return 0;
54 }
实验任务6

屏幕截图 2025-12-24 152633

屏幕截图 2025-12-24 152625

 

posted @ 2025-12-30 21:20  aiuydihua  阅读(2)  评论(0)    收藏  举报