实验七

任务四

源代码:

#include<stdio.h>
int main() {
    FILE* fp;
    fp = fopen("data4.txt", "r");
    if(fp == NULL) {
        perror("Error opening file");
        return 1;
    }
    char ch;
    int a = 1, b = 0;
    while ((ch = fgetc(fp)) != EOF) {
            if (ch == '\n') {
             a++;
        }
        if (ch != ' '&&ch!='\n'&&ch!='\t')
        {
            b++;
        }
    }
   fclose(fp);
   printf("data4.txt统计结果:\n行数:%d\n字符数(不计空白符):%d", a, b);
   }

运行结果:

 

 

任务五

源代码:

#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, N);  
    write(stu, N);   

    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;
    }

    while (!feof(fin)) {
        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 st[], int n) {
    int i;
    float sum;
    FILE *fout = fopen("list_pass.txt","w");
    if(fout == NULL) {
        perror("Error opening file");
        return;
    }
    for (i = 0; i < n; i++){
        sum = st[i].objective + st[i].subjective;
        char result[10];
        if (sum >= 60) {
            sprintf(result, "Pass");
        }
        else {
            sprintf(result, "Fail");
        }
        fprintf(fout, "%ld %s %.2f %.2f %.2f %s\n", st[i].id, st[i].name, st[i].objective, 
                                                    st[i].subjective, sum, result);
        }
        fclose(fout);
    }

int process(STU st[], int n, STU st_pass[]) {
    int pass_count = 0;
    int i;
    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[pass_count] = st[i];
            pass_count++;
        }
        else {
            strcpy(st[i].result, "不合格");
        }
    }
    return pass_count;
}

运行结果:

 

 

任务六

源代码:

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

void readStudentList(char students[][200], int *count) {
    FILE *fp;
    fp = fopen("list.txt", "r");
    if (fp == NULL) {
        printf("无法打开文件\n");
        return;
    }
    *count = 0;
    while (fgets(students[*count], 200, fp)!= NULL) {
        (*count)++;
    }
    fclose(fp);
}

void randomSelectStudents(char students[][200], char selected[][200], int count) {
    int i;
    int selectedCount = 0;
    srand((unsigned int)time(NULL));
    while (selectedCount < 5) {
        int index = rand() % count;
        int found = 0;
        for (i = 0; i < selectedCount; i++) {
            if (strcmp(students[index], selected[i]) == 0) {
                found = 1;
                break;
            }
        }
        if (!found) {
            strcpy(selected[selectedCount], students[index]);
            selectedCount++;
        }
    }
}

void displayAndWrite(char selected[][200]) {
    int i;
    printf("----------随机抽点名单----------\n");
    for (i = 0; i < 5; i++) {
        printf("%s", selected[i]);
    }
    char fileName[100];
    printf("----------保存到文件----------\n"); 
    printf("请输入文件名:");
    scanf("%s", fileName);
    FILE *fp = fopen(fileName, "w");
    if (fp == NULL) {
        printf("无法创建文件\n");
        return;
    }
    for (i = 0; i < 5; i++) {
        fputs(selected[i], fp);
    }
    fclose(fp);
    printf("保存成功!\n");
}

int main() {
    char students[80][200];
    char selected[5][200];
    int count;
    readStudentList(students, &count);
    randomSelectStudents(students, selected, count);
    displayAndWrite(selected);
    return 0;
}

运行结果:

 

posted @ 2024-12-30 01:00  林子龙  阅读(3)  评论(0编辑  收藏  举报