学校作业,先发到博客上
更新更改已知错误
再度更新最终版

#include<string.h>  
#include<stdio.h>
#include<stdlib.h>
#define N 1000 
struct Student
{
    char ID[20];
    char Name[20];
    float Mark[3];
    float Average;
};
float Avg(struct Student stu);//求平均值
int Student_SearchByIndex(char id[]);//通过学号返回数组下标
int Student_SearchByName(char name[]);//通过姓名返回数组下标
void Student_DisplaySingle(int index);//显示单条学生记录
void Student_Insert();//插入学生信息
void Student_Modify();//修改学生信息
void Student_Delete();//删除学生信息
void Student_SelectByName();//按姓名查询
void Student_SelectByID();//按学号查询
void Student_SortByAverage();//按平均值排序
void Student_Display();//显示全部学生信息
void IO_ReadInfo();//将学生信息从文件读出
void IO_WriteInfo();//将学生信息写入文件



/*声明学生数组及学生数量*/

struct Student students[N];
int num = 0;

/*求平均值*/
float Avg(struct Student stu)
{
    return (stu.Mark[0] + stu.Mark[1] + stu.Mark[2]) / 3;
}

/*通过学号返回数组下标
将学生学号与查询的学号做比较,
如果相等则返回该学生的下标 ,找不到则返回-1. */
int Student_SearchByIndex(char id[])
{
    int i;
    for (i = 0; i < num; i++)
    {
        if (strcmp(students[i].ID, id) == 0)
        {
            return i;
        }
    }
    return -1;
}



/*通过姓名返回数组下标*/
int Student_SearchByName(char name[])
{
    int i;
    for (i = 0; i < num; i++)
    {
        if (strcmp(students[i].Name, name) == 0)
        {
            return i;
        }
    }
    return -1;
}



/*显示单条学生记录*/
void Student_DisplaySingle(int index)
{
    printf("%10s%10s%8s%8s%8s%10s\n", "学号", "姓名", "成绩", "成绩", "成绩", "平均成绩");
    printf("-------------------------------------------------------------\n");
    printf("%10s%10s%8.2f%8.2f%8.2f%10.2f\n", students[index].ID, students[index].Name,
        students[index].Mark[0], students[index].Mark[1], students[index].Mark[2], students[index].Average);

}

/*插入学生信息*/
void Student_Insert()
{
    while (1)
    {
        printf("请输入学号:");
        scanf("%s", &students[num].ID);
        getchar();
        printf("请输入姓名:");
        scanf("%s", &students[num].Name);
        getchar();
        printf("请输入成绩:");
        scanf("%f", &students[num].Mark[0]);
        getchar();
        printf("请输入成绩:");
        scanf("%f", &students[num].Mark[1]);
        getchar();
        printf("请输入成绩:");
        scanf("%f", &students[num].Mark[2]);
        getchar();
        students[num].Average = Avg(students[num]);
        num++;
        printf("是否继续?(y/n)");
        if (getchar() == 'n')
        {
            break;
        }
    }
}



/*修改学生信息*/
void Student_Modify()
{
    //float Mark[0],Mark[1],Mark[2];  
    while (1)
    {
        char id[20];
        int index;
        printf("请输入要修改的学生的学号:");
        scanf("%s", &id);
        getchar();
        index = Student_SearchByIndex(id);
        if (index == -1)
        {
            printf("学生不存在!\n");
        }
        else
        {
            printf("你要修改的学生信息为:\n");
            Student_DisplaySingle(index);
            printf("-- 请输入新值--\n");
            printf("请输入学号:");
            scanf("%s", &students[index].ID);
            getchar();
            printf("请输入姓名:");
            scanf("%s", &students[index].Name);
            getchar();
            printf("请输入成绩:");
            scanf("%f", &students[index].Mark[0]);
            getchar();
            printf("请输入成绩:");
            scanf("%f", &students[index].Mark[1]);
            getchar();
            printf("请输入成绩:");
            scanf("%f", &students[index].Mark[2]);
            getchar();
            students[index].Average = Avg(students[index]);
        }
        printf("是否继续?(y/n)");
        if (getchar() == 'n')
        {
            break;
        }
    }
}

/*删除学生信息*/
void Student_Delete()
{
    int i;
    while (1)
    {
        char id[20];
        int index;
        printf("请输入要删除的学生的学号:");
        scanf("%s", &id);
        getchar();
        index = Student_SearchByIndex(id);

        if (index == -1)
        {
            printf("学生不存在!\n");
        }
        else
        {
            printf("你要删除的学生信息为:\n");
            Student_DisplaySingle(index);
            printf("是否真的要删除?(y/n)");
            if (getchar() == 'y')
            {
                for (i = index; i < num - 1; i++)
                {
                    students[i] = students[i + 1];//把后边的对象都向前移动    
                }
                num--;
            }
            getchar();
        }
        printf("是否继续?(y/n)");
        if (getchar() == 'n')
        {
            break;
        }
    }
}

/*按学号查询*/
void Student_SelectByID()
{
    while (1)
    {
        char ID[20];
        int index;
        printf("请输入要查询的学生的学号:");
        scanf("%s", &ID);

        index = Student_SearchByIndex(ID); //调用查询函数 

        if (index == -1)
        {
            printf("学生不存在!\n");
        }
        else
        {
            printf("你要查询的学生信息为:\n");
            Student_DisplaySingle(index);    //调用显示单条记录函数
        }
        printf("是否继续?(y/n)");
        fflush(stdin);
        if (getchar() == 'n')
        {
            break;
        }
    }
}

/*按姓名查询*/
void Student_SelectByName()
{
    while (1)
    {
        char name[20];
        int index;
        printf("请输入要查询的学生的姓名:");
        scanf("%s", &name);
        getchar();
        index = Student_SearchByName(name);

        if (index == -1)
        {
            printf("学生不存在!\n");
        }
        else
        {
            printf("你要查询的学生信息为:\n");
            Student_DisplaySingle(index);
        }
        printf("是否继续?(y/n)");
        if (getchar() == 'n')
        {
            break;
        }
    }
}


/*按平均值排序*/
void Student_SortByAverage()
{
    int i, j;
    struct Student tmp;
    for (i = 0; i < num; i++)
    {
        for (j = 1; j < num - i; j++)
        {
            if (students[j - 1].Average < students[j].Average)
            {
                tmp = students[j - 1];
                students[j - 1] = students[j];
                students[j] = tmp;
            }
        }
    }
}

/*显示全部学生信息*/
void Student_Display()
{
    int i;
    printf("%10s%10s%8s%8s%8s%10s\n", "学号", "姓名", "成绩", "成绩", "成绩", "平均成绩");
    printf("-------------------------------------------------------------\n");
    for (i = 0; i < num; i++)
    {
        printf("%10s%10s%8.2f%8.2f%8.2f%10.2f\n", students[i].ID, students[i].Name,
            students[i].Mark[0], students[i].Mark[1], students[i].Mark[2], students[i].Average);
    }
}

/*将学生信息从文件读出*/
void IO_ReadInfo()
{
    FILE* fp;
    int i;
    if ((fp = fopen("Database.txt", "rb")) == NULL)
    {
        printf("不能打开文件!\n");
        return;
    }
    if (fread(&num, sizeof(int), 1, fp) != 1)
    {
        num = -1;
    }
    else
    {
        for (i = 0; i < num; i++)
        {
            fread(&students[i], sizeof(struct Student), 1, fp);
        }
    }
    fclose(fp);
}

/*将学生信息写入文件*/
void IO_WriteInfo()
{
    FILE* fp;
    int i;
    if ((fp = fopen("Database.txt", "wb")) == NULL)
    {
        printf("不能打开文件!\n");
        return;
    }
    if (fwrite(&num, sizeof(int), 1, fp) != 1)
    {
        printf("写入文件错误!\n");
    }
    for (i = 0; i < num; i++)
    {
        if (fwrite(&students[i], sizeof(struct Student), 1, fp) != 1)
        {
            printf("写入文件错误!\n");
        }
    }
    fclose(fp);
}


int main()
{
    int choice;
    IO_ReadInfo(); //调用读学生信息函数
    while (1)
    {
        /*主菜单*/
        printf("\n**********************欢迎使用学生成绩管理系统********************\n");
        printf(" \n1.增加学生记录\t2.查看学生记录\t3.修改学生记录\t4.删除学生记录\n");
        printf(" \n5.查询学生记录\t6.按平均成绩排序\t\t7. 退出\n");
        printf("\n******************************************************************\n");
        printf("请选择(1-7):");
        scanf("%d", &choice);
        fflush(stdin);
        switch (choice)
        {
        case 1:
            Student_Insert();
            break;
        case 2:
            Student_Display();
            break;
        case 3:
            Student_Modify();
            break;
        case 4:
            Student_Delete();
            break;
        case 5:
            Student_SelectByID();
            break;
        case 6:
            Student_SortByAverage();
            Student_Display();
            break;
        case 7:
            exit(0);
            break;
        }
        IO_WriteInfo();
    }
    return 0;
}