实验7 文件应用编程

实验四

源代码

#include <stdio.h>
#include <ctype.h>  

int main(void)
{
    FILE *fp;
    char ch;
    int line_count = 0;    
    int char_count = 0;    

    if ((fp = fopen("data4.txt", "r")) == NULL)
    {
        perror("打开文件失败");
        return 1;
    }

    while ((ch = fgetc(fp)) != EOF)
    {
        if (ch == '\n')
        {
            line_count++; 
        }
        if (!isspace(ch))
        {
            char_count++;
        }
    }
    
    printf("data4.txt统计结果:\n");
    printf("行数: %d\n", line_count);
    printf("字符数(不计空白符): %d\n", char_count);

    fclose(fp); 
    return 0;
}

屏幕截图 2026-06-23 162344

 

实验五

源代码

  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 int process(STU st[], int n, STU st_pass[])
 72 {
 73     int pass_num = 0;
 74     for(int i = 0; i < n; i++)
 75     {
 76         
 77         st[i].sum = st[i].objective + st[i].subjective;
 78 
 79         if(st[i].sum >= 60)
 80         {
 81             strcpy(st[i].result, "通过");
 82             st_pass[pass_num] = st[i];
 83             pass_num++;
 84         }
 85         else
 86         {
 87             strcpy(st[i].result, "未通过");
 88         }
 89     }
 90     return pass_num; 
 91 }
 92 
 93 void write(STU st[], int n)
 94 {
 95     FILE *fout;
 96     fout = fopen("list_pass.txt", "w");
 97     if(!fout)
 98     {
 99         printf("文件打开失败,无法写入\n");
100         return;
101     }
102 
103     fprintf(fout, "准考证号\t姓名\t客观题\t操作题\t总分\t结果\n");
104     for(int i = 0; i < n; i++)
105     {
106         fprintf(fout, "%ld\t%s\t%.2f\t%.2f\t%.2f\t%s\n",
107                 st[i].id, st[i].name,
108                 st[i].objective, st[i].subjective, st[i].sum, st[i].result);
109     }
110     fclose(fout);
111 }

屏幕截图 2026-06-23 164244

屏幕截图 2026-06-23 165704

 

实验六

源代码

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <time.h>
 4 #include <string.h>
 5 
 6 #define TOTAL_STU 80    
 7 #define LUCKY_NUM 5     
 8 
 9 typedef struct {
10     char id[20];    
11     char name[20];  
12     char cls[30];   
13 } Student;
14 
15 int readAllStudent(Student stu[]) {
16     FILE *fp = fopen("list.txt", "r");
17     if (fp == NULL) {
18         printf("打开list.txt失败,请检查文件路径!\n");
19         exit(1);
20     }
21     int count = 0;
22     while (fscanf(fp, "%s %s %s", stu[count].id, stu[count].name, stu[count].cls) != EOF && count < TOTAL_STU) {
23         count++;
24     }
25     fclose(fp);
26     return count;
27 }
28 
29 void getLuckyIndex(int idx[], int total, int luckyCnt) {
30     int used[TOTAL_STU] = {0}; 
31     srand((unsigned)time(NULL)); 
32 
33     for (int i = 0; i < luckyCnt; ) {
34         int randPos = rand() % total; 
35         if (used[randPos] == 0) {
36             idx[i++] = randPos;
37             used[randPos] = 1;
38         }
39     }
40 }
41 
42 
43 int writeLucky(Student lucky[], int cnt, char fileName[]) {
44     FILE *fp = fopen(fileName, "w");
45     if (fp == NULL) {
46         printf("文件创建失败!\n");
47         return 0;
48     }
49     for (int i = 0; i < cnt; i++) {
50         fprintf(fp, "%s\t%s\t%s\n", lucky[i].id, lucky[i].name, lucky[i].cls);
51     }
52     fclose(fp);
53     return 1;
54 }
55 
56 int main() {
57     Student allStu[TOTAL_STU];
58     Student luckyStu[LUCKY_NUM];
59     int luckyIdx[LUCKY_NUM];
60     char saveFileName[50];
61 
62     int realTotal = readAllStudent(allStu);
63     if (realTotal < LUCKY_NUM) {
64         printf("学生总数不足%d人,无法抽奖!", LUCKY_NUM);
65         return 1;
66     }
67 
68     getLuckyIndex(luckyIdx, realTotal, LUCKY_NUM);
69 
70     for (int i = 0; i < LUCKY_NUM; i++) {
71         luckyStu[i] = allStu[luckyIdx[i]];
72     }
73 
74     printf("--------中奖名单--------\n");
75     for (int i = 0; i < LUCKY_NUM; i++) {
76         printf("%s\t%s\t%s\n", luckyStu[i].id, luckyStu[i].name, luckyStu[i].cls);
77     }
78     printf("------------------------\n");
79 
80     printf("输入文件名:");
81     scanf("%s", saveFileName);
82 
83     if (writeLucky(luckyStu, LUCKY_NUM, saveFileName)) {
84         printf("保存成功!\n");
85     } else {
86         printf("保存失败!\n");
87     }
88 
89     return 0;
90 }

屏幕截图 2026-06-23 165004

屏幕截图 2026-06-23 165009

 

posted @ 2026-06-23 16:57  渝寒京  阅读(5)  评论(0)    收藏  举报