用C语言链表编写学生成绩管理系统

本代码供读者学习使用,请不要随意转载。

一、设计题目:学生成绩管理系统

二、目的与要求
每位学生记录包含有学号、姓名、性别、出生日期、三门功课的成绩(高等数学、大学英语、C语言)、总分和平均分
系统菜单:
(1)录入学生记录
(2)添加学生记录
(3)删除学生记录
(4)修改学生记录(要求输入密码)
(5)查找学生记录(按学号、按姓名)
(6)按总分对记录进行降序排列
(7)将当前结果显示或打印,重新保存进数据文件中
(8)输出所有学生信息
(9)计算班级平均分
(10)修改管理员密码(未保存至文件,程序关闭后失效)
(11)C语言挂科人数
(0)结束程序

#include<stdio.h>
#include<stdlib.h> 
#include<string.h>
#include<malloc.h>
#include<math.h>
#define LEN sizeof(struct student)
 struct student
{
    struct student *next;
    long num;
    char name[8];
    char sex[4];
    int year;
    int month;
    int day;
    float c;
    float math;
    float eng;
    float sum;
};

int n;
struct student * creat()          //建立基础的学生信息库 
{
    struct student * head,*p1,*p2;
    n=0;
    p1=p2=(struct student *)malloc(LEN);
    printf("输入学生的学号为0时,停止录入.\n");
        printf("请输入学生学号:");
        scanf("%ld",&p1->num);
        if(p1->num!=0)
        {
        printf("请输入学生姓名:");scanf("%s",p1->name);
        printf("请输入学生性别:");scanf("%s",p1->sex); 
     printf("请输入学生生日:\n");
     printf("年:");scanf("%d",&p1->year);
     printf("月:");scanf("%d",&p1->month);
     printf("日:");scanf("%d",&p1->day);
        printf("c语言:");scanf("%f",&p1->c);
        printf("高数:");scanf("%f",&p1->math);
        printf("英语:");scanf("%f",&p1->eng);
        p1->sum=p1->c+p1->eng+p1->math;
        printf("\n");
    }
    while(p1->num!=0)
    {
        n=n+1;
        if(n==1)
        {
            head=p1;
        }
        else
        {
            p2->next=p1;
        }
        p2=p1;
        p1=(struct student *)malloc(LEN);
            printf("请输入学生学号:");
    scanf("%ld",&p1->num);
    if(p1->num!=0)
    {
    printf("请输入学生姓名:");scanf("%s",p1->name);
    printf("请输入学生性别:");scanf("%s",p1->sex);
     printf("请输入学生生日:\n");
     printf("年:");scanf("%d",&p1->year);
     printf("月:");scanf("%d",&p1->month);
     printf("日:");scanf("%d",&p1->day);
        printf("c语言:");scanf("%f",&p1->c);
        printf("高数:");scanf("%f",&p1->math);
        printf("英语:");scanf("%f",&p1->eng);
        p1->sum=p1->c+p1->eng+p1->math;
        printf("\n");
    }
    }
     p2->next=NULL;
    return head;
}

struct student * del(struct student *head,long num) //删除学生信息 
{
    struct student  *p1,*p2;
    if(head==NULL)
    {
        printf("\nlist null!\n");
        return head;
    }
    p1=head;
    while(num!=p1->num&&p1->next!=NULL)
    {
        p2=p1;p1=p1->next;
    }
        if(num==p1->num)
        {
            if(p1==head)
                head=p1->next;
            else
                p2->next=p1->next;
            printf("你删除的学生信息为:\n");
            printf("学号:%ld\n",p1->num);
                    printf("姓名:%s\n",p1->name);
                    printf("性别:%s\n",p1->sex);
                    printf("生日:\n");
                    printf("年:%d\n",p1->year);
                    printf("月:%d\n",p1->month);
                    printf("日:%d\n",p1->day);
                    printf("c语言:%5.2f\n",p1->c);
                    printf("高数:%5.2f\n",p1->math);
                    printf("英语:%5.2f\n",p1->eng);
                    p1->sum=p1->c+p1->math+p1->eng;
                    printf("总分:%5.2f\n",p1->sum);
                    printf("\n");
            n=n-1;
        }
            else
                printf("输入有误!\n");
        return head;
}

struct student * insert (struct student *head,struct student *stud) //添加学生信息 
{
    struct student * p0,*p1,*p2;
    p1=head;
    p0=stud;
    if(head==NULL)
    {
        head=p0;
        p0->next=NULL;
    }
    else
        while((p0->num>p1->num)&&(p1->next!=NULL))
        {
            p2=p1;
            p1=p1->next;
        }
        if(p0->num<=p1->num)
        {
            if(head==p1)
                head=p0;
            else
                p2->next=p0;
            p0->next=p1;
        }
        else
        {
            p1->next=p0;
            p0->next=NULL;
        }
        n=n+1;
        return head;
}




void sort(struct student  *head) //排序 
{
    struct student *p, *q, *max;
    long temp1;
    char temp2[4],temp9[4];
    int temp3,temp4,temp5;
    float temp,temp6,temp7,temp8;
    p = head;
    while (p != NULL)
    {
        max = p;
        q = p->next;
        while (q != NULL)
        {
            if (max->sum < q->sum)
                max = q;
            q = q->next;
        }
        // 交换值
        if (max != p)
        {
            temp = max->sum;max->sum = p->sum;p->sum = temp;
            temp1=max->num;max->num=p->num;p->num=temp1;
            strcpy(temp2,max->name);strcpy(max->name,p->name);strcpy(p->name,temp2);
            temp3=max->year;max->year=p->year;p->year=temp3;
            temp4=max->month;max->month=p->month;p->month=temp4;
            temp5=max->day;max->day=p->day;p->day=temp5;
            temp6=max->c;max->c=p->c;p->c=temp6;
            temp7=max->math;max->math=p->math;p->math=temp7; 
            temp8=max->eng;max->eng=p->eng;p->eng=temp8;
            strcpy(temp9,max->sex);strcpy(max->sex,p->sex);strcpy(p->sex,temp9);
        }
        p = p->next;
    }
printf("排序以后的学生信息为:\n"); 
struct student *p1;
  p1 = head;
int count=1;
while(p1!=NULL)
{
    printf("第%d名:\n",count);
    printf("学号:%ld   ",p1->num);
                    printf("姓名:%s   ",p1->name);
                    printf("生日:");
                    printf("%d.",p1->year);
                    printf("%d.",p1->month);
                    printf("%d   ",p1->day);
                    printf("c语言:%5.3f   ",p1->c);
                    printf("高数:%5.3f   ",p1->math);
                    printf("英语:%5.3f   ",p1->eng);
    printf("总分:%4.2f   ",p1->sum);
    printf("\n");
    count++;
    p1=p1->next;   
}
}

float ave(struct student * head)//求平均数
{
    int i;
    float ave,sum=0;
    struct student *p;
    p=head;
    for(i=0;p!=NULL;i++)
    {
        sum=sum+p->sum;
        p=p->next;
    }
    ave=sum/i;
    return ave;
}

void change(struct student *head,long num)//修改学生数据 
{
    struct student *p;
    p=head;
    for(;p!=NULL;)
    {
        if(p->num==num)
        {
            printf("请输入学生姓名:");scanf("%s",p->name);
            printf("请输入学生性别:");scanf("%s",p->sex);
     printf("请输入学生生日:\n");
     printf("年:");scanf("%d",&p->year);
     printf("月:");scanf("%d",&p->month);
     printf("日:");scanf("%d",&p->day);
        printf("c语言:");scanf("%f",&p->c);
        printf("高数:");scanf("%f",&p->math);
        printf("英语:");scanf("%f",&p->eng);
        p->sum=p->c+p->eng+p->math;
        printf("\n");
        break;
        }
        else
        {
            p=p->next;
         } 
    }
}

void filein(struct student *head)//保存到文件中 
{
    FILE *fp;
    struct student *p;
    if((fp=fopen("D:\\20161181\\23\\student.dat","wb"))==NULL)//打开文件 
    {
        printf("can't open.\n");
        exit(0);
    }
    p=head; 
    while(p!=NULL)                //将链表的内容存储到文本文件中 
    {
        fwrite(p,LEN,1,fp);
        printf("\n");
        p=p->next;
    }
    fclose(fp);
    printf("成功保存至D:\\20161181\\23\\student.dat\n");
}
struct student *fileout(struct student *head)
{
    FILE *fp;
    struct student *p,*s;
    if((fp=fopen("D:\\20161181\\23\\student.dat","rb"))==NULL)
        return NULL;
    else if(fgetc(fp)==EOF)
        return NULL;
    rewind(fp);
    head=(struct student *)malloc(LEN);
    fread(head,LEN,1,fp);
    p=head;
    while(!feof(fp))
    {
        s=(struct student *)malloc(LEN);
        if(fread(s,LEN,1,fp)==0)
            break;
        p->next=s;
        p=s;
        p->next=NULL;
    }
    return head;
    fclose(fp);

}

struct student * locate(struct student *head,long num1)//按学号查找
{
 struct student *p1,*p2;
          p1=head;
          if(head==NULL)    //空链表时返回
          {
                printf("/n链表为空!/n");
                return(head);
          }
          else
          {
                while(num1!=p1->num && p1->next!=NULL)
                {
                    p2=p1;
                    p1=p1->next;
                }
    if(p1->num==num1)//比较输入学号是否与链表中学生学号匹配 
        {
            printf("查找的学生信息为:\n");
            printf("学号:%ld\n",p1->num);
            printf("姓名:%s\n",p1->name);
            printf("性别:%s\n",p1->sex);                                                      
            printf("生日:\n");                            
            printf("年:%d\n",p1->year);                         
            printf("月:%d\n",p1->month);                     
            printf("日:%d\n",p1->day);                       
            printf("c语言:%5.3f\n",p1->c);                            
            printf("高数:%5.3f\n",p1->math);                          
            printf("英语:%5.3f\n",p1->eng);
            p1->sum=p1->c+p1->eng+p1->math;
            printf("总分:%5.2f\n",p1->sum);                      
            printf("\n");                          
            return head;
        }
    else
    {
        printf("无该学生数据\n");
        return head;
    }
}
       }
  struct student * locate1(struct student *head)//按姓名查找
{
    char name[10];
    printf("请输入要查询学生的姓名:");
    scanf("%s",name); 
 struct student *p1,*p2;
          p1=head;
          if(head==NULL)    //空链表时返回
          {
                printf("/n链表为空!/n");
                return(head);
          }
          else
          {
                while(strcmp(name,p1->name)==0 && p1->next!=NULL)
                {
                    p2=p1;
                    p1=p1->next;
                }
    if(strcmp(name,p1->name)==0)//比较输入姓名与链表中学生姓名是否匹配 
        {
            printf("查找的学生信息为:\n");
            printf("学号:%ld\n",p1->num);
            printf("姓名:%s\n",p1->name);
            printf("性别:%s\n",p1->sex);                                                      
            printf("生日:\n");                            
            printf("年:%d\n",p1->year);                         
            printf("月:%d\n",p1->month);                     
            printf("日:%d\n",p1->day);                       
            printf("c语言:%5.3f\n",p1->c);                            
            printf("高数:%5.3f\n",p1->math);                          
            printf("英语:%5.3f\n",p1->eng);
            p1->sum=p1->c+p1->eng+p1->math;
            printf("总分:%5.2f\n",p1->sum);                      
            printf("\n");                          
            return head;
        }
    else
    {
        printf("无该学生数据\n");
        return head;
    }
}
       }



      //输出学生信息 
 void print(struct student *head)
 {
    struct student *p;
    p=head;
    printf("现在链表里的数据为:\n");
                while(p!=NULL)
                {
                    printf("学号:%ld   ",p->num);
                    printf("姓名:%s   ",p->name);
                    printf("性别:%s   ",p->sex);
                    printf("生日:%d.",p->year);
                    printf("%d.",p->month);
                    printf("%d   ",p->day);
                    printf("c语言:%5.2f   ",p->c);
                    printf("高数:%5.2f   ",p->math);
                    printf("英语:%5.2f   ",p->eng);
                    printf("总分:%5.2f   ",p->sum);
                    printf("\n");
                    p=p->next;
                }
                printf("\n");
}
void sum(struct student *head)
{
    int n=0;
    struct student *p,*p1;
    p=head;
    while(p!=NULL)
    {
    if(p->c<60)
    {
        n++;    
    }
    p=p->next; 
    }
    if(n==0)
    { 
    printf("(o^.^o)本次C语言无人挂科.\n");
    }
if(n>0)
 { 
 printf("本次C语言挂科人数有%d人\n\n",n);
 printf("挂科人员的学号,姓名如下\n\n");
 }
 p1=head;
 while(p1!=NULL&&p1->c<60)
 {
    printf("学号:%ld  ",p1->num);
    printf("姓名:%s\n",p1->name);
    p1=p1->next;
 }

printf("\n");
}
int main()
{
    int choose,n;
    long number,e;
    char name[6];
    struct student *head,*p;
     char password[10]={"123456"};
     char password1[10],password2[10],password3[10],password4[10],num[12];
    printf("请输入进入学生成绩管理系统的管理员代号和密码:\n系统默认登录密码为:123456.\n管理员代号:");
        scanf("%s",num);
        printf("密码:"); 
        scanf("%s",password1);
        printf("\n");
    for(;;)
    {
        if(strcmp(password1,password)!=0)
        {
    printf("输入错误,请重新输入:\n管理员代号:");
        scanf("%s",num);
        printf("\n密码:"); 
        scanf("%s",password1);
        printf("\n");
        }
        else
        {
        printf("%s管理员成功登陆\n",num); 

        break;
        }   
    }
            printf("★★★★欢迎使用学生成绩管理系统★★★★\n\n");
            printf("---------------------------------------\n");
            printf("\t0.退出程序\n\t1.录入学生记录\n\t2.添加学生记录\n");
            printf("\t3.删除学生记录\n\t4.修改学生记录\n");
            printf("\t5.查找学生记录\n\t6.按总分对学生记录进行降序排序\n");
            printf("\t7.将数据保存至文件\n\t8.输出所有学生信息\n\t9.计算班级平均分\n");
            printf("\t10.修改管理员密码\n\t11.统计C语言挂科人数\n");
            system("pause");
            system("cls");
            head=fileout(head);
            choose=-1;
            while(choose!=0)
            {
                printf("★★★★欢迎使用学生成绩管理系统★★★★\n");
                printf("---------------------------------------\n");
                printf("\t0.退出程序\n\t1.录入学生记录\n\t2.添加学生记录\n");
                printf("\t3.删除学生记录\n\t4.修改学生记录\n");
                printf("\t5.查找学生记录\n\t6.按总分对学生记录进行降序排序\n");
                printf("\t7.将数据保存至文件\n\t8.输出所有学生信息\n\t9.计算班级平均分\n");
                printf("\t10.修改管理员密码\n\t11.统计C语言挂科人数\n");
                printf("请输入一个数字:\n");
                scanf("%d",&choose);
                system("cls");
                switch(choose)
                {
                    case 0:
                        printf("\n\n");
                        printf("★★★★期待您的下次使用★★★★");

                        printf("\n\n");
                        break;
                    case 1:
                     head=creat();
                     system("pause");
                     system("cls");
                     break;
                     case 2:
                        p=(struct student *)malloc(LEN);
                        printf("请输入要添加学生学号\n"); 
                        printf("学号:\n");
                        scanf("%ld",&p->num);
                        if(p->num!=0)
                        {
                            printf("请输入学生姓名:");scanf("%s",p->name);
                            printf("请输入学生性别:");scanf("%s",p->sex);
                            printf("请输入学生生日:\n");
                            printf("年:");scanf("%d",&p->year);
                            printf("月:");scanf("%d",&p->month);
                            printf("日:");scanf("%d",&p->day);
                            printf("c语言:");scanf("%f",&p->c);
                            printf("高数:");scanf("%f",&p->math);
                            printf("英语:");scanf("%f",&p->eng); 
                            p->sum=p->c+p->eng+p->math;
                            head=insert(head,p);
                        }
                        system("pause");
                        system("cls");
                        break;
                        case 3:
                            printf("输入您要删除的学号:\n");
                            scanf("%ld",&e);
                            if(e!=0)
                            head=del(head,e);
                            system("pause");
                            system("cls");
                            break;
                            case 4:
                                int num;
                                printf("请输入密码:\n");
                                scanf("%s",password4);
                                while(strcmp(password,password4)!=0)
                                {
                                        printf("输入错误,请重输:\n");
                                        scanf("%s",password4);
                                } 
                                     printf("输入密码正确!\n");
                                     printf("请输入要修改学生学号:");
                                     scanf("%ld",&num);
                                     change(head,num); 
                                    system("pause");
                                    system("cls");
                                    break;
                                    case 5:
                                        int ch;
                                        printf("输入您要查找的学生的内容:\n1.按学号查找\n2.按姓名查找\n");
                                        scanf("%d",&ch);
                                        if(ch==1)
                                        {
                                            printf("请输入要查询学生学号:");
                                        scanf("%ld",&number);
                                        head=locate(head,number);
                                        printf("\n");
                                        }
                                        if(ch==2)
                                        {
                                            head=locate1(head);
                                            printf("\n");
                                        }
                                        system("pause");
                                        system("cls");
                                        break;
                                    case 6:
                                        sort(head);
                                        system("pause");
                                        system("cls");
                                        break;
                                        case 7:
                                            filein(head); 
                                            system("pause");
                                            system("cls");
                                            break;                  
                    case 8:
                        print(head);
                        system("pause");
                        system("cls");
                        break;
                        case 9:

                            float aver;
                            aver=ave(head);
                            printf("该班平均分为:%4.2f\n",aver);
                            system("pause");
                            system("cls");
                            break;
                            case 10:
                        printf("旧密码:");
                        scanf("%s",password1);
                        printf("新密码:");
                        scanf("%s",password2);
                        printf("请您确认新密码:"); 
                        scanf("%s",password3);
                        for(;;)
                        {
                            if(strcmp(password,password1)!=0)
                            {
                            printf("输入的旧密码有误\n");
                            printf("旧密码:");
                        scanf("%s",password1);
                        printf("新密码:");
                        scanf("%s",password2);
                        printf("请确认新密码:"); 
                        scanf("%s",password3);
                            }
                            if(strcmp(password2,password3)!=0)
                            {
                            printf("输入的新密码与确认的新密码不一致\n");
                            printf("旧密码:");
                        scanf("%s",password1);
                        printf("新密码:");
                        scanf("%s",password2);
                        printf("请确认新密码:"); 
                        scanf("%s",password3);
                            }
                            if(strcmp(password1,password)==0&&strcmp(password2,password3)==0)
                            {
                                printf("成功修改密码.\n");
                                break; 
                            }
                        }
                        strcpy(password,password2);
                        system("pause");
                        system("cls");
                        break;
                        case 11:
                            sum(head);
                            system("pause");
                            system("cls");
                            break;
                }
            }
    return 0;
 }

1.先输入系统规定密码123456进入学生成绩管理系统。进入后能看到以下界面。
主界面
2.按照下图信息进行学生信息的录入。
学生信息录入
3.按3删除学号为3的学生信息。
这里写图片描述
再按8看看链表中的数据是不是被删除了。
这里写图片描述
4.按2添加学生为3的学生信息。
这里写图片描述
按8看看添加的效果,程序是按照学号排序的方式插入的。
这里写图片描述

5.按数字5查找学生记录。(两种方式。1.按照学号查询 2.按照姓名查询)
1.按照学号查询
这里写图片描述
2.按照姓名查询
这里写图片描述

6.按数字6根据总分对学生成绩进行排序。
这里写图片描述

7.按数字9计算班级平均分
8.按数字10进行密码修改(由于未存入文件,再次打开仍然要输入原始密码,此项密码修改只针对第4项功能的修改学生记录时的密码输入)
这里写图片描述
9.按数字4进行学生记录的修改(需要输入上次修改密码)
这里写图片描述

可以看到原始密码123456已经失效。
9.按数字11统计C语言挂科人数。
有挂科人员时,会输出挂科人员的学号和姓名。
这里写图片描述
10.按数字7将学生信息保存至文件(文件路径为D:\20161181\23\stu.dat)。
第二次打开程序时,原有数据会丢失,由于没有做读取文件的功能。

读者可以自行完善文件的读取功能

这里写图片描述
11.按数字0退出程序。

经测试,上述代码未出现问题。

猜你喜欢

转载自blog.csdn.net/weixin_40169642/article/details/82467214