实验7


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
const int N = 10;
 
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,FILE *fp);
void output(STU s[], int n,FILE *fout);
void process(STU s[], int n);

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

// 将file1.txt中小写字母转换成大写后,另存为file2.txt 
#include <stdio.h>
#include <stdlib.h>

int main() {
    FILE *fin, *fout; // 定义文件类型指针
    int ch;
    
    fin = fopen("file1.txt", "r"); // 以只读文本方式打开文件file1.txt
    if (fin == NULL) {
        printf("fail to open file1.txt\n");
        exit(0);    
    } 
    
    fout = fopen("file2.txt", "w"); // 以写文本方式打开文件file2.txt, 如果文件不存在,就创建一个
    if (fout == NULL) {
        printf("fail to open or create file2.txt\n");
        exit(0);
    } 
    
    while( !feof(fin) ) {
        ch = fgetc(fin);  // 从fin指向的文件file1.txt中读取单个字符,暂存在字符变量ch中 
        
        if(ch >= 'a' && ch <= 'z')  // 如果是小写字母,则转换成大写 
            ch -= 32;
        
        fputc(ch, fout); // 将字符变量ch中的字符写入fout指向的文件file2.txt中 
    }
    
    fclose(fin);    
    fclose(fout);
    
    return 0;
}

当前路径上无file3

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

#define N 10

// 定义一个结构体类型STU 
typedef struct student {
    int num;
    char name[20];
    int score;
}STU;

int main() {
    STU st, stmax, stmin;
    int i;
    FILE *fp;
    
    // 以只读文本方式打开文件file1.dat 
    fp = fopen("file1.dat", "r");
    if( !fp ) {  // 如果打开失败,则输出错误提示信息,然后退出程序 
        printf("fail to open file1.dat\n");
        exit(0);
    }
    
    stmax.score = 0;    // 先假定最高分是0,后面如发现比当前最高分还高的分数,就更新最高分 
    stmin.score = 100;    // 先假定最低分是100分,后面如发现比当前最低分更低的分数,就更新最低分 
    
    while(!feof(fp)) {
        fscanf(fp, "%d %s %d", &st.num, st.name, &st.score);  // 从fp指定的文件中格式化读取一个学生信息
        
        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

// 定义一个结构体类型STU 
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;
    
    // 以只读文本方式打开文件file1.dat 
    fin = fopen("file1.dat", "r");
    if( !fin ) {  // 如果打开失败,则输出错误提示信息,然后退出程序 
        printf("fail to open file1.dat\n");
        exit(0);
    }
    
    // 从fin指向的数据文件file1.dat中读取数据到结构体数组st
    for(i=0; i<N; i++) 
        fscanf(fin, "%d %s %d", &st[i].num, st[i].name, &st[i].score);
    
    fclose(fin);  // 关闭fin指向的文件file1.dat
    
    // 调用函数sort()对数组st中数据,按分数由高到低排序 
    sort(st, N);
    
    // 以写方式打开/创建文本文件file3.dat
    fout = fopen("file3.dat", "w");
    if( !fout ) {  // 如果打开失败,则输出错误提示信息,然后退出程序 
        printf("fail to open file1.dat\n");
        exit(0);
    }
    
    // 将排序后的数组st中数据输出到屏幕,同时,也写入文件file3.dat
    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);  // 关闭fout指向的文件file3.dat
    
    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("file4.dat","rb");
        if(!fin){
            printf("fail to open file4.dat\n");
            exit(0);
        }
    for(i=0; i<N; i++) 
        fread(&st[i],sizeof(st[i]),1,fin);
    
    sort(st, N);
    
    
    for(i=0; i<N; i++) 
        printf("%-6d%-10s%3d\n", st[i].num, st[i].name, st[i].score);
    
        
    fclose(fin);  
    
    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; 
            }
} 

 

posted @ 2020-12-29 09:19  赖梓贤  阅读(75)  评论(0编辑  收藏  举报