实验7 文件应用编程

4. 实验任务4:文件简单应用

#include<stdio.h>
int main()
{
    FILE *fp;
    char ch;
    int line = 0,num = 0;
    fp = fopen("C:\\Users\\ASUS\\Desktop\\data4.txt","r");
    if(!fp)
    {
        printf("fail");
        return 1;
    }
    while((ch = fgetc(fp))!=EOF)
    {
        if(ch == '\n')
        line++;
        if(ch!=' '&&ch!='    '&&ch!='\n')
        num++;
    }
    line++;
    fclose(fp);
    printf("data4.txt统计结果\n"); 
    printf("行数:%d\n",line); 
    printf("字符数(不包括空白符):%d",num);
    return 0;
}

5. 实验任务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, 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);
    }
}


// 从文本文件 examinee.txt 读入考生信息:准考证号,姓名,客观题得分,操作题得分
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);
        // 计算总分
        st[i].sum = st[i].objective + st[i].subjective;
    }
    fclose(fin);
}


// 把通过考试的考生完整信息写入文件 list_pass.txt
// 准考证号,姓名,客观题得分,操作题得分,总分,结果
void write (STU s[],int n)
{
    int i = 0;
    FILE *fto;
    fto = fopen("C:\\Users\\ASUS\\Desktop\\list.txt","w");
    if(!fto)
    {
      printf("fail");
        return;
    }
    for(;i<n;++i)
    {
    if(s[i].sum>=60)
      {
        fprintf(fto,"%ld %s %f %f", &s[i].id, s[i].name, &s[i].objective, &s[i].subjective);
      }
    }
    fclose(fto);
}


// 对考生信息进行处理:计算每位考生考试总分、结果;统计考试通过的人数
int process(STU st[],int n,STU st_pass[])
{
int i = 0,count = 0;
for(;i<n;++i)
{
    st[i].sum = st[i].objective+st[i].subjective;
    if(st[i].sum>=60)
  {
    strcpy(st[i].result,"通过");
    st_pass[i].id = st[i].id;
    strcpy(st_pass[i].name,st[i].name);
    st_pass[i].objective = st[i].objective;
    st_pass[i].subjective = st[i].subjective;
    st_pass[i].sum = st[i].sum;
    strcpy(st_pass[i].result,st[i].result);
    count++;
  }
    else
        strcpy(st[i].result,"未通过");
}
    return count;
}

6. 实验任务6:文件综合应用

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define M 100
#define N 100


int main() {
    char students[M][N];
    int num_students = 0;
    FILE *fp;
    char filename[100];
    int s[5] = {-1};
    int i, j, ra;


    // 打开文件读取学生信息
    fp = fopen("C:\\Users\\ASUS\\Desktop\\list.txt", "r");
    if (!fp) {
        printf("fail");
        return 0;
    }


    // 读取文件中的学生信息
    while (fgets(students[num_students], N, fp)!= NULL) {
        num_students++;
    }
    fclose(fp);


    srand(time(NULL));
    if (num_students < 5) {
        printf("文件中的学生信息少于 5 条,无法进行随机选择。\n");
        return 0;
    }


    for (i = 0; i < 5; i++) {
        do {
            ra = rand() % num_students;
        } while (ra == s[0] || ra == s[1] || ra == s[2] || ra == s[3] || ra == s[4]);
        s[i] = ra;
    }


    for (i = 0; i < 5; i++) {
        printf("%s", students[s[i]]);
    }


    printf("请输入文件名:");
    scanf("%s",filename);


    fp = fopen(filename, "w");
    if (!fp) {
        printf("fail");
        return 0;
    }


    for (i = 0; i < 5; i++) {
        fputs(students[s[i]], fp);
    }


    fclose(fp);


    return 0;
}

 

posted @ 2024-12-30 19:44  杨玉鹏  阅读(13)  评论(0)    收藏  举报