实验7

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

#define N 10
typedef struct student {
    int num;
    char name[20];
    int score;
}STU;

int main() {
    STU st, stmax, stmin;
    int i;
    FILE *fp;
    
    fp = fopen("file1.dat", "r");
    if( !fp ) {  
        printf("fail to open file1.dat\n");
        exit(0);
    }
    
    stmax.score = 0;    
    stmin.score = 100;
    while( !feof(fp) ) {//修改处,源代码为:for(i=0; i<N; i++)
        fscanf(fp, "%d %s %d", &st.num, st.name, &st.score);  
        if(st.score > stmax.score)
            stmax = st;
        else if(st.score < stmin.score)
            stmin = st; 
    } 
    
    fclose(fp);
    
    printf("最高分学生信息: %5d%15s%5d\n", stmax.num, stmax.name, stmax.score);
    printf("最低分学生信息: %5d%15s%5d\n", stmin.num, stmin.name, stmin.score);

    return 0;
}

 

#include <stdio.h>
#include <stdlib.h>
 
#define N 10
 
 
typedef struct student {
    int num;
    char name[20];
    int score;
}STU;
 
void sort(STU *pst, int n);
 
int main() {
    FILE *fin, *fout;
    STU st[N];
    int i;
    fin = fopen("file1.dat", "r");
    if( !fin ) { 
        printf("fail to open file1.dat\n");
        exit(0);
    }
    for(i=0; i<N; i++)
        fscanf(fin, "%d %s %d", &st[i].num, st[i].name, &st[i].score);
    fclose(fin);
    sort(st, N);
    fout = fopen("file3.dat", "w");
    if( !fout ) { 
        printf("fail to open file1.dat\n");
        exit(0);
    }
    for(i=0; i<N; i++) {
        printf("%-6d%-10s%3d\n", st[i].num, st[i].name, st[i].score);
        fprintf(fout, "%-6d%-10s%3d\n", st[i].num, st[i].name, st[i].score);
    }   
    fclose(fout); 
    return 0;
}
 
void sort(STU *pst, int n) {
    STU *pi, *pj, t;
    for(pi = pst; pi < pst+n-1; pi++)
        for(pj = pi+1; pj < pst+n; pj++)
            if(pi->score < pj->score) {
                t = *pi;
                *pi = *pj;
                *pj = t;
            }
     
}

 

#include <stdio.h> 
#include <stdlib.h>
#define N 10


typedef struct student {
    int num;
    char name[20];
    int score;
}STU;

void sort(STU *pst, int n); 

int main() {
    FILE *fin, *fout;
    STU st[N];
    int i;
    fin = fopen("file1.dat", "r");
    if( !fin ) {  
        printf("fail to open file1.dat\n");
        exit(0);
    }
    for(i=0; i<N; i++) 
        fscanf(fin, "%d %s %d", &st[i].num, st[i].name, &st[i].score);
    fclose(fin); 
    sort(st, N);
    fout = fopen("file3.dat", "wb");
    if( !fout ) {  
        printf("fail to open file1.dat\n");
        exit(0);
    }
    for(i=0; i<N; i++) {
        printf("%-6d%-10s%3d\n", st[i].num, st[i].name, st[i].score);
        fprintf(fout, "%-6d%-10s%3d\n", st[i].num, st[i].name, st[i].score);
    }    
    fclose(fout);  
    return 0;
}

void sort(STU *pst, int n) {
    STU *pi, *pj, t;
    for(pi = pst; pi < pst+n-1; pi++)
        for(pj = pi+1; pj < pst+n; pj++) 
            if(pi->score < pj->score) {
                t = *pi;
                *pi = *pj;
                *pj = t; 
            }
    
}

 

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
const int N = 10;

// 定义结构体类型struct student,并定义其别名为STU 
typedef struct student {
    long int id;
    char name[20];
    float objective;    /*客观题得分*/
    float subjective; /*操作题得分*/
    float sum;
    char level[10];    
}STU; 

// 函数声明
void input(STU s[], int n);
void output(STU s[], int n);
void process(STU s[], int n);

int main() {
    STU stu[N];
    
    printf("录入%d个考生信息: 准考证号,姓名,客观题得分(<=40),操作题得分(<=60)\n", N); 
    input(stu, N);
    
    printf("\n对考生信息进行处理: 计算总分,确定等级\n");
    process(stu, N);
    
    printf("\n打印考生完整信息: 准考证号,姓名,客观题得分,操作题得分,总分,等级\n");
    output(stu, N); 
    
    return 0;
} 

// 录入考生信息:准考证号,姓名,客观题得分,操作题得分
void input(STU s[], int n) {
    int i;
    FILE *fin;
    fin=fopen("examinee.txt","r");//只读方式打开"examinee.txt"
    if(fin==NULL){//若打开失败,则显示提示信息,然后退出程序
        printf("fail to open examinee.txt\n");
        exit(0);
    }
    for(i=0;i<n;i++){
    fscanf(fin,"%d %s %f %f",&s[i].id,s[i].name,&s[i].objective,&s[i].subjective);
    }//从原文件读取数据
    fclose(fin);
}

//输出考生完整信息: 准考证号,姓名,客观题得分,操作题得分,总分,等级
void output(STU s[], int n) {
    int i;
    FILE *fout;
    fout=fopen("result.txt","w");// 以只写文本方式打开/创建文件"result.txt"
    for(i=0;i<n;i++){
    printf("%5d %10s %5.1f %5.1f %5.1f %10s\n",s[i].id,s[i].name,s[i].objective,s[i].subjective,s[i].sum,s[i].level);//在屏幕上输出结果
    fprintf(fout,"%5d %10s %5.1f %5.1f %5.1f %10s\n",s[i].id,s[i].name,s[i].objective,s[i].subjective,s[i].sum,s[i].level);//在文件里输出结果
    }
    fclose(fout);
}

void process(STU s[], int n) {
    int i,j,k,m=0;
    STU temp;
    for(i=0;i<n;i++)
        s[i].sum=s[i].objective+s[i].subjective;
    for(j=0;j<n;j++){
        for(k=j;k<n;k++){
            if(s[j].sum<s[k].sum){
                temp=s[j];
                s[j]=s[k];
                s[k]=temp;
            }
        }
    }//对学生总分进行排序
    while(m<=n){
        if(m<=n*0.1-1)
        strcpy(s[m].level,"优秀");
        else if(m>n*0.1-1&&m<=n*0.5-1)
        strcpy(s[m].level,"合格");
        else
        strcpy(s[m].level,"不合格"); 
        m++;
    };//评定等级
}

 

posted @ 2019-06-24 22:57  Soledad·QX  阅读(83)  评论(0编辑  收藏  举报