实验7

实验任务4
#include <stdio.h>
#include <stdlib.h>

int main() {
    FILE *fp;
    char ch;
    int line_count = 0;   // 统计行数 
    int char_count = 0;   // 统计非空白字符数 

    // 以只读方式打开文件data4.txt 
    fp = fopen("data4.txt", "r");
    if (fp == NULL) {
        printf("fail to open data4.txt\n");
        return 1;
    }

    // 逐字符读取文件直至末尾 [cite: 455, 456]
    while ((ch = fgetc(fp)) != EOF) {
        // 判断是否为行末换行符
        if (ch == '\n') {
            line_count++; 
        }
        
        // 判断是否为非空白符:不包括空格(' ')、回车('\n')、水平制表符('\t') 
        if (ch != ' ' && ch != '\n' && ch != '\t' && ch != '\r') {
            char_count++; 
        }
    }

    // 假设最后一行如果没有以换行符结束,也计入行数
    // 根据示例预期结果,此处需结合具体文件结尾情况判断
    // 如果文件非空且最后一个字符不是换行符,行数加1
    /* fseek(fp, -1, SEEK_END);
    if(fgetc(fp) != '\n') line_count++; 
    */

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

    fclose(fp); [cite: 41]
    
    return 0;
}

82cf1442bda2d616683435eee75b464c

实验任务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;
}

fd35af40c32e1b55c5f424eadd59ef1d

实验任务6

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

int main(){
    char s[N][N];
    char filename[80];
    char hit[N][N];
    FILE *fp,*fout;
    int has_hit[N] = {0};
    int i,n;
    int lucky_i;

    fp = fopen("C:\\Users\\jr787\\Desktop\\实验7数据文件及部分代码_gbk\\list.txt","r");
    if(!fp){
        perror("list.txt");
        return 1;
    }

    i = 0;
    while(i < N && (fgets(s[i],N,fp)!= NULL)){
        printf("%s",s[i]);
        ++i;
    }
    n = i;

    printf("\n------------中奖名单------------\n");
    srand(time(0));

    for(i = 0;i < M;i++){
        lucky_i = rand () % N ;

        if(has_hit[lucky_i])
            continue;

        has_hit[lucky_i] = 1;
        strcpy(hit[i],s[lucky_i]);
        printf("%s",s[lucky_i]);
    }

    printf("\n------------保存到文件------------\n");
    printf("输入文件名:") ;
    gets(filename);
    fout = fopen(filename,"w");
    if(!fout){
        perror(filename);
        return 1;
    }

    for(i =0;i < M;++i)
        fprintf(fout,"%s\n",hit[i]);

    printf("保存成功!");

    fclose(fout);
    fclose(fp);
    return 0;

}

84e3b3230a1811c3227345cbaf8132bf

fa7ea94d03c5b9e97ae2e60029bd4cf1

 

posted @ 2025-12-31 18:06  F_avor  阅读(2)  评论(1)    收藏  举报