实验七

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

int main() {
    FILE *fin, *fout; 
    int ch;
    
    fin = fopen("file1.txt", "r"); 
    if (fin == NULL) {
        printf("fail to open file1.txt\n");
        exit(0);    
    } 
    
    fout = fopen("d:\\file3.txt", "w"); 
    if (fout == NULL) {
        printf("fail to open or create file2.txt\n");
        exit(0);
    } 
    
    while( !feof(fin) ) {
        ch = fgetc(fin);  
        
        if(ch >= 'a' && ch <= 'z') 
            ch -= 32;
        
        fputc(ch, fout); 
    }
    
    fclose(fin);    
    fclose(fout);
#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)) {
        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("file4.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);
    

    fwrite(st, sizeof(STU), N, fout);  
        
    fclose(fout);  
    
    fout=fopen("file4.dat","r");
    
    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>
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;
} 

// 从文本文件examinee.txt读入考生信息:准考证号,姓名,客观题得分,操作题得分
void input(STU s[], int n) {
    int i;
    FILE *fin;
    fin = fopen("examinee.txt", "r");
    for(i=0; i<n; i++)
    fscanf(fin,"%ld %s %f %f",&s[i].id,s[i].name,&s[i].objective,&s[i].subjective);
}
void output(STU s[], int n) {
    int i;
    printf("准考证号   姓名  客观题得分   操作题得分   总分       等级\n "); 
    FILE *fp;
    fp = fopen("result.txt","w");
    for (i=0;i<n;i++)
        printf("%4ld    %6s     %5.2f        %.2f     %6.2f     %4s\n\n",s[i].id,s[i].name,s[i].objective,s[i].subjective,s[i].sum,s[i].level);
   
}

// 对考生信息进行处理:计算总分,排序,确定等级
void process(STU s[], int n) {
  int i,j;
    STU t;
    for(i=0;i<n;i++)
s[i].sum=s[i].objective+s[i].subjective;
    for(i=0;i<n-1;i++){
        for(j=0;j<n-1-i;j++)
           if(s[j].sum<s[j+1].sum){
               t=s[j];
               s[j]=s[j+1];
               s[j+1]=t;
           }
    }
    for(i=0;i<n;i++){
    if(i<1)
    strcpy(s[i].level,"优秀");
    
    else if(i>=1&&i<=5)
    strcpy(s[i].level,"合格");
    
    else
    strcpy(s[i].level,"不合格");
    
    }
} 

实验七

return 0;
}

实验七

上一篇:彻彻底底地理解TCP三次握手和四次挥手的全部过程


下一篇:下载项目下的文件