学生成绩管理系统C语言版

2018-02-02  本文已影响0人  奋斗live
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAX 10000
void add_student();
void edit_student();
void del_student();
void select_student_by_name();
void select_student_by_score();
void query_student();
void print_student();
void IO_write();
void IO_read();
void config_write();
void config_read();
//返回此学生学号对应的索引
int verify_sno();
typedef struct stu{
    char sno[40];//10大小
    char name[40];//20大小
    float math_score;//4
    float englist_score;//4
    long age;
    float avg_score;
}Stu;
char is_continue[0];
char sno[10];char  name[20];
float math_score,englist_score,avg_score;
int num=0;//学生数量
int res;
FILE * fp;
int main(){
    int choose_num;//选择方式
    Stu * student;
    student = (Stu *)malloc(10*(sizeof(Stu)));
    if(student==NULL){
        printf("动态分配内存失败\n");
    }
    if(num==0){//刚启动
        config_read();
    }
    while(1){
        printf("\n------ 学生成绩管理系统------\n");  
        printf("1. 增加学生记录\n");  
        printf("2. 修改学生记录\n");  
        printf("3. 删除学生记录\n");  
        printf("4. 显示所有学生信息\n");  
        printf("5. 按姓名查询学生记录\n");  
        printf("6. 按平均成绩排序\n");  
        printf("7. 退出\n");  
        printf("请选择(1-7):");
        scanf("%d",&choose_num);
        switch(choose_num){
            case 1://增加学生
                add_student(student);
                break;
            case 2://修改学生
                edit_student(student);
                break;
            case 3://删除学生记录
                del_student(student);
                break;
            case 4://显示所有学生信息
                query_student(student);
                break;
            case 5://按照学生姓名查询学生记录
                select_student_by_name(student);
                break;
            case 6://按照平均成绩查询学生记录
                select_student_by_score(student);
                break;
            default:
                exit(0);
                break;
        }
    }
}

void add_student(Stu * student){
    while(1){
        printf("请输入学生学号:");
        scanf("%s",sno);
        res = verify_sno(student);
        if(res != -1){
            printf("已经具有这个学号的学生了\n");
            printf("请重新输入\n");
            continue;
        }
        printf("请输入学生姓名:");
        scanf("%s",name);
        printf("请输入数学成绩:");
        scanf("%f",&math_score);
        printf("请输入英语成绩:");
        scanf("%f",&englist_score);
        memcpy(student[num].sno,sno,50);
        memcpy(student[num].name,name,50);
        student[num].math_score = math_score;
        student[num].englist_score = englist_score;
        student[num].avg_score = (math_score+englist_score)/2;
        num++;
        
        IO_write(student);
        printf("是否继续,请输入Y/N\n");
        scanf("%s",is_continue);
        getchar();//清除缓冲区中的欢迎符号
        if(strcmp(is_continue,"N")==0){
            break;
        }
    }
    
}

void query_student(Stu * student){
    while(1){
        IO_read(student);
        print_student(student);
        printf("是否继续,请输入Y/N\n");
        scanf("%s",is_continue);
        getchar();//清除缓冲区中的欢迎符号
        if(strcmp(is_continue,"N")==0){
            break;
        }
    }
}

void edit_student(Stu * student){
    while(1){
        int index;
        printf("输入要修改的学生的编号\n");
        scanf("%d",&index);
        if(index<1 || index>num){
            printf("输入的编号不存在\n");
            break;
        }
        printf("请输入学生学号:");
        scanf("%s",sno);
        res = verify_sno(student);
        if(res != -1){
            printf("已经具有这个学号的学生了\n");
            printf("请重新输入\n");
            continue;
        }
        printf("请输入学生姓名:");
        scanf("%s",name);
        printf("请输入数学成绩:");
        scanf("%f",&math_score);
        printf("请输入英语成绩:");
        scanf("%f",&englist_score);
        memcpy(student[index-1].sno,sno,50);
        memcpy(student[index-1].name,name,50);
        student[index-1].math_score = math_score;
        student[index-1].englist_score = englist_score;
        student[index-1].avg_score = (math_score+englist_score)/2;
        IO_write(student);
        printf("是否继续,请输入Y/N\n");
        scanf("%s",is_continue);
        getchar();//清除缓冲区中的欢迎符号
        if(strcmp(is_continue,"N")==0){
            break;
        }
        
    }
}

void del_student(Stu * student){
    IO_read(student);
    while(1){
        int i;
        int res;
        printf("输入要删除的学生的学号\n");
        scanf("%s",sno);
        res = verify_sno(student);
        if(res==-1){
            printf("没有此学生\n");
            printf("请重新输入\n");
            continue;
        }
        if((res+1)==num){//删除的学生是最后一位
            num--;
            IO_write(student);
        }else{//不是最后一位
            num--;
            for(i=res;i<num;i++){
                memcpy(student[i].sno,student[i+1].sno,50);
                memcpy(student[i].name,student[i+1].name,50);
                student[i].math_score = student[i+1].math_score;
                student[i].englist_score = student[i+1].englist_score;
            }
            IO_write(student);
        }
        printf("是否继续,请输入Y/N\n");
        scanf("%s",is_continue);
        getchar();//清除缓冲区中的欢迎符号
        if(strcmp(is_continue,"N")==0){
            break;
        }
        
    }
}

void select_student_by_name(Stu * student){
    while(1){
        IO_read(student);
        int i;
        printf("输入学生姓名\n");
        scanf("%s",name);
        printf("    编号      学生学号        学生姓名       数学成绩       英语成绩\n");
        for(i=0;i<num;i++){
            if(strcmp(student[i].name,name)==0){
                printf("    %d          %s      %s      %f      %f \n",i+1,student[i].sno,student[i].name,student[i].math_score,student[i].englist_score);
            }
        }
        printf("是否继续,请输入Y/N\n");
        scanf("%s",is_continue);
        getchar();//清除缓冲区中的欢迎符号
        if(strcmp(is_continue,"N")==0){
            break;
        }
    }
    
}

void select_student_by_score(Stu * student){
    while(1){
        IO_read(student);
        int i,j;
        for(i=0;i<num-1;i++){
            for(j=0;j<num-i-1;j++){
                if(student[j].avg_score>=student[j+1].avg_score){
                    memcpy(sno,student[j].sno,50);
                    memcpy(name,student[j].name,50);
                    math_score = student[j].math_score;
                    englist_score = student[j].englist_score;
                    avg_score = student[j].avg_score;
                    
                    
                    memcpy(student[j].sno,student[j+1].sno,50);
                    memcpy(student[j].name,student[j+1].name,50);
                    student[j].math_score = student[j+1].math_score;
                    student[j].englist_score = student[j+1].englist_score;
                    student[j].avg_score = student[j+1].avg_score;
                    
                    
                    memcpy(student[j+1].sno,sno,50);
                    memcpy(student[j+1].name,name,50);
                    student[j+1].math_score = math_score;
                    student[j+1].englist_score = englist_score;
                    student[j+1].avg_score = avg_score;
                }
            }
        }
        printf("    编号      学生学号        学生姓名       数学成绩       英语成绩\n");
        for(i=0;i<num;i++){
            printf("    %d          %s      %s      %f      %f \n",i+1,student[i].sno,student[i].name,student[i].math_score,student[i].englist_score);
        }
        printf("是否继续,请输入Y/N\n");
        scanf("%s",is_continue);
        getchar();//清除缓冲区中的欢迎符号
        if(strcmp(is_continue,"N")==0){
            break;
        }
    }
}

void IO_write(Stu * student){
    FILE *fp;
    int i;
    fp = fopen("student.txt","wb");
    if(fp==NULL){
        printf("打开文件失败\n");
        exit(0);
    }
    for(i=0;i<num;i++){
        if(fwrite(&student[i],sizeof(struct stu),1,fp)!=1){
            printf("写入失败\n");
            fclose(fp);
        }
    }
    fclose(fp);
    config_write();
}

void print_student(Stu * student){
    int i;
    if(num==0){
        printf("暂无学生信息\n");
        return;
    }
    printf("    编号      学生学号        学生姓名       数学成绩       英语成绩\n");
    for(i=0;i<num;i++){
        printf("    %d          %s      %s      %f      %f \n",i+1,student[i].sno,student[i].name,student[i].math_score,student[i].englist_score);
    }
}

void IO_read(Stu * student){
    FILE *fp;
    int i;
    fp = fopen("student.txt","rb");
    if(fp==NULL){
        printf("打开文件失败\n");
        exit(0);
    }
    
    for(i=0;i<num;i++){
        fread(&student[i],sizeof(struct stu),1,fp);
    }
    fclose(fp);
}


void config_write(){
    fp = fopen("config.txt","wb");
    if(fp==NULL){
        printf("打开文件失败\n");
        exit(0);
    }
    if(fwrite(&num,sizeof(int),1,fp)!=1){
        printf("写入失败\n");
        fclose(fp);
    }
    fclose(fp);
}

void config_read(){
    fp = fopen("config.txt","rb");
    if(fp==NULL){
        printf("打开文件失败\n");
        exit(0);
    }
    fread(&num,sizeof(int),1,fp);
    fclose(fp);
}

/**
 *验证是否已经有重复的学号
 */
int verify_sno(Stu * student){
    int i;
    IO_read(student);
    for(i=0;i<num;i++){
        if(strcmp(student[i].sno,sno)==0){//学号相同
            return i;
        }
    }
    return -1;
}



 
上一篇下一篇

猜你喜欢

热点阅读