实验报告7

实验4

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

signed main() {
    FILE *fp;
    fp=fopen("data4.txt","r");
    if(fp==NULL){
        printf("fail to open file to read\n");
        return 0;
    }
    int n=0,nl=0;
    char c=fgetc(fp);
    while(c!=EOF){
        // putchar(c);
        if(c=='\n'){
            nl++;
        }else{
            if(c!=' '&&c!=EOF){
                n++;
            }
        }
        c=fgetc(fp);
    }
    fclose(fp);
    // printf("\n");
    printf("行数:%d\n",nl+1);
    printf("字符数(不计空白字符):%d\n",n);
    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, 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);
}

// 从文本文件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);

    fclose(fin);
}

// 把通过考试的考生完整信息写入文件list_pass.txt
// 准考证号,姓名,客观题得分,操作题得分,总分,结果
void write(STU s[], int n) {
    // 待补足
    // xxx
    FILE *fout;
    fout=fopen("list_pass.txt","w");
    if(fout==NULL){
        printf("fail to open file to write\n");
        return;
    }
    fprintf(fout,"准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n");
    for(int i=0;i<n;i++){
        if(strcmp(s[i].result,"通过")==0){
            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);
}

// 对考生信息进行处理:计算每位考生考试总分、结果;统计考试通过的人数
int process(STU st[], int n, STU st_pass[]) {
    // 待补足
    // xxx
    FILE *f;
    int count=0;
    f=fopen("examinee.txt","r");
    if(f==NULL){
        printf("fail to open file\n");
        return 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,"通过");
            count++;
        }
        else{
            strcpy(st[i].result,"不通过");
        }
    }
    fclose(f);
    return count;
}

image

image

实验6

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
struct Stu{
    long id;
    char name[100];
    char class[100];
}a[114514],b[114514];
int h[114514];
signed main() {
    srand((unsigned int)time(NULL));
    time_t rawtime;
    struct tm * timeinfo;
    time(&rawtime);
    timeinfo = localtime(&rawtime);
    char str[20];
    strftime(str, sizeof(str),"%Y%m%d.txt",timeinfo);
    FILE *fin, *fout;
    fout=fopen(str,"w");
    fin=fopen("list.txt","r");
    int i=0;
    while(fscanf(fin,"%ld %s %s",&a[i].id,a[i].name,a[i].class)!=EOF){
        i++;
    }
    int n=i;
    for(int j=1;j<=5;){
        int t=rand()%n;
        while(!h[t]){
            h[t]=1;
            b[j++]=a[t];
        }
        t=rand()%n;
    }
    for(int j=1;j<=5;j++){
        for(int k=j+1;k<=5;k++){
            if(b[j].id>b[k].id){
                struct Stu t=b[j];
                b[j]=b[k];
                b[k]=t;
            }
        }
    }
    fclose(fin);
    printf("----------%s中奖名单----------\n",str);
    printf("准考证号\t姓名\t班级\n");
    for(int j=1;j<=5;j++){
        printf("%ld\t%s\t%s\n",b[j].id,b[j].name,b[j].class);
    }
   
    fprintf(fout,"----------%s中奖名单----------\n",str);
    fprintf(fout,"准考证号\t姓名\t班级\n");
    for(int j=1;j<=5;j++){
        fprintf(fout,"%ld\t%s\t%s\n",b[j].id,b[j].name,b[j].class);
    }
    fclose(fout);

    printf("\n文件保存成功!\n");
    return 0;
}

image

image

posted @ 2025-12-25 19:29  Carrotz  阅读(7)  评论(0)    收藏  举报