实验7 文件应用编程

实验任务4

源代码

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

 

运行截图

捕获

试验任务5

源代码

  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     system("pause");
 42     return 0;
 43 }
 44 
 45 // 把所有考生完整信息输出到屏幕上
 46 // 准考证号,姓名,客观题得分,操作题得分,总分,结果
 47 void output(STU st[], int n) {
 48     int i;
 49     
 50     printf("准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n");
 51     for (i = 0; i < n; i++)
 52         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);
 53 }
 54 
 55 // 从文本文件examinee.txt读入考生信息:准考证号,姓名,客观题得分,操作题得分
 56 void read(STU st[], int n) {
 57     int i;
 58     FILE *fin;
 59 
 60     fin = fopen("examinee.txt", "r");
 61     if (!fin) {
 62         printf("fail to open file\n");
 63         return;
 64     }
 65 
 66     for (i = 0; i < n; i++)
 67         fscanf(fin, "%ld %s %f %f", &st[i].id, st[i].name, &st[i].objective, &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 j=0;
 76  int person=0;
 77  for(i=0;i<n;++i){
 78     st[i].sum=st[i].objective+st[i].subjective;
 79     if(st[i].sum>=60){
 80         strcpy(st[i].result,"通过");
 81         person++;
 82         st_pass[j]=st[i];
 83         j++;
 84     }
 85     else
 86         strcpy(st[i].result,"未通过");
 87  
 88  }
 89  return person;
 90 }
 91 
 92 // 把通过考试的考生完整信息写入文件list_pass.txt
 93 // 准考证号,姓名,客观题得分,操作题得分,总分,结果
 94 void write(STU st[], int n) {
 95    FILE *fp;
 96    int i;
 97    fp=fopen("list_pass.txt","w");
 98 
 99    if(fp==NULL){
100     printf("fail to open file to write");
101     return;
102    }
103    fprintf(fp,"准考证号\t姓名\t客观题得分\t操作题得分\t总分\t结果\t\n");
104    for(i=0;i<n;i++){
105    fprintf(fp,"%ld%c%f%f%f",st[i].id,st[i].name,st[i].objective,st[i].subjective,st[i].sum,st[i].result);
106    fclose(fp);
107    }
108 
109 }

 

运行截图

2352352342

QQ截图20260623212302

 

 实验任务6

源代码

 1 #pragma execution_character_set("utf-8") 
 2 #include<stdio.h>
 3 #include<string.h>
 4 #include<stdlib.h>
 5 #include<time.h>
 6 
 7 
 8 typedef struct{
 9     char id[10];
10     char name[40];
11     char class_id[40];
12 }student;
13 
14 int main(){
15     student STU[80];
16     int lucky[5];
17     int i,j;
18     int index;
19     char filename[30];
20     FILE *fin,*fout;
21     fin=fopen("list.txt","r");
22     
23     if(fin==NULL){
24         printf("fail to open file to read!");
25         return 1;
26     }
27 
28     for(i=0;i<80;i++){
29         fscanf(fin,"%s%s%s",STU[i].id,STU[i].name,STU[i].class_id);
30     }
31     fclose(fin);
32 
33     srand(time(NULL));
34 
35     for(i=0;i<5;i++){
36         while(1){//不断生成,直到不重复
37         int flag=0;
38         index=rand()%80;
39         for(j=0;j<i;j++){
40             if(lucky[j]==index){
41                 flag=1;
42                 break;
43             }
44         }
45         if(flag==0){
46             lucky[i]=index;
47             break;
48            }
49         }
50     }
51 
52 
53     printf("输入文件名:\n");
54     scanf("%s",filename);
55     fout=fopen(filename,"w");
56 
57     if(fout==NULL){
58         printf("fail to open file to write!");
59         return 1;
60     }
61     printf("------------中奖名单-----------\n");
62     fprintf(fout,"------------中奖名单-----------\n");
63 
64     int num;
65     for(i=0;i<5;i++){
66         num=lucky[i];
67         printf("%-15s %-14s %-s\n", STU[num].id, STU[num].name, STU[num].class_id);
68         fprintf(fout,"%-15s %-14s %-s\n", STU[num].id, STU[num].name, STU[num].class_id);
69         }
70     printf("-------------保存到文件---------\n");
71     printf("名单已保存到%s\n",filename);
72 
73     fclose(fout);
74     system("pause");
75     return 0;
76 }

 

运行截图 

23

 

posted @ 2026-06-23 21:24  code_000  阅读(5)  评论(0)    收藏  举报