实验7

实验任务4

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

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

    fp = fopen("data4.txt", "r");
    if (fp == NULL) {
        printf("无法打开文件!\n");
        return 1;
    }

    while ((ch = fgetc(fp)) != EOF) {
        if (ch == '\n') {
            line_count++;
        }
        if (!isspace(ch)) {
            char_count++;
        }
    }
    if (line_count > 0 || char_count > 0) {
        line_count++;
    }

    printf("data4.txt统计结果:\n");
    printf("行数:\t\t%d\n", line_count);
    printf("字符数(不计空白符):\t%d\n", char_count);

    fclose(fp);
    return 0;
}

image

实验任务5

#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() {
    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_pass, cnt);
    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 %s %f %f", &st[i].id, st[i].name, &st[i].objective, &st[i].subjective);

    fclose(fin);
}

void write(STU s[], int n) {
    FILE *fout = fopen("list_pass.txt", "w");
    if (!fout) {
        printf("文件list_pass.txt打开失败\n");
        return;
    }
    fprintf(fout, "准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n");
    for (int i = 0; i < n; i++) {
        fprintf(fout, "%ld\t\t%s\t%.2f\t\t%.2f\t\t%.2f\t\t%s\n",
                s[i].id, s[i].name, s[i].objective,
                s[i].subjective, s[i].sum, s[i].result);
    }
    fclose(fout);
    printf("通过考生信息已写入list_pass.txt\n");
}

int process(STU st[], int n, STU st_pass[]) {
    int pass_cnt = 0;
    for (int 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[pass_cnt] = st[i];
            pass_cnt++;
        } else {
            strcpy(st[i].result, "未通过");
        }
    }
    return pass_cnt;
}

image

实验任务6

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

#define STUDENT_NUM 80
#define WIN_NUM 5

typedef struct {
    char id[20];
    char name[20];
    char cls[30];
} Student;

void readStudents(Student stu[], int num) {
    FILE *fp = fopen("list.txt", "r");
    if (fp == NULL) {
        printf("无法打开list.txt文件!\n");
        exit(1);
    }
    for (int i = 0; i < num; i++) {
        fscanf(fp, "%s %s %s", stu[i].id, stu[i].name, stu[i].cls);
    }
    fclose(fp);
}

int main() {
    Student allStudents[STUDENT_NUM];
    Student winners[WIN_NUM];
    int flag[STUDENT_NUM] = {0};

    readStudents(allStudents, STUDENT_NUM);
    srand((unsigned int)time(NULL));

    int winCount = 0;
    while (winCount < WIN_NUM) {
        int randIdx = rand() % STUDENT_NUM;
        if (flag[randIdx] == 0) {
            winners[winCount] = allStudents[randIdx];
            flag[randIdx] = 1;
            winCount++;
        }
    }

    printf("---中奖名单---\n");
    for (int i = 0; i < WIN_NUM; i++) {
        printf("%s\t%s\t%s\n", winners[i].id, winners[i].name, winners[i].cls);
    }

    char fileName[50];
    printf("---保存到文件---\n输入文件名:");
    scanf("%s", fileName);

    FILE *fpOut = fopen(fileName, "w");
    if (fpOut == NULL) {
        printf("文件打开失败!\n");
        return 1;
    }
    for (int i = 0; i < WIN_NUM; i++) {
        fprintf(fpOut, "%s\t%s\t%s\n", winners[i].id, winners[i].name, winners[i].cls);
    }
    fclose(fpOut);
    printf("保存成功!\n");

    return 0;
}

image

posted @ 2025-12-30 22:12  屈开辰  阅读(1)  评论(0)    收藏  举报