⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 student.c

📁 用动态链表实现关于学生的姓名
💻 C
字号:
#include<stdio.h>
#include<malloc.h>
#define NULL 0
struct student
{long number;
 char name[20];
 int math;
 int cxsj;
 struct student *next;
};
int n;
struct student *creat(void)
{int flag=0;
 struct student *head;
 struct student *p1,*p2,*p;
 n=0;
 p1=p2=(struct student*)malloc(sizeof(struct student));
 printf("请输入该学生的学号(当学号为0的时候表示结束):");
 scanf("%ld",&p1->number);
 printf("\n");
 printf("请输入该学生的姓名:");
 scanf("%s",p1->name);
 printf("\n");
 printf("请输入该学生的数学成绩:");
 scanf("%d",&p1->math);
 printf("\n");
 printf("请输入该学生的程序设计成绩:");
 scanf("%d",&p1->cxsj);
 printf("\n");
 printf("该生信息输入完成!!!\n@------------------------------------------------------------------------------@\n");
 head=NULL;
 while(p1->number!=0)
   {n=n+1;
    if(n==1)
    head=p1;
    else
    p2->next=p1;
    p2=p1;
    p1=(struct student *)malloc(sizeof(struct student));
    leap:printf("请输入该学生的学号(当学号为0的时候表示结束):");
    scanf("%ld",&p1->number);
    if(p1->number==0)
      {p2->next=NULL;
       break;
       }
    p=head;
    while(p!=NULL)
      {if(p->number==p1->number)
         {flag=1;
         }
       p=p->next;
       }
    if(flag==1)
      {printf("您输入的学号有重复,请重新输入!!\n");
       flag=0;
       goto leap;
       }
    printf("\n");
    printf("请输入该学生的姓名:");
    scanf("%s",p1->name);
    printf("\n");
    printf("请输入该学生的数学成绩:");
    scanf("%d",&p1->math);
    printf("\n");
    printf("请输入该学生的程序设计成绩:");
    scanf("%d",&p1->cxsj);
    printf("\n");
    printf("该生信息输入完成!!!\n@------------------------------------------------------------------------------@\n");
    }
 p2->next=NULL;
 printf("输入完毕!\n");
 p1=head;
    printf("学号\t\t姓名\t\t数学成绩\t程序设计成绩\t总分\n");
    while( head )
    {
        
        printf("%ld\t\t%s\t\t%d\t\t%d\t\t%d\n",head->number ,head->name ,head->math ,head->cxsj ,head->cxsj + head->math );
        head = head->next ;
    }
    head =p1;
 return(head);
 }
struct student *insert(struct student *head,struct student *stu)
{int flag=0;
 struct student *p0,*p1,*p2,*p;
 leap:stu = (struct student *)malloc(sizeof(struct student));
 printf("请输入添加学生的学号当学号(为0的时候表示结束):");
 scanf("%ld",&stu->number);
 printf("\n");
 p=head;
 while(p!=NULL)
   {if(p->number==stu->number)
      {flag=1;
      }
    p=p->next;
    }
 if(flag==1)
   {printf("您输入的学号有重复,请重新输入!!\n");
    flag=0;
    goto leap;
    }
 printf("请输入添加学生的姓名:");
 scanf("%s",stu->name);
 printf("\n");
 printf("请输入添加学生的数学成绩:");
 scanf("%d",&stu->math);
 printf("\n");
 printf("请输入添加学生的程序设计成绩:");
 scanf("%d",&stu->cxsj);
 printf("\n");
 p1=head;
 p0=stu;
 if(head==NULL)
   {head=p0;
    p0->next=NULL;
    }
 else
   {while((p0->number>p1->number)&&(p1->next!=NULL))
      {p2=p1;
       p1=p1->next;
       }
    if(p0->number<=p1->number)
      {if(head==p1)
         {head=p0;
         }
       else
        {p2->next=p0;
        }
       p0->next=p1;
       }
    else
      {p1->next=p0;
       p0->next=NULL;
       }
    }
 n=n+1;
 printf("已添加成功!\n");
 p1=head;
    printf("学号\t\t姓名\t\t数学成绩\t程序设计成绩\t总分\n");
    while( head )
    {
        
        printf("%ld\t\t%s\t\t%d\t\t%d\t\t%d\n",head->number ,head->name ,head->math ,head->cxsj ,head->cxsj + head->math );
        head = head->next ;
    }
    head =p1;
 return(head);
 }
void deleteall(struct student *head)
{
    struct student *p;
    p=head;
    while(head)
    {
        head = head->next ;
        free(p);
        p = head;
    }
}
struct student *del(struct student *head)
{long num;
 struct student *p1,*p2;
 printf("请输入要删除的同学的学号\n");
 scanf("%ld",&num);
 if(head==NULL)
   {printf("这是一组空数据\n");
    goto end;
    }
 p1=head;
 while((num!=p1->number)&&(p1->next!=NULL))
   {p2=p1;
    p1=p1->next;
    }
 printf("%ld %ld\n",num,p1->number);
 if(num==p1->number)
   {if(p1==head)
      head=p1->next;
    else
      p2->next=p1->next;
    printf("已删除:%ld\n",num);
    n=n-1;
    }
 else
   printf("%ld 没找到!\n",num);
   end:
   p1=head;
    printf("学号\t\t姓名\t\t数学成绩\t程序设计成绩\t总分\n");
    while( head )
    {
        
        printf("%ld\t\t%s\t\t%d\t\t%d\t\t%d\n",head->number ,head->name ,head->math ,head->cxsj ,head->cxsj + head->math );
        head = head->next ;
    }
    head =p1;
   return(head);
 }
shux(struct student *head)
{int i,j;
 int temp1,temp2;
 char m[20];
 struct student *p,*q;
 if(head==NULL)
   {printf("这是一个空数据!\n");
   }
 else
   {for(i=1;i<=n-1;i++)
      {p=head;
       q=p->next;
       for(j=1;j<=n-i;j++)
         {if(p->math<q->math)
            {temp1=p->math;
             p->math=q->math;
             q->math=temp1;
             temp2=p->cxsj;
             p->cxsj=q->cxsj;
             q->cxsj=temp2;
             strcpy(m,p->name);
             strcpy(p->name,q->name);
             strcpy(q->name,m);
             }
          p=q;
          q=p->next;
          }
      }
    printf("按数学成绩排序的结果为:\n");
    }
 p=head;
    printf("学号\t\t姓名\t\t数学成绩\t程序设计成绩\t总分\n");
    while( head )
    {
        
        printf("%ld\t\t%s\t\t%d\t\t%d\t\t%d\n",head->number ,head->name ,head->math ,head->cxsj ,head->cxsj + head->math );
        head = head->next ;
    }
    head =p;
 }
chengxsj(struct student *head)
{int i,j;
 int temp1,temp2;
 char m[20];
 struct student *p,*q;
 if(head==NULL)
   {printf("这是一个空数据!\n");
   }
 else
   {for(i=1;i<=n-1;i++)
      {p=head;
       q=p->next;
       for(j=1;j<=n-i;j++)
         {if(p->cxsj<q->cxsj)
            {temp1=p->math;
             p->math=q->math;
             q->math=temp1;
             temp2=p->cxsj;
             p->cxsj=q->cxsj;
             q->cxsj=temp2;
             strcpy(m,p->name);
             strcpy(p->name,q->name);
             strcpy(q->name,m);
             }
          p=q;
          q=p->next;
          }
      }
    printf("按程序设计成绩排序的结果为:\n");
    }
    p=head;
    printf("学号\t\t姓名\t\t数学成绩\t程序设计成绩\t总分\n");
    while( head )
    {
        
        printf("%ld\t\t%s\t\t%d\t\t%d\t\t%d\n",head->number ,head->name ,head->math ,head->cxsj ,head->cxsj + head->math );
        head = head->next ;
    }
    head =p;
 }
zfen(struct student *head)
{int i,j;
 int temp1,temp2;
 char m[20];
 struct student *p,*q;
 if(head==NULL)
   {printf("这是一个空数据!\n");
   }
 else
   {for(i=1;i<=n-1;i++)
      {p=head;
       q=p->next;
       for(j=1;j<=n-i;j++)
         {if((p->cxsj+p->math)<(q->cxsj+q->math))
            {temp1=p->math;
             p->math=q->math;
             q->math=temp1;
             temp2=p->cxsj;
             p->cxsj=q->cxsj;
             q->cxsj=temp2;
             strcpy(m,p->name);
             strcpy(p->name,q->name);
             strcpy(q->name,m);
             }
          p=q;
          q=p->next;
          }
      }
    printf("按总分排序的结果为:\n");
    }
 p=head;
    printf("学号\t\t姓名\t\t数学成绩\t程序设计成绩\t总分\n");
    while( head )
    {
        
        printf("%ld\t\t%s\t\t%d\t\t%d\t\t%d\n",head->number ,head->name ,head->math ,head->cxsj ,head->cxsj + head->math );
        head = head->next ;
    }
    head =p;
 }
paixu(struct student *head)
{int x=0;
 printf("1.数学成绩排序\n");
 printf("2.程序设计成绩排序\n");
 printf("3.总分排序。\n");
 printf("4.返回主菜单\n");
 printf("请输入X的值=");
 scanf("%d",&x);
 switch(x)
  {default:printf("输入了错误的X值\n");
   case 1:shux(head);break;
   case 2:chengxsj(head);break;
   case 3:zfen(head);break;
   case 4:return;break;
   }
}
xuehao(struct student *head)
{long x;
 struct student *p;
 p=head;
 printf("请输入要查询的学生学号:\n");
 scanf("%ld",&x);
 while((x!=p->number)&&(p!=NULL))
   {p=p->next;
   }
 if(p==NULL)
   {printf("您要找的学号不存在!\n");
    }
 else
   {printf("您所查询的学号对应的学生信息如下:\n");
    printf("%ld %s %5d %5d\n",p->number,p->name,p->math,p->cxsj);
    }
 }
xingming(struct student *head)
{char n[20];
 struct student *p;
 p=head;
 printf("请输入要查询的学生姓名:\n");
 scanf("%s",n);
 while((strcmp(n,p->name)!=0)&&(p!=NULL))
   {p=p->next;
   }
 if(p==NULL)
   {printf("您所要找的姓名不存在!\n");
   }
 else
   {printf("您所查询的姓名对应的学生信息如下:\n");
    printf("%ld %s %5d %5d\n",p->number,p->name,p->math,p->cxsj);
    }
}
shuxue(struct student *head)
{int score,flag=0;
 struct student *p;
 p=head;
 printf("请输入您要查询的数学成绩:\n");
 scanf("%d",&score);
 while(p!=NULL)
   {if((score==p->math)&&(flag==1))
      {printf("%ld %s %5d %5d\n",p->number,p->name,p->math,p->cxsj);
      }
    if((score==p->math)&&flag==0)
      {printf("考 %d 分的同学有:\n",score);
       printf("%ld %s %5d %5d\n",p->number,p->name,p->math,p->cxsj);
       flag=1;
       }
     p=p->next;
    }
 if(flag==0)
   {printf("没有考 %d 分的学生!\n",score);
   }
}
chxsj(struct student *head)
{int score,flag=0;
 struct student *p;
 p=head;
 printf("请输入您要查询的程序设计分数:\n");
 scanf("%d",&score);
 while(p!=NULL)
   {if((score==p->math)&&(flag==1))
      {printf("%ld %s %5d %5d\n",p->number,p->name,p->math,p->cxsj);
      }
    if((score==p->math)&&flag==0)
      {printf("考 %d 分的同学有:\n",score);
       printf("%ld %s %5d %5d\n",p->number,p->name,p->math,p->cxsj);
       flag=1;
       }
     p=p->next;
    }
 if(flag==0)
   {printf("没有考 %d 分的学生!\n",score);
   }
}
zf(struct student *head)
{int zongf,flag=0;
 struct student *p;
 p=head;
 printf("请输入您要查询的总分:\n");
 scanf("%d",&zongf);
 while(p!=NULL)
   {if((zongf==p->math+p->cxsj)&&(flag==1))
      {printf("%ld %s %5d %5d\n",p->number,p->name,p->math,p->cxsj);
      }
    if((zongf==p->math+p->cxsj)&&(flag==0))
      {printf("考 %d 分的同学有:\n",zongf);
       printf("%ld %s %5d %5d\n",p->number,p->name,p->math,p->cxsj);
       flag=1;
       }
    p=p->next;
    }
 if(flag==0)
   {printf("没有考 %d 分的学生!\n",zongf);
   }
}
chaxun(struct student *head)
{int x=0;
 printf("1.学号查询\n");
 printf("2.姓名查询\n");
 printf("3.数学成绩查询\n");
 printf("4.程序设计成绩查询\n");
 printf("5.总分查询\n");
 printf("6.返回主菜单\n");
 printf("请输入X的值=");
 scanf("%d",&x);
 switch(x)
   {default:printf("输入了错误的X值\n");
    case 1:xuehao(head);break;
    case 2:xingming(head);break;
    case 3:shuxue(head);break;
    case 4:chxsj(head);break;
    case 5:zf(head);break;
    case 6:break;
    }
 }
int main(void)
{int x=0;
 struct student *head=NULL,stu;
 do
   {printf("1.新建数据\n");
    printf("2.添加数据\n");
    printf("3.删除数据\n");
    printf("4.排序\n");
    printf("5.查询\n");
    printf("6.退出\n");
    printf("请输入X的值=");
    scanf("%d",&x);
    switch(x)
     {case 1:deleteall(head);head=creat();break;
      case 2:head=insert(head,&stu);break;
      case 3:head=del(head);break;
      case 4:paixu(head);break;
      case 5:chaxun(head);break;
      case 6:break;
      default:printf("输入了错误的X值\n");
      }
    }
 while(x!=6);
 getch();
 return 0;
 }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -