实验七

任务四

 1 #include<stdio.h>
 2 
 3 int main(){
 4   FILE *fp;
 5   int i,n=0,ch;
 6   int lines=0;
 7   int newchar='\0';
 8   fp=fopen("data4.txt","r");
 9   if(fp==NULL){
10       printf("无法打开文件\n");
11       return 1;
12   }
13   while((ch=fgetc(fp))!=EOF){
14       if(ch=='\n')
15         lines++;
16   
17   if(ch!=' '&&ch!='\n'&&ch!='\t')
18      n++;
19   newchar=ch;
20  
21 }
22  if(newchar!='\n'&&newchar!='\0')
23   lines++;
24 fclose(fp);
25 printf("data4.txt统计结果:\n");
26 printf("行数:%d\n",lines);
27 printf("字符数(不计空白符):%d\n",n);
28 return 0;
29 }
View Code

屏幕截图 2026-06-21 164858

任务五

  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      st[i].sum=st[i].objective+st[i].subjective;
 76      if(st[i].sum>=60){
 77          strcpy(st[i].result,"通过");
 78          st_pass[cnt++]=st[i]; 
 79      }
 80      else{
 81          strcpy(st[i].result,"不通过"); 
 82      }
 83      
 84  }
 85  return cnt;
 86 }
 87 
 88 // 把通过考试的考生完整信息写入文件list_pass.txt
 89 // 准考证号,姓名,客观题得分,操作题得分,总分,结果
 90 void write(STU st[], int n) {
 91    FILE *fp;
 92    fp=fopen("list_pass.txt","w");
 93    
 94    if(fp==NULL){
 95        printf("无法打开文件");
 96        return; 
 97    }
 98    fprintf(fp,"%-10s %-10s  %-20s %-20s %-20s %-20s\n","准考证号","姓名","客观题得分","操作题得分","总分","结果");
 99    for(int i=0;i<n;i++){
100        fprintf(fp,"%-10ld%-12s%-25.2f%-25.2f%-25.2f%s\n",st[i].id,st[i].name,st[i].objective, st[i].subjective, st[i].sum, st[i].result);
101    }
102    
103    fclose(fp);
104    
105 }
View Code

屏幕截图 2026-06-21 170946

屏幕截图 2026-06-21 172647

任务六

 

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<time.h>
 4 #include<string.h>
 5 #define M 100
 6 #define N 5
 7 
 8 typedef struct{
 9     long id;
10     char name[20];
11     char cla[50];
12 }Student;
13 
14 int main(){
15     FILE *fp;
16     Student lucky_stu[N];
17     Student all[M];
18     int total=0;
19     char filename[50];
20     int choose;
21     fp=fopen("list.txt","r");
22     if(fp==NULL){
23     printf("打开失败");
24     return 1;
25     } 
26      while(fscanf(fp,"%ld %s %s",&all[total].id,all[total].name,all[total].cla)==3)
27        total++;
28     fclose(fp);
29     
30     srand((unsigned)time(NULL));
31     int design[M]={0};
32     for(int i=0;i<N;i++){
33         do{
34             choose=rand()%total;
35         }while(design[choose]);
36         design[choose]=1;
37         lucky_stu[i]=all[choose];
38     }
39     printf("中奖名单");
40     for(int i=0;i<N;i++)
41     printf("%-10ld %-10s  %-10s\n",lucky_stu[i].id,lucky_stu[i].name,lucky_stu[i].cla);
42     printf("请输入文件名");
43     scanf("%s",filename); 
44     fp=fopen(filename,"w");
45     
46     if(fp==NULL){
47     printf("文件打开失败");
48     return 1;
49 }
50 for(int i=0;i<N;i++){
51 fprintf(fp,"%-10ld %-10s %-10s\n",lucky_stu[i].id,lucky_stu[i].name,lucky_stu[i].cla);
52 }
53 fclose(fp);
54 printf("保存成功!");
55 return 0;      
56 }
View Code

屏幕截图 2026-06-22 125754

屏幕截图 2026-06-22 125815

 

posted @ 2026-06-22 12:59  文xuan  阅读(0)  评论(0)    收藏  举报