实验七

task4

源代码:

#include<stdio.h>
#define N 10
#define M 80
void count_line(int *ptr1);
void count_num(int *ptr2);


int main(){
    int a,b;
    count_line(&a);
    count_num(&b);
    
    printf("行数为: %d \n",a);
    printf("字符数为:%d\n",b);
    return 0;
} 
void count_line(int *ptr1){
    char ch;
    int count=0;
    FILE *fp;
    
    fp=fopen("C:\\Users\\31338\\Downloads\\实验7数据文件及部分代码\\实验7数据文件及部分代码\\data4.txt","r");
    
    if(!fp){
        printf("fail to open");
    }
    while(!feof(fp)){
        ch=getc(fp); 
        if(ch=='\n'){
            count++;
        }
    }
    fclose(fp);
    *ptr1=count+1;
}

void count_num(int *ptr2){
    char ch;
    int count=0,number;
    FILE *fp;
    
    fp=fopen("C:\\Users\\31338\\Downloads\\实验7数据文件及部分代码\\实验7数据文件及部分代码\\data4.txt","r");
    
    if(!fp){
        printf("fail to open");
    }
     while(!feof(fp)) {
        ch = fgetc(fp);
        if(ch == EOF){
             break;
        }
        if(ch!='\n'&&ch!=' ')
          count++;
    }

    fclose(fp);
    *ptr2=count;
}

运行截图:

task5

源代码:

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

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

// 把通过考试的考生完整信息写入文件list_pass.txt
// 准考证号,姓名,客观题得分,操作题得分,总分,结果
void write(STU st[], int n) {
    int i;
    FILE *fout;
    
    fout=fopen("list_pass.txt","w");
    if(!fout){
        printf("fail to open file\n");
        return;    
    }
        fprintf(fout,"准考证号\t\t姓名\t\t客观题得分\t\t操作题得分\t\t总分\t\t结果\n");
        for (i = 0; i < n; i++)
           if(strcmp(st[i].result,"通过")==0){
            fprintf(fout,"%-20ld%-20s%-20.2f%-20.2f%-20.2f%-20s\n", st[i].id, st[i].name, st[i].objective, st[i].subjective, st[i].sum, st[i].result);
}
        
    fclose(fout);
}

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

运行截图:

task6

源代码:

#include<stdio.h>
#define N 80
typedef struct{
    char num[10];
    char name[20];
    char clas[20];
    int order;
}STU;

int main(){
    int i,number,t,count=0,n;
    char filename[N];
    FILE *fp1,*fp2;
    STU x[N],h[N];
    fp1=fopen("C:\\Users\\31338\\Downloads\\实验7数据文件及部分代码\\实验7数据文件及部分代码\\list.txt","r");
    if(!fp1){
        printf("fail to open");
        return;
    }
    n=sizeof(x)/sizeof(x[0]);
    for(i=0;i<n;i++){
        t=fscanf(fp1,"%s%s%s\n",x[i].num ,x[i].name ,x[i].clas  );
        if(t!=3){
            break;
        }
        x[i].order =i+1;
    }
    
    while(count!=5){
    number=1+rand()%80;
       for(i=0;i<N;i++){
           if(number==x[i].order&&x[i].order !=0 ){
               printf("%-20s%-10s%-10s\n",x[i].num ,x[i].name ,x[i].clas );
               h[count++]=x[i];
               x[i].order =0;
           }
       }
}
    printf("输入文件名:");
    scanf("%s",filename); 
    fp2=fopen(filename,"w");
    if(!fp2){
        printf("创建失败");
        return; 
    }
    printf("创建成功");
    for(i=0;i<5;i++){
        fprintf(fp2,"%-20s%-10s%-10s\n",h[i].num ,h[i].name ,h[i].clas);
    }
    return 0;
}

运行截图:

 

posted @ 2024-12-28 22:19  danny555  阅读(14)  评论(0)    收藏  举报