学生成绩管理系统

闲着没事,做一个经典项目:学生成绩管理系统。

实现功能如下:

增加新学生成绩、查看所有成绩榜单、根据姓名/学号修改或删除学生信息、查看实时排名

至此,我链表的学习就算告一段落了。

界面如下:

 

代码:(六百行,没有头文件)

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


//学生成绩
typedef struct Student
{
    char num[10];        //编号
    char name[20];        //名字
    int c_grade;        //c成绩
    int math_grade;        //数学
    int english_grade;    //英语
    int total;            //总分
    float ave;            //平均分
    int mingci;            //排名
}student;

typedef struct NODE
{
    struct Student data;    //学生数据
    struct NODE *next;        //指向下个节点
}node;

typedef struct Link
{
    int size_link;        //链表长度(学生数)
    node *head;            //头节点指针
    node *tail;            //尾节点指针
}link;

/********函数声明*******/
void DisplayMianSurface(void);
node *CreateNode(char num[],char name[],int c_grade,int math_grade,int english_grade);
link *BuildLink();
void DisplayAddStudent(link *p_link);
int AddNewStudent(link *p_link,char num[],char name[],int c_grade,int math_grade,int english_grade);
void GeneralFunction();
void DisplayAllStudent(link *p_link);
void QueryScore(link *p_link);
void ReviseScore(link *p_link);
void HeadofForm(void);
void PrintfAllInfo(node *p_tmp);
void RefreshRanking(link *p_link);
int ScoreRanking(link *p_link,node *p_node);

/*******
*函数功能:显示主界面
*
*
********/
void DisplayMianSurface(void)
{
    printf("*------------------------------------*
");
    printf("*| Welcome to Student Manage System |*
");
    printf("*| -------------------------------- |*
");
    printf("*| 1.View all student scores        |*
");
    printf("*| 2.Add new student information    |*
");
    printf("*| 3.Delete student information     |*
");
    printf("*| 4.Query student scores           |*
");
    printf("*| 5.Revise student scores          |*
");
    printf("*------------------------------------*
");
}


/*******
*函数功能:新建节点
*输入参数
*返回值
********/
node *CreateNode(char num[],char name[],int c_grade,int math_grade,int english_grade)
{
    node *p_creat = NULL;
    p_creat = (node *)malloc(sizeof(node));
    if (p_creat == NULL)
    {
        printf("*| ERROR:CreateNode Failed          |*
");
        printf("*------------------------------------*
");
        exit(0);
    }
    memset(p_creat,0,sizeof(node));

    strcpy(p_creat->data.num,num);
    strcpy(p_creat->data.name,name); 
    p_creat->data.c_grade = c_grade;
    p_creat->data.math_grade = math_grade;
    p_creat->data.english_grade = english_grade;
    p_creat->next = NULL;

    return p_creat;
}


/*******
*函数功能:新建链表
*
*
********/
link *BuildLink()
{
    link *p_creat = NULL;
    p_creat = (link *)malloc(sizeof(link));
    if (p_creat == NULL)
    {
        printf("*| ERROR:BuildLink Failed           |*
");
        printf("*------------------------------------*
");
        exit(0);
    }
    memset(p_creat,0,sizeof(link));
    p_creat->head = NULL;
    p_creat->tail = NULL;

    return p_creat;
}

/*******
*函数功能:1.增添新学生界面
*
*
********/
void DisplayAddStudent(link *p_link)
{
    char num[10],name[20];
    int a;
    int c_grade,math_grade,english_grade;
NewStudent:
    printf("   Please input student number:	");
    scanf("%s",num);
    printf("   Please input student name:	");
    scanf("%s",name);
    printf("   Please input c score:	");
    scanf("%d",&c_grade);
    printf("   Please input math score:	");
    scanf("%d",&math_grade);
    printf("   Please input english score:	");
    scanf("%d",&english_grade);    
    printf("*------------------------------------*
");

    AddNewStudent(p_link,num,name,c_grade,math_grade,english_grade);
    printf("*| New student massage add sucssed  |*
");
    printf("*------------------------------------*
");
    while(1)
    {
        printf("*  Please enter 1.Continue 2.cancel:");
        scanf("%d",&a);
        if (a == 1)
        {
            goto NewStudent;
        }
        else if( a == 2)
        {
            return;
        }
    }
}

/*******
*函数功能:显示所有学生信息
*
*
********/
void DisplayAllStudent(link *p_link)
{
    node *p_tmp = p_link->head;
    //第一组数据作为头节点 不动 也不打印
    p_tmp = p_tmp->next; //从第二个开始显示*********
    int s;
    printf("*------------------------------------------------------------------------------*
");
    HeadofForm();
    while(p_tmp != NULL)
    {
        PrintfAllInfo(p_tmp);
        p_tmp = p_tmp->next;
    }
    printf("*------------------------------------------------------------------------------*
");
    printf("*  Please enter 1 to return
");
    printf("*------------------------------------------------------------------------------*
");
    while(1)
    {
        scanf("%d",&s);
        if(s == 1)
        {
            return;
        }
    }
}


/*******
*函数功能:榜单排头
*
*
********/
void HeadofForm(void)
{
    printf("|Num	|Name	|Cscore	|Math	|English	|Total	|Average	|Ranking
");
}

/*******
*函数功能:打印全部信息
*
*
********/
void PrintfAllInfo(node *p_tmp)
{
    printf(" %s	",p_tmp->data.num);
    printf(" %s	",p_tmp->data.name);
    printf(" %d	",p_tmp->data.c_grade);
    printf(" %d	",p_tmp->data.math_grade);
    printf(" %d		",p_tmp->data.english_grade);
    printf(" %d	",p_tmp->data.total);
    printf(" %.2lf		",p_tmp->data.ave);
    printf(" %d
",p_tmp->data.mingci);
}

/*******
*函数功能:根据名字(编号)查询学生分数
*
*
********/
void QueryScore(link *p_link)
{
    node *p_tmp = p_link->head;
    int tmp;
    char name[20],num[10];
NUMBERorNAME:
    printf("*  Please choose 1.Number 2.Name 3.Cancel:");
    scanf("%d",&tmp);
    if (tmp == 1)
    {
        printf("*  Please enter Student Number :");
        scanf("%s",num);
        while(p_tmp != NULL)
        {
            if (strcmp(p_tmp->data.num,num) == 0)
            {
                printf("*------------------------------------------------------------------------------*
");
                HeadofForm();
                PrintfAllInfo(p_tmp);
                printf("*------------------------------------------------------------------------------*
");
                goto NUMBERorNAME;
            }
            p_tmp = p_tmp->next;
        }
        printf("*  The Number you are looking for does not exist
");
        goto NUMBERorNAME;
    }
    else if(tmp ==2)
    {
        printf("*  Please enter Student Name :");
        scanf("%s",name);
        while(p_tmp != NULL)
        {
            if (strcmp(p_tmp->data.name,name) == 0)
            {
                printf("*------------------------------------------------------------------------------*
");
                HeadofForm();
                PrintfAllInfo(p_tmp);
                printf("*------------------------------------------------------------------------------*
");
                goto NUMBERorNAME;
            }
            p_tmp = p_tmp->next;
        }
        printf("*  The Name you are looking for does not exist
");
        goto NUMBERorNAME;
    } 
    else if(tmp == 3)
    {
        return ;
    }
    else{
        printf("*  Please enter again
");
        goto NUMBERorNAME;
    }

}


/*******
*函数功能:根据名字(编号)修改学生分数
*
*
********/
void ReviseScore(link *p_link)
{
    node *p_tmp = p_link->head;
    int tmp;
    char name[20],num[10];
NUMBERorNAME:
    printf("*  Please choose 1.Number 2.Name 3.Cancel:");
    scanf("%d",&tmp);
    if (tmp == 1)
    {
        printf("*  Please enter Student Number :");
        scanf("%s",num);
        while(p_tmp != NULL)
        {
            if (strcmp(p_tmp->data.num,num) == 0)
            {
                printf("*  Please input c score:	");
                scanf("%d",&p_tmp->data.c_grade);
                printf("*  Please input math score:	");
                scanf("%d",&p_tmp->data.math_grade);
                printf("*  Please input english score:	");
                scanf("%d",&p_tmp->data.english_grade);
                p_tmp->data.total = p_tmp->data.c_grade + p_tmp->data.math_grade + p_tmp->data.english_grade;
                p_tmp->data.ave = (float)(p_tmp->data.total) / 3;    
                p_tmp->data.mingci = ScoreRanking(p_link,p_tmp);
                printf("*  Modified to complete
");
                printf("*------------------------------------------------------------------------------*
");
                HeadofForm();
                PrintfAllInfo(p_tmp);
                printf("*------------------------------------------------------------------------------*
");
                goto NUMBERorNAME;
            }
            p_tmp = p_tmp->next;
        }
        printf("*  The Number you are looking for does not exist
");
        goto NUMBERorNAME;
    }
    else if(tmp ==2)
    {
        printf("*  Please enter Student Name :");
        scanf("%s",name);
        while(p_tmp != NULL)
        {
            if (strcmp(p_tmp->data.name,name) == 0)
            {
                printf("*  Please input c score:	");
                scanf("%d",&p_tmp->data.c_grade);
                printf("*  Please input math score:	");
                scanf("%d",&p_tmp->data.math_grade);
                printf("*  Please input english score:	");
                scanf("%d",&p_tmp->data.english_grade);
                p_tmp->data.total = p_tmp->data.c_grade + p_tmp->data.math_grade + p_tmp->data.english_grade;
                p_tmp->data.ave = (float)(p_tmp->data.total) / 3;
                p_tmp->data.mingci = ScoreRanking(p_link,p_tmp);
                printf("*  Modified to complete
");    
                printf("*------------------------------------------------------------------------------*
");
                HeadofForm();
                PrintfAllInfo(p_tmp);
                printf("*------------------------------------------------------------------------------*
");
                goto NUMBERorNAME;
            }
            p_tmp = p_tmp->next;
        }
        printf("*  The Name you are looking for does not exist
");
        goto NUMBERorNAME;
    } 
    else if(tmp == 3)
    {
        return ;
    }
    else{
        printf("*  Please enter again
");
        goto NUMBERorNAME;
    }
}


/*******
*函数功能:刷新排名
*
*
********/
void RefreshRanking(link *p_link)
{
    node *p_tmp = p_link->head;
    while(p_tmp != NULL)
    {
        p_tmp->data.mingci = ScoreRanking(p_link,p_tmp);
        p_tmp = p_tmp->next;
    }
}

/*******
*函数功能:根据姓名(编号)删除学生
*
*
********/
void DeleteStudent(link *p_link)
{
    node *p_tmp = p_link->head,*p_free = p_link->head;
    int tmp;
    char julge[10];
    char name[20],num[10];
NUMBERorNAME:
    printf("*  Please choose 1.Number 2.Name 3.Cancel:");
    scanf("%d",&tmp);
    if (tmp == 1)
    {
        printf("*  Please enter Student Number :");
        scanf("%s",num);
        while(p_tmp != NULL)
        {
            if (strcmp(p_tmp->data.num,num) == 0)
            {
                printf("*  Please confirm whether to delete (yes/no)	");
                scanf("%s",julge);
                if (strcmp(julge,"yes") == 0)
                {

                    p_free->next = p_tmp->next;
                    if (p_tmp->next == NULL)
                    {
                        p_link->tail = p_free;
                    }
                    free(p_tmp);
                    goto NUMBERorNAME;
                }
                else if(strcmp(julge,"no") == 0)
                {
                    goto NUMBERorNAME;
                }
                else
                {
                    return;
                }
            }    
            p_free = p_tmp;
            p_tmp = p_tmp->next;
        }
        printf("*  The Number you are looking for does not exist
");
        goto NUMBERorNAME;
    }
    else if(tmp ==2)
    {
        printf("*  Please enter Student Name :");
        scanf("%s",name);
        while(p_tmp != NULL)
        {
            if (strcmp(p_tmp->data.name,name) == 0)
            {
                printf("*  Please confirm whether to delete (yes/no)	");
                scanf("%s",julge);
                if (strcmp(julge,"yes") == 0)
                {

                    p_free->next = p_tmp->next;
                    if (p_tmp->next == NULL)
                    {
                        p_link->tail = p_free;
                    }
                    free(p_tmp);
                    goto NUMBERorNAME;
                }
                else if(strcmp(julge,"no") == 0)
                {
                    goto NUMBERorNAME;
                }
                else
                {
                    return;
                }
            }    
            p_free = p_tmp;
            p_tmp = p_tmp->next;
        }
        printf("*  The Name you are looking for does not exist
");
        goto NUMBERorNAME;
    } 
    else if(tmp == 3)
    {
        return ;
    }
    else{
        printf("*  Please enter again
");
        goto NUMBERorNAME;
    }

}



/*******
*函数功能:测试函数:添加3个学生信息
*
*
********/
void Text(link *p_link)
{
    char *a[3] ,*b[3];
    a[1] = "1201";
    a[2] = "1763";
    a[0] = "1492";
    AddNewStudent(p_link,"0000","000",00,00,00); //头节点
    AddNewStudent(p_link,a[1],"Tom",24,89,72);
    AddNewStudent(p_link,a[2],"Hong",53,37,92);
    AddNewStudent(p_link,a[0],"Yang",34,63,23);
}


/*******
*函数功能:1.增添新学生
*
*
********/
int AddNewStudent(link *p_link,char num[],char name[],int c_grade,int math_grade,int english_grade)
{
    node *p_tmp = p_link->tail;
    node *p_creat = CreateNode(num,name,c_grade,math_grade,english_grade);
    if (p_link->head == NULL)
    {
        p_link->head = p_creat;
    }
    if (p_link->tail != NULL)
    {
        p_tmp->next = p_creat;
    }    
    p_link->tail = p_creat;

    p_link->size_link++;
    p_creat->data.total = p_creat->data.c_grade + p_creat->data.math_grade + p_creat->data.english_grade;
    p_creat->data.ave = (float)(p_creat->data.total) / 3;
    p_creat->data.mingci = ScoreRanking(p_link,p_creat);
    return 0;
}


/*******
*函数功能:求排名
*
*
********/
int ScoreRanking(link *p_link,node *p_node)
{
    int rank = 0,tmp;
    node *p_tmp = p_link->head;
    tmp = p_node->data.total;

    while(p_tmp != NULL)
    {
        if (tmp <= p_tmp->data.total)
        {
            rank++;
        }
        p_tmp = p_tmp->next;
    }
    return rank;
}

/*******
*函数功能:总功能函数
*
*
********/
void GeneralFunction()
{
    link *p_link = BuildLink();
    Text(p_link);//此为测试函数*********************
    int ack;
    while(1)
    {
        system("cls");
        DisplayMianSurface();
        printf("*  Please input fuction number:	");
        scanf("%d",&ack);
        if(ack == 1)
        {
            RefreshRanking(p_link);    //刷新排名
            DisplayAllStudent(p_link);    //打印榜单
        }
        else if(ack == 2)
        {
            DisplayAddStudent(p_link);
        }
        else if(ack == 3)
        {
            DeleteStudent(p_link);
        }
        else if(ack == 4)
        {
            QueryScore(p_link);
        }
        else if(ack == 5)
        {
            ReviseScore(p_link);
        }
        else
        {
            printf("   ERROR:Input Wrong Number
");
        }
    }
}


int main(int argc, char const *argv[])
{
    GeneralFunction();
    return 0;
}
原文地址:https://www.cnblogs.com/qifeng1024/p/12552551.html