C语言实现班级学生成绩管理系统

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
struct Student//学生结构
{
    char Name[10];//姓名
    int No;//学号
    float Score[5];//5克的成绩
    float Total;//总分


};


typedef struct Node//结点结构
{
    struct Student st;
    struct Node *pNext;
} NODE, *PNODE;
/*
NODE等价于struct Student st
PNODE等价于struct Node *pNext
*/




PNODE InputStudent(void)//输入学生信息
{
    int len,i;//学生的人数
    NODE stu;//学生结构
    PNODE pHead  = (PNODE)malloc(sizeof(NODE));//定义一个头结点并且为头结点分配内存
    PNODE pTail = pHead;//定义一个指向头结点的指针
    pTail->pNext = NULL;//清空指针域
    printf("请输入学生的人数:");
    scanf("%d",&len);
    for(i=0; i<len; i++)
    {
        system("cls");//清屏
        printf("请输入第%d个学生的姓名:", i+1);
        scanf("%s", stu.st.Name);
        printf("请输入第%d个学生的学号:", i+1);
        scanf("%d", &stu.st.No);
        printf("请输入第%d个学生的语文成绩:", i+1);
        scanf("%f", &stu.st.Score[0]);
        printf("请输入第%d个学生的数学成绩:", i+1);
        scanf("%f", &stu.st.Score[1]);
        printf("请输入第%d个学生的英语成绩:", i+1);
        scanf("%f", &stu.st.Score[2]);
        printf("请输入第%d个学生的物理成绩:", i+1);
        scanf("%f", &stu.st.Score[3]);
        printf("请输入第%d个学生的化学成绩:", i+1);
        scanf("%f", &stu.st.Score[4]);
        stu.st.Total = stu.st.Score[0] + stu.st.Score[1] + stu.st.Score[2] + stu.st.Score[3] + stu.st.Score[4];//计算总分
        PNODE pNew = (PNODE)malloc(sizeof(NODE));//为新节点分配内存
        pNew->st = stu.st;//初始化结点的数据域
        pTail->pNext = pNew;//将新结点挂到老结点后
        pNew->pNext = NULL;//清空新结点的指针域
        pTail = pNew;//将pTail移到新结点上
    }
    return pHead;
}


void OutputStudent(PNODE pHead)//输出学生信息
{
    PNODE p = pHead->pNext;  //定义一个指针用于遍历学生信息
    printf("姓名   学号  语文   数学   英语   物理   化学   总分\n");
    while(NULL != p)
    {
        printf("%s  %d   %-4.f   %-4.f   %-4.f   %-4.f   %-4.f  %-4.f\n", p->st.Name,p->st.No, p->st.Score[0], p->st.Score[1], p->st.Score[2], p->st.Score[3],  p->st.Score[4], p->st.Total);
        p = p->pNext;
    }
}


void SearchStudent(PNODE pHead)//查找学生信息
{
    char Name[10];
    printf("请输入你需要查找的学生的姓名:");
    scanf("%s",Name);
    PNODE p = pHead->pNext;
    while(NULL != p)
    {
        if(0 == strcmp(Name,p->st.Name))
        {
            printf("%s  %d   %-4.f   %-4.f   %-4.f   %-4.f   %-4.f  %-4.f\n", p->st.Name,p->st.No, p->st.Score[0], p->st.Score[1], p->st.Score[2], p->st.Score[3],  p->st.Score[4], p->st.Total);
        }
        p = p->pNext;
    }
}


void ChangeStudent(PNODE pHead)//修改学生信息
{
    char Name[10];
    printf("请输入你需要修改的学生的姓名:");
    scanf("%s",&Name);
    PNODE p = pHead->pNext;//定义一个指针用于遍历学生信息
    while(NULL != p)
    {


        if(0 == strcmp(Name,p->st.Name))
        {


            printf("修改前的学生的信息:");
            printf("姓名   学号  语文   数学   英语   物理   化学   总分\n");
            printf("%s  %d   %-4.f   %-4.f   %-4.f   %-4.f   %-4.f  %-4.f\n", p->st.Name,p->st.No, p->st.Score[0], p->st.Score[1], p->st.Score[2], p->st.Score[3],  p->st.Score[4], p->st.Total);
            system("pause");
            system("cls");
            printf("请输入新的学生的姓名:");
            scanf("%s",&p->st.Name);
            printf("请输入新的学生的学号:");
            scanf("%d",&p->st.No);
            printf("请输入新的学生的语文:");
            scanf("%f",&p->st.Score[0]);
            printf("请输入新的学生的数学:");
            scanf("%f",&p->st.Score[1]);
            printf("请输入新的学生的英语:");
            scanf("%f",&p->st.Score[2]);
            printf("请输入新的学生的物理:");
            scanf("%f",&p->st.Score[3]);
            printf("请输入新的学生的化学:");
            scanf("%f",&p->st.Score[4]);
            p->st.Total = p->st.Score[0] + p->st.Score[1] + p->st.Score[2] + p->st.Score[3] + p->st.Score[4];
            break;


        }
        p = p->pNext;
    }
}


void DeleteStudent(PNODE pHead)//删除学生信息
{
    PNODE p = pHead;
    int a;
    int i=0;
    printf("请输入你需要删除的学生编号:");
    scanf("%d",&a);
    while(NULL == p->pNext)
    {
        p = p->pNext;
        i++;
    }
    PNODE q = p->pNext;
    p->pNext = q->pNext;
    free(q);
    printf("你已经成功删除了第%d个学生的信息!\n",a);


}


void InsertStudent(PNODE pHead)//添加学生信息
{
    PNODE p = pHead;
    PNODE s;
    int a;//插入结点的位置
    int i=0;//计数器
    struct Student stu;//学生结构
    printf("请输入你想插入的位置:");
    scanf("%d",&a);
    while(p->pNext != NULL && i<a)
    {
        p = p->pNext;
        i++;
    }


    if(NULL == p || i>a)
    {
        printf("插入结点的位置不存在!\n");


        return;
    }
    printf("请输入学生的姓名:");
    scanf("%s",stu.Name);
    printf("请输入学生的学号:");
    scanf("%d",&stu.No);
    printf("请输入学生的语文:");
    scanf("%f",&stu.Score[0]);
    printf("请输入学生的数学:");
    scanf("%f",&stu.Score[1]);
    printf("请输入学生的英语:");
    scanf("%f",&stu.Score[2]);
    printf("请输入学生的物理:");
    scanf("%f",&stu.Score[3]);
    printf("请输入学生的化学:");
    scanf("%f",&stu.Score[4]);
    stu.Total = stu.Score[0] + stu.Score[1] + stu.Score[2] + stu.Score[3] + stu.Score[4];
    s = (PNODE)malloc(sizeof(NODE));//申请填装结点
    s->st = stu;
    s->pNext = p->pNext;
    p->pNext = s;
}


void ScortByTotal(PNODE pHead)//对总分进行排序
{
    PNODE p, q;//定义两个指针,进行遍历
    NODE temp;  //用于交换
    for(p = pHead->pNext; p != NULL; p = p->pNext)
    {
        for(q = pHead->pNext; q != NULL; q= q->pNext)
        {
            if(p->st.Total < q->st.Total)//当前一个学生的总分小于后一个学生的总分时
            {
                temp.st = p->st;//将p的数据赋值给temp
                p->st = q->st;//将q的数据赋值给p
                q->st = temp.st;//将temp的数据赋值给q
            }
        }
    }
}


void menu()
{
    printf("\t\t|**********欢迎进入学生信息管理系统**********|\n");
    printf("\t\t|                 1,录入学生信息             |\n");
    printf("\t\t|                 2,显示学生信息             |\n");
    printf("\t\t|                 3,查询学生信息             |\n");
    printf("\t\t|                 4,修改学生信息             |\n");
    printf("\t\t|                 5,添加学生信息             |\n");
    printf("\t\t|                 6,删除学生信息             |\n");
    printf("\t\t|                 7,按总分排名次             |\n");
    printf("\t\t|                 0,退出                     |\n");
    printf("\t\t|********************************************|\n");
    printf("请输入你所选的选项(0--7:");


}


int main()
{
    PNODE pHead = NULL;//定义一个指针
    int a;
    while(1)
    {
        menu();
        scanf("%d",&a);
        switch(a)
        {
        case 1:
            pHead = InputStudent();
            system("pause");
            system("cls");
            break;
        case 2:
            OutputStudent(pHead);
            system("pause");
            system("cls");
            break;
        case 3:
            SearchStudent(pHead);
            system("pause");
            system("cls");
            break;
        case 4:
            ChangeStudent(pHead);
            system("pause");
            system("cls");
            break;


        case 5:
            InsertStudent(pHead);
            system("pause");
            system("cls");
            break;
        case 6:
            DeleteStudent(pHead);
            system("pause");
            system("cls");
            break;
        case 7:
            ScortByTotal(pHead);
            OutputStudent(pHead);
            system("pause");
            system("cls");
            break;
        case 0:
            exit(-1);
        }


    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/u012345982/article/details/80197365