实验7

任务四

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

int main(void) {
    FILE *fp;
    int ch;
    int lines = 0;
    int chars = 0;
    int has_char_in_line = 0;

    fp = fopen("data4.txt", "r");
    if (fp == NULL) {
        printf("fail to open file\n");
        return 1;
    }

    while ((ch = fgetc(fp)) != EOF) {
        if (ch == '\n') {
            ++lines;
            has_char_in_line = 0;
        } else {
            has_char_in_line = 1;
        }

        if (!isspace((unsigned char)ch))
            ++chars;
    }

    if (has_char_in_line)
        ++lines;

    fclose(fp);

    printf("文件data4.txt中共有%d行\n", lines);
    printf("文件data4.txt中共有%d个非空白字符\n", chars);

    return 0;
}

  结果

1

任务五

#include <stdio.h>
#include <string.h>

#define N 10

typedef struct {
    long id;            // 准考证号
    char name[20];      // 姓名
    float objective;    // 客观题得分
    float subjective;   // 操作题得分
    float sum;          // 总分
    char result[10];    // 考试结果
} STU;

void read(STU st[], int n);
void write(STU st[], int n);
void output(STU st[], int n);
int process(STU st[], int n, STU st_pass[]);

int main(void) {
    STU stu[N], stu_pass[N];
    int cnt;
    double pass_rate;

    printf("从文件读入%d个考生信息: 已完成\n", N);
    read(stu, N);

    printf("\n对考生成绩进行统计: 已完成\n");
    cnt = process(stu, N, stu_pass);

    printf("\n所有考生完整信息:\n");
    output(stu, N);

    printf("\n通过考试的名单写入文件: 已完成!\n");
    write(stu_pass, cnt);

    pass_rate = 1.0 * cnt / N;
    printf("\n本次等级考试通过率: %.2f%%\n", pass_rate * 100);

    return 0;
}

void output(STU st[], int n) {
    int i;

    printf("准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n");
    for (i = 0; i < n; i++)
        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);
}

void read(STU st[], int n) {
    int i;
    FILE *fin;

    fin = fopen("examinee.txt", "r");
    if (!fin) {
        printf("fail to open file\n");
        return;
    }

    for (i = 0; i < n; i++)
        fscanf(fin, "%ld %19s %f %f", &st[i].id, st[i].name,
               &st[i].objective, &st[i].subjective);

    fclose(fin);
}

int process(STU st[], int n, STU st_pass[]) {
    int i;
    int cnt = 0;

    for (i = 0; i < n; i++) {
        st[i].sum = st[i].objective + st[i].subjective;

        if (st[i].sum >= 60) {
            strcpy(st[i].result, "通过");
            st_pass[cnt] = st[i];
            ++cnt;
        } else {
            strcpy(st[i].result, "未通过");
        }
    }

    return cnt;
}

void write(STU st[], int n) {
    int i;
    FILE *fout;

    fout = fopen("list_pass.txt", "w");
    if (!fout) {
        printf("fail to open file\n");
        return;
    }

    fprintf(fout, "准考证号\t姓名\t客观题得分\t操作题得分\t总分\t结果\n");
    for (i = 0; i < n; i++)
        fprintf(fout, "%ld\t%s\t%.2f\t%.2f\t%.2f\t%s\n",
                st[i].id, st[i].name, st[i].objective, st[i].subjective,
                st[i].sum, st[i].result);

    fclose(fout);
}

  结果

2

3

任务六

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#define N 80
#define PICK 5
#define LEN 128

typedef struct {
    long id;
    char name[32];
    char class_name[64];
} Student;

int read_students(Student st[], int maxn);
void make_date_filename(char filename[], int size);
void pick_students(int selected[], int n, int count);
void sort_by_id(Student st[], int n);
void output_students(FILE *fp, Student st[], int n);

int main(int argc, char *argv[]) {
    Student students[N], winners[PICK];
    int selected[PICK];
    int n, i;
    char filename[LEN];
    FILE *fout;

    n = read_students(students, N);
    if (n < PICK) {
        printf("学生人数不足,无法抽取%d位中奖名单\n", PICK);
        return 1;
    }

    if (argc > 1) {
        strncpy(filename, argv[1], LEN - 1);
        filename[LEN - 1] = '\0';
    } else {
        printf("请输入保存中奖名单的文件名(直接回车则使用系统日期): ");
        if (fgets(filename, LEN, stdin) == NULL)
            filename[0] = '\0';

        filename[strcspn(filename, "\r\n")] = '\0';
        if (filename[0] == '\0')
            make_date_filename(filename, LEN);
    }

    srand((unsigned int)time(NULL) ^ (unsigned int)clock());
    pick_students(selected, n, PICK);

    for (i = 0; i < PICK; i++)
        winners[i] = students[selected[i]];

    sort_by_id(winners, PICK);

    fout = fopen(filename, "w");
    if (fout == NULL) {
        printf("fail to open file\n");
        return 1;
    }

    printf("中奖名单如下:\n");
    output_students(stdout, winners, PICK);

    output_students(fout, winners, PICK);
    fclose(fout);

    printf("中奖名单已写入文件: %s\n", filename);

    return 0;
}

int read_students(Student st[], int maxn) {
    int n = 0;
    FILE *fin;

    fin = fopen("list.txt", "r");
    if (fin == NULL) {
        printf("fail to open file\n");
        return 0;
    }

    while (n < maxn &&
           fscanf(fin, "%ld %31s %63s", &st[n].id, st[n].name,
                  st[n].class_name) == 3) {
        ++n;
    }

    fclose(fin);
    return n;
}

void make_date_filename(char filename[], int size) {
    time_t now;
    struct tm *local;

    now = time(NULL);
    local = localtime(&now);
    if (local == NULL || strftime(filename, (size_t)size, "%Y%m%d.txt", local) == 0)
        strncpy(filename, "winners.txt", (size_t)size);

    filename[size - 1] = '\0';
}

void pick_students(int selected[], int n, int count) {
    int flag[N] = {0};
    int i = 0;
    int index;

    while (i < count) {
        index = rand() % n;
        if (!flag[index]) {
            flag[index] = 1;
            selected[i] = index;
            ++i;
        }
    }
}

void sort_by_id(Student st[], int n) {
    int i, j;
    Student temp;

    for (i = 0; i < n - 1; i++) {
        for (j = 0; j < n - 1 - i; j++) {
            if (st[j].id > st[j + 1].id) {
                temp = st[j];
                st[j] = st[j + 1];
                st[j + 1] = temp;
            }
        }
    }
}

void output_students(FILE *fp, Student st[], int n) {
    int i;

    fprintf(fp, "学号\t\t姓名\t班级\n");
    for (i = 0; i < n; i++)
        fprintf(fp, "%ld\t%s\t%s\n", st[i].id, st[i].name, st[i].class_name);
}

  结果

d

 

posted @ 2026-06-23 18:11  飞行雪绒  阅读(6)  评论(0)    收藏  举报