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

📄 11.cpp

📁 一个用C语言写的学生成绩管理系统。主要是用链表实现。对初学C语言的同志有点用
💻 CPP
字号:
#include <stdio.h>
#include <stdlib.h>           /*头文件*/
#include <string.h>
#include <conio.h>
#define LEN sizeof(struct student)    /*令LEN代表struct student类型数据的长度*/
struct student                       /*声明一个学生结构体,包含四个成员*/
{
    int num;                        /* 定义一个整型的学号成员*/   
    char name[20];                  /* 定义一个字符型的名字成员*/
    long QQ;                        /* 定义一个长整型的QQ成员*/
    long telenum;                   /* 定义一个长整型的电话号码成员*/
    struct student *next;           /*定义一个指向学生结构体的next指针*/
};
struct student *creat(struct student *head);      /* 函数的声明*/
void check(struct student *head);
struct student *insert(struct student *head);
struct student *dele(struct student *head);           
struct student *xiugai(struct student *head);
void print(struct student *head);
int is_empty(struct student *head);
struct student *inverse(struct student *head);
struct student *daxiao(struct student *head);
void main()
{
     struct student *head ;
     char menu;                    /*指定一个字符menu*/
     head=creat(head);      /*调用creat函数并使地址返回给head*/
     printf("\nPlease input you chose menu sign!\n");
     printf("\ncheck(c\C) insert(i\I) dele(d\D) xiugai(x\X) print(p\P) inverse(n\N) daxiao(m\M) exit(e)");
     for (;;)
     {
	 menu = getch();         /*输入menu*/
	 printf("\t%c",menu);
	 if(menu!='e')
	 {
	     switch(menu)
	     {
		  case 'c':
		  case 'C':  check(head);   /*如果输入C、c则调用查找函数,并使地址返回给head*/
			     break;
		  case 'i':
		  case 'I':  head=insert(head);   /*如果输入i、I调用插入函数,并使地址返回给head*/
			     print(head);
			     break;
		  case 'd':
		  case 'D':  head=dele(head);   /*如果输入d、D调用删除函数,并使地址返回给head*/
			      print(head);
			     break;
		  case 'x':
		  case 'X':  head=xiugai(head);   /*如果输入x、X调用修改函数,并使地址返回给head*/
			     print(head);
			     break;
		  case 'p':
		  case 'P':  print(head);
			     break;
		  case 'n':
		  case 'N':  head=inverse(head);
			     print(head);
			     break;
		  case 'm':
		  case 'M':  print(head);
			     head=daxiao(head);
			     print(head);
			     break;
		  default:   printf("Error enter!!");
			     break;
	     }
	 }
	 else
	      break;
     }
}
struct student *creat(struct student *head)       /*定义建立函数*/  /*建立链表*/
{
    struct student *p,*q;
    int i,length;
    if((head=(struct student *)malloc(LEN))==NULL)
    {
	printf("Not enough menory to allocate buffer\n");
	exit(1);
    }
    printf("\nPlease input length of list!");
    scanf("%d",&length);
    for (i=0;i<length;i++)
    {
	if((p=(struct student *)malloc(LEN))==NULL)   /*为p开辟一个空间*/
	{
	    printf("Not enough menory to allocate buffer\n");
	    exit(1);
	}
	if(i==0)
	{
	    head->next=p;
	    q=p;
	}
	else
	{
	    q->next=p;
	    q=p;
	}
	printf("NO.:");
	scanf("%d",&p->num);   /*输入各成员的值*/
	printf("Name:");
	scanf("%s",p->name);
	printf("QQ:");
	scanf("%ld",&p->QQ);
	printf("Telenum:");
	scanf("%ld",&p->telenum);
	}
	p->next=NULL;
	return head;
}
void check(struct student *head)   /*定义查找函数*/
{
     struct student *p;
     int xuehao ;
     char xingming[20],sign;
     printf("\nPlease input the sign(x,m)!");
     sign = getch();
     printf("\t%c",sign);
     switch(sign)
     {
	  case 'x':   printf("\nPlease input the check student's xuehao!");
		      scanf("%d",&xuehao);
		      break;
	  case 'm':   printf("\nPlease input the check student's name!");
		      scanf("%s",xingming);
		      break;
	  default:    printf("Error enter!");
		      break;
     }
     for (p = head;p != NULL; )
     {
	  if(p->num!=xuehao && strcmp(p->name,xingming)!=0)
	      p=p->next;
	  else
	  {
	      printf("%2d%6s%8ld%10ld",p->num,p->name,p->QQ,p->telenum);
	      break;
	  }
     }
}
struct student *insert(struct student *head)
{
     struct student *p1,*p2,*p;
     char sign,xingming[20];
     int xuehao;
     p=(struct student *)malloc(LEN);
     printf("\nPlease input insert stuent's data!");
     scanf("%d%s%ld%ld",&p->num,p->name,&p->QQ,&p->telenum);
     printf("Please input the sign(x,m)!");
     sign = getch();
     printf("\t%c",sign);
     switch(sign)
     {
	  case 'x':   printf("\nPlease input  student's xuehao!");
		      scanf("%d",&xuehao);
		      break;
	  case 'm':   printf("\nPlease input student's name!");
		      scanf("%s",xingming);
		      break;
	  default:    printf("Error enter!");
		      break;
     }
     p1=head;
     p2=p1;
     if(is_empty(head)==1)
     {
	  head->next=p;
	  p->next=NULL;
     }
     else
     {
	  while(p1->num!=xuehao&&strcmp(p1->name,xingming)!=0&&p1->next!=NULL)
	  {
		  p2=p1;
		  p1=p1->next;
	  }
	  if(xuehao==p1->num||strcmp(p1->name,xingming)==0)
	  {
	      if(p1==head)
	      head->next=p;
	      else
	      p2->next=p;
	      p->next=p1;
	  }
	  else
	  {
	       p1->next=p;
	       p->next=NULL;
	  }
     }
     return head;
}
struct student *dele(struct student *head)
{
     struct student *p1,*p2;
     char sign,xingming[20];
     int xuehao;
     printf("\nPlease input the sign!(x,m)");
     sign=getch();
     printf("\t%c",sign);
     switch(sign)
     {
	 case 'm':  printf("\nPlease input dele student's name!");
		    scanf("%s",xingming);
		    break;
	 case 'x':  printf("\nPlease input dele studetn's num!");
		    scanf("%d",&xuehao);
		    break;
	 default:   printf("Error enter!");
		    break;
     }
     p1=head;
     while(xuehao!=p1->num&&strcmp(p1->name,xingming)!=0&&p1->next!=NULL)
     {                            /*p1指向的不是要找的接点,且后面还有接点*/
	  p2=p1;
	  p1=p1->next;               /*p1后移一个接点*/
     }
     if(xuehao==p1->num||strcmp(p1->name,xingming)==0)  /*找到要删除的点*/
     {
	  if(p1==head)            /*若p1指向是首接点,把第二个接点地址赋给head*/
	  head=p1->next;
	  else
	  p2->next=p1->next;      /*否则将下一个地址赋给前一个接点地址*/
     }
     else                    /*没找到接点*/
     printf("\nSorry not found you want to  dele student's data!\n");
     return head;
}
struct student *xiugai(struct student *head)
{
     struct student *p;
     char sign,xingming[20];
     int xuehao;
     printf("\nPlease input the sign(x,m)!");
     sign=getch();
     printf("\t%c",sign);
     switch(sign)
     {
	 case 'm':  printf("\nPlease input the xiugai student's name!");
		    scanf("%s",xingming);
		    break;
	 case 'x':  printf("\nPlease input the xiugai studetn's num!");
		    scanf("%d",&xuehao);
		    break;
	 default:   printf("Error enter!");
		    break;

     }
     p=head;
     while(p!=NULL)
     {
	  if((p->num!=xuehao)&&strcmp(p->name,xingming)!=0)
	       p=p->next;
	  else
	  {
	       printf("Please input the xiugai student's data!");
	       scanf("%d%s%ld%ld",&p->num,p->name,&p->QQ,&p->telenum);
	       break;
	  }
     }
     return head;
}
void print(struct student *head)
{
     struct student *p;
     printf("\n   No.    name         QQ          telenum\n");
     for (p=head->next;p!=NULL;p=p->next)
     {
	 printf("%5d%8s%12ld%16ld",p->num,p->name,p->QQ,p->telenum);
	 printf("\n");
     }
}
int is_empty(struct student *head)
{
    int flag;
    if(head->next==NULL)
    flag=1;
    else
    flag=0;
    return(flag);
}
struct student *inverse(struct student *head)
{
    struct student *p,*q,*w,*temphead;
    if (is_empty(head) == 0)
    {
	for (p = head, q = head->next; q->next != NULL; p = p->next, q = q->next)
	    ;
	temphead = q;
	p->next = NULL;
	while (head->next != NULL)
	{
	    for(p = head, q = head->next; q->next != NULL; p = p->next, q = q->next)
		;
	    for (w = temphead; w ->next!= NULL; w = w->next)
		;
	    w->next = q;
	    p->next = NULL;
	}
	head->next = temphead;
    }
    else
         exit(1);
    return head;
}
struct student *daxiao(struct student *head)
{
     struct student *p1,*p2,*p,*temp;
     int i,length;
     for (p1=head->next,length=1;p1!=NULL;p1=p1->next)
	 length++;
     for (i=0;i<length;i++)
     {
	 for (p1=head,p2=head->next;p2!=NULL;p=p1,p1=p1->next,p2=p2->next)
	 {
	     if(p1->num>p2->num)
	     {
		  if(p1==head)
		  {
		       p1->next=p2->next;
		       p2->next=p1;
		       temp=p1;
		       p1=p2;
		       p2=temp;
		  }
		  else
		  {
		       p->next=p2;
		       p1->next=p2->next;
		       p2->next=p1;
		       temp=p1;
		       p1=p2;
		       p2=temp;
		  }
	     }
	 }
     }
     return head;
}

⌨️ 快捷键说明

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