C語言學生信息管理系統(鏈表實現)

基礎功能:

  • 使用鏈表

  • 使用結構體

  • 對學生信息的增刪改查

  • 按姓名或者學號進行學生成績的排序


 

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define M 2
#define N 4
typedef struct score//定義成績結構體
{
	int math;//數學
	int c_language;//c語言
	int english;//英語
	double ave;//平均分
	int total_sum;//總分
}SCORE;

typedef struct stu
{
     char id[20];//學號
	 char name [10];//姓名
	 char sex[20];//性別
	 char pro[20];//專業
	 int year;//入學年份
     SCORE stu_score;//學生成績
	 char stu_lesson[N][50];//學生選課
}STU;

typedef struct list
{
	  STU data;
	  struct list *next;
}LIST;//創建鏈表結構體
LIST *Creat_List(STU s[]);//鏈表的創建
int  Stu_Entry(STU stu[]);//學生信息的錄入
int Score_Entry(STU score[]);//學生成績的錄入
void  S_score(LIST*head);//利用鏈表進行成績的輸出
void infor_show(LIST*head);//利用鏈表對學生信息進行輸出
void S_sort(STU s[],int flag);//利用結構體進行排序 
SCORE Score_search(char *s,LIST *p,int flag);//成績的查詢
LIST *Delet_Score(char*s,LIST*tr,int flag);//成績的刪除
LIST *Delet_infor(char*st,LIST *ht,int flag);//學生記錄的刪除
LIST *Re_score(char *sr,SCORE R_score,LIST *h,int flag);//成績修改
void Show_infor(char *S_s,LIST*h,int flag);//學生信息的查詢
int Elect_les(STU lesson[]);//學生選課
LIST* Insert_infor(LIST *in_infor );//學生信息的添加
void main()
{
	     printf("\t\t\t歡迎進入學生信息管理系統\n");
	     system("color 9");
	     int choice;//輸入你的選擇
	     char user[23];//輸入用戶名
	     char  key[20];//輸入密碼
	     STU stu[M];//結構體數組 
	     LIST *head;//鏈表的頭結點
	     head=Creat_List(stu);
          printf("請輸入用戶名:\n");
          scanf("%s",user);
		   printf("請輸入密碼:\n");
            scanf("%s",key);
			 printf("*******************************\n");
			 printf("*     1---信息錄入             *\n");
             printf("*     2---成績錄入             *\n");
             printf("*     3---成績輸出             *\n");
             printf("*     4---成績排序             *\n");
			 printf("*     5---成績查詢             *\n");
			 printf("*     6---成績修改             *\n");
			 printf("*     7---信息添加             *\n");
			 printf("*     8---信息刪除             *\n");
			 printf("*     9---成績刪除             *\n");
			 printf("*     10--信息查詢             *\n");
			 printf("*     11--信息輸出             *\n");
             printf("*     12--學生選課             *\n");
             printf("*     13--退出程序             *\n");
             printf("*******************************\n");

	 if(strcmp(user,"admin")==0&&strcmp(key,"123456")==0)
	 {
	      printf("請輸入你的選擇:\n");
		 while(scanf("%d",&choice)!=EOF)
		 {
              system("cls");
			 printf("*******************************\n");
			 printf("*     1---信息錄入             *\n");
             printf("*     2---成績錄入             *\n");
			 printf("*     3---成績輸出             *\n");
			 printf("*     4---成績排序             *\n");
			 printf("*     5---成績查詢             *\n");
			 printf("*     6---成績刪除             *\n");
			 printf("*     7---信息添加             *\n");
			 printf("*     8---信息刪除             *\n");
			 printf("*     9---成績修改             *\n");
			 printf("*     10--信息查詢             *\n");
			 printf("*     11--信息輸出             *\n");
			 printf("*     12--學生選課             *\n");
			 printf("*     13--退出程序             *\n");
			 printf("*******************************\n");
			 switch(choice)
			 {

			 case 1:{
				       printf("請錄入學生的信息:\n");
				       if(Stu_Entry(stu))
						   printf("信息錄入完成\n");
					    else
					       printf("信息錄入失敗\n");

                         system("pause");break;
					}
			 case 2:{
				            printf("請錄入每位學生對應的成績:\n");
					        if(Score_Entry(stu))
						       printf("學生成績錄入完成\n");
					       else
					           printf("學生成績錄入失敗\n");
                               system("pause");break;
					  }
			 case 3:{
				          printf("成績的輸出爲:\n");
						 
						  S_score(Creat_List(stu));
						  system("pause");break;

					   }
			 case 4:{  


				                  int choice;
								  printf("請輸入排序的方式(1-平均分排序 2-總分排序)\n");
									  scanf("%d",&choice);
			                    printf("成績排序的結果爲:\n");
								
			                      S_sort(stu,choice);
			                      S_score(Creat_List(stu));
			                      system("pause");break;
								  
			                    system("pause");break;
                           }
				case 5:{
				                  SCORE  result;
								   char name_search[20];
								   char id_search[20];
								   int choice;
								  printf("請輸入查詢方式(1-學號查詢 2-姓名查詢):\n"); 
								      scanf("%d",&choice);
								      switch(choice)
								     {
								       case 1: 
									    {   printf("請輸入需要查詢的學生的學號:\n");
										     scanf("%s",id_search);
											 printf("學號爲%s學生的數學 c語言 英語成績 總分以及平均分爲:\n",id_search);
										    result= Score_search(id_search,Creat_List(stu),1);
										      printf("查詢的結果爲:\n");
										      
				                            printf("%d  %d  %d  %d  %.1lf\n",result.math,
                                                result.c_language,result.english,result.total_sum,result.ave);
										      break;
									   }
									        
								   case 2:{
									          printf("請輸入需要查詢的學生的姓名:\n");
										      scanf("%s",name_search);
										       printf("學生%s的數學 c語言 英語成績 總分以及平均分爲:\n",name_search);
										      result= Score_search(id_search,Creat_List(stu),2);
											   printf("查詢的結果爲:\n");
											   
				                                 printf("%d  %d  %d  %d  %.1lf\n",result.math,
                                                   result.c_language,result.english,result.total_sum,result.ave);
										      break;
									      
										  } 
								   }
				                  
                                  system("pause");break;
                         }


				case 6: {
				                      
                                       char id[20];//需要查詢學號 
							           char name[20];//需要查詢的姓名 
									  int choice;//輸入你的選擇 
									 printf("請輸入查詢方式(1-學號查詢 2-姓名查詢):\n"); 
								      scanf("%d",&choice);
								      switch(choice)
								     {
								       case 1: 
									    {     
										    printf("請輸入要刪除成績學生的學號:\n");
										    scanf("%s",id);
										    if(Delet_Score(id,Creat_List(stu),1)!=NULL)
								               {
												    printf("成績刪除成功!\n");
								                    S_score(Delet_Score(id,Creat_List(stu),1));
								               }
                                            else 
                                               printf("成績刪除失敗!\n");
										         break;
									   }
									        
								   case 2:{
									          printf("請輸入要刪除信息學生的姓名:\n");
										      scanf("%s",name);
										       printf("信息刪除後的結果爲:\n");
											    if(Delet_Score(name,Creat_List(stu),2)!=NULL)
								                {

									               printf("成績刪除成功!\n");
								                   S_score(Delet_Score(name,Creat_List(stu),2));
								                 }
                                                 else
                                                    printf("成績刪除失敗!\n");
										              break;
									      
										  } 
								   }
				              
				            system("pause");break;

				            }
				case 7:{

					              

									   printf("插入後的結果爲:\n");
								       S_score(Insert_infor(Creat_List(stu)));
									   system("pause");break;
					   }

					  
				case 8:{         
									printf("學生信息的刪除:\n");
				                    LIST *q;
				                    char id[20];
							        char name[20];
									 int choice;
									 printf("請輸入查詢方式(1-學號查詢 2-姓名查詢):\n"); 
								      scanf("%d",&choice);
								      switch(choice)
								     {
								       case 1: 
									    { 
										       printf("請輸入要刪除信息學生的學號:\n");
										       scanf("%s",id);
										       q=Delet_infor(id,Creat_List(stu),1);
											   if(q==NULL)
												   printf("信息刪除失敗\n");
											   else
											   { 
												    printf("信息刪除後的結果爲:\n");
											         infor_show(q);
											   }
										      break;
									   }
									        
								   case 2:{
									          printf("請輸入要刪除信息學生的姓名:\n");
										      scanf("%s",name);
										       printf("信息刪除後的結果爲:\n");
											   if(q==NULL)
												   printf("信息刪除失敗!\n");
											    else
												{ 
													printf("信息刪除後的結果爲:\n");
												    q=Delet_infor(name,Creat_List(stu),2);
												}
											  
										       infor_show(q);
										      break;
									      
										  } 
								   }
				              
				             
				              
				               
				             system("pause");break;

                         }
                         case 9:{               
						                      
                                                
                                              int choice;
										        SCORE re_score;
										          int sum=0; 
										          
										          char id[20];
										          char name[20];
										 printf("請輸入要修改的英語 c語言 數學:\n");
										  scanf("%d %d %d",&re_score.english,&re_score.c_language, 
										  &re_score.math); 
										sum=re_score.english+re_score.c_language+re_score.math; 
										     re_score.ave=sum/3.0;
                                    printf("請輸入查詢方式(1-學號查詢 2-姓名查詢):\n"); 
								      scanf("%d",&choice);
								      switch(choice)
								     {
								       case 1: 
									    {  printf("請輸入要修改學生的學號:\n");
										     scanf("%s",id);
										     printf("成績修改後的結果爲:\n");
										    S_score(Re_score(id,re_score,Creat_List(stu),choice));
										      break;
									   }
									        
								   case 2:{
									          printf("請輸入要修改學生的姓名:\n");
										      scanf("%s",name);
										       printf("成績修改後的結果爲:\n");
										       S_score(Re_score(name,re_score,Creat_List(stu),choice));
										      break;
									         
										  } 
								   }
                                             
                                            
                                           
                                             system("pause");break;
                                    }
                case 10:{
					                 char id[20];
								     char name[20];
								      int choice;
								   printf("請輸入查詢方式(1-學號查詢 2-姓名查詢):\n"); 
								      scanf("%d",&choice);
								   switch(choice)
								   {
								     case 1: 
									    {  printf("請輸入要查詢學生的學號:\n");
										     scanf("%s",id);
										   Show_infor(id,Creat_List(stu),choice);
										   break;
									    }
									        
								    case 2:{
									          printf("請輸入要查詢學生的姓名:\n");
										      scanf("%s",name);
										      Show_infor(id,Creat_List(stu),choice);
										      break;
									      
										   } 
								   }
								    system("pause");break;

                            }
				case 11: {
					           printf("學生信息的輸出結果爲:\n");
                                    infor_show(Creat_List(stu));
                               system("pause");break;

                          }
				case 12:{       system("cls");
					            //system("pause");
					            printf("請輸入選修課程中的四門課程:\n");

					             if( Elect_les(stu))

									 printf("學生選課成功!\n");
								   else
									   printf("學生選課失敗!\n");

								break;

					   }


				case 13:  {
				                    printf("系統已經安全退出!!!");
				                    exit(0);
                                    system("pause");break;
                            }



                default:{
                                printf("輸入的選擇有誤,請重新輸入你的選擇:\n");
                                  system("pause");break;
                           }


			 }
			 printf("可以選擇繼續錄入也可以按ctrl+z退出\n");
		 }
	 }
	 else
	 {
	 	 system("cls");
	     printf("輸入用戶名或者密碼錯誤,請退出後重新輸入!\n");
	    
	 }
}
LIST *Creat_List(STU *s)//PS:鏈表的建立
{
	  //尾插法進行建立鏈表 
	LIST *h,*p;
	LIST*tail; 
	int i;
	h=(LIST*)(malloc)(sizeof(LIST));
	if(h==NULL)
	return h;
	else
      tail=h;
	for(i=0;i<M;i++)
	{
		p=(LIST*)malloc(sizeof(LIST));
		  if(p==NULL)
		  return NULL;
		  else
		  {
		     p->data=s[i];
		     tail->next=p;
		     tail=p;
		     tail->next=NULL;
		   }
	}
	//頭插法進行創建鏈表
	/* LIST*h,*p;
	 int i;
	 h=(LIST*)malloc(sizeof(LIST));
	  if(h==NULL)
	  return NULL;
	  else
	   h->next=NULL;
	   for(i=0;i<M;i++)
	   {
	     p=(LIST*)malloc(sizeof(LIST));
	       if(p==NULL)
	         return NULL;
	         else
	         {
	         	p->data=s[i];
	         	p->next=h->next;
	         	h->next=p;
			 }
		 }*/
	
	 
	return h;
}
int Stu_Entry(STU stu[M])//PS:學生信息錄入函數的封裝
{
     int i;

    for(i=0;i<M;i++)
	   {

              printf("輸入第%d個學生學號 姓名 性別  專業  入學年份: \n", i + 1);
              scanf("%s %s %s %s %d", stu[i].id, stu[i].name, stu[i].sex,
              &stu[i].pro, &stu[i].year);
              stu[i].stu_score.math=0;
		      stu[i].stu_score.c_language=0;
		      stu[i].stu_score.english=0;
        }

                 if(i==M)
		           return 1;
				 else
					 return 0;
}
int Score_Entry(STU score[])//PS:成績的輸入的函數的封裝
{
	     int i;
           int s=0;
	    STU *p=score;
	     for(i=0;i<M;i++)

	 {
		 printf("請輸入%d位學生的數學 c語言 英語:\n",i+1);
		   scanf("%d %d %d",&score[i].stu_score.math,&score[i].stu_score.c_language,
                       &score[i].stu_score.english);
                           getchar();
        s=score[i].stu_score.math+score[i].stu_score.c_language+score[i].stu_score.english;
		       score[i].stu_score.total_sum=s;
              score[i].stu_score.ave=s/3.0;
	 }
	 if(i==M)
		 return 1;
	  else
		  return 0;
}

void  S_score(LIST*head)//PS:成績輸出函數的封裝
{
    LIST *p;
    int count=1;
    p=head->next;
    if(p==NULL)
        {
            printf("鏈表發生錯誤!!!\n");
                return;
         }
        else
        {
            printf("HEAD->\n");
           while(p!=NULL)
            {
                printf("%d-學號:%s學生的數學 c語言 英語成績 總分以及平均分爲:\n",count,p->data.id);
                printf("%d  %d  %d  %d %.1lf\n",p->data.stu_score.math,
                       p->data.stu_score.c_language,
			       p->data.stu_score.english,p->data.stu_score.total_sum,p->data.stu_score.ave);
			       p=p->next;
			       count++;
            }
            printf("->END\n");

        }
}


void S_sort(STU s[],int flag)//PS:成績的排序函數的封裝 利用結構體 
{
    int i,j;
      int k;
      STU t;
  if(flag==1)
   {
	 for(i=0;i<M-1;i++)//選擇排序
     {     k=i;
        for(j=i;j<M;j++)
            if(s[k].stu_score.ave<s[j].stu_score.ave)
                       k=j;
           if(k!=i)
           {
               t=s[k];
               s[k]=s[i];
               s[i]=t;
            }
       }
   }
   if(flag==2)
   {
   	for(i=0;i<M-1;i++)//選擇排序
     {     k=i;
        for(j=i;j<M;j++)
            if(s[k].stu_score.total_sum<s[j].stu_score.total_sum)
                       k=j;
           if(k!=i)
           {
               t=s[k];
               s[k]=s[i];
               s[i]=t;
            }
       }
   	}
}
SCORE Score_search(char *s,LIST *h,int flag)//PS:成績的查詢函數的封裝
{
    LIST *p;
    p=h;
	 
    if(p==NULL)
    {
        printf("鏈表有誤!\n");
        
    }
    else
    {
        /*while(p!=NULL)
        {
            if(strcmp(p->data.id,s)==0)
                {
                    break;
                }
                p=p->next;
        }
        return (p->data.stu_score);*/
	
		       if(strcmp(p->data.id,s)==0&&flag==1)
			         return (p->data.stu_score);
			         
			    else if(strcmp(p->data.name,s)==0&&flag==2)
				{
					   
				     return (p->data.stu_score);
					   
				}
			     else
				    return (Score_search(s,p->next,flag));//遞歸

			   
    }

}
LIST* Delet_Score(char* s,LIST *tr,int flag)//PS:刪除成績函數的封裝
{
    LIST *p;
    p=tr->next;
    if(p==NULL)
    {
        printf("鏈表有誤,成績刪除失敗!\n");
          return 0;
    }
    else
    {     if(flag==1)
        {
		while(p!=NULL)
        {
            if(strcmp(p->data.id,s)==0)
            {
                p->data.stu_score.c_language=0;
                p->data.stu_score.english=0;
                p->data.stu_score.math=0;
                p->data.stu_score.total_sum=0;
                p->data.stu_score.ave=0.0;
                break;
            }
            p=p->next;
        }
		}
		  if(flag==2)
        {
		while(p!=NULL)
        {
            if(strcmp(p->data.id,s)==0)
            {
                p->data.stu_score.c_language=0;
                p->data.stu_score.english=0;
                p->data.stu_score.math=0;
                p->data.stu_score.ave=0.0;
                break;
            }
            p=p->next;
        }
		}
		
        if(p==NULL)
            return NULL;
        else
            return tr;
    }
}
LIST* Delet_infor(char *st,LIST *ht,int flag)//PS:學生信息刪除函數的封裝
{
     LIST*p;
     LIST*tem;
     p=ht->next;
     if(p==NULL)
        {
            printf("鏈表錯誤!\n");
             return NULL;
        }
      else
      {
         while(p!=NULL)
         {
               if(strcmp(p->data.id,st)==0&&flag==1)
                  {
                      tem=p;
                       break;
                  }
			   if(strcmp(p->data.name,st)==0&&flag==2)
                  {
                      tem=p;
                       break;
                  }
			      
                 p=p->next;

           }
    while(strcmp(p->data.id,st)!=0&& p->next!=NULL)//PS:循環查找要刪除的節點
    {
        tem=p;
        p=p->next;
    }
    if( strcmp(p->data.id,st)==0)//找到了一個節點的num和num相等
    {
        if(p == ht->next)//找到的節點是頭節點後面的一個節點
        {
            ht->next= p->next;
        }
        else
        {
            tem->next=p->next;
        }
        return ht;

      }
    else
        return ht=NULL;
     }

}
LIST *Re_score(char *sr,SCORE R_score,LIST *h,int flag)//PS:成績修改函數的封裝
{
    //LIST *q;
    //q=h;
    LIST *p;
    p=h->next;
    if(p==NULL)
            {
                     printf("鏈表發生錯誤,成績修改失敗!\n");
                       return p;
            }
        else
        {
            while(p!=NULL)
            {   
                if(strcmp(p->data.id,sr)==0&&flag==1)
                {
                   break;
                }
                if(strcmp(p->data.id,sr)==0&&flag==2)
                {
                	break;
				}
                p=p->next;
              }

                
			  p->data.stu_score= R_score;

        }
        return  h;
}
void infor_show(LIST*h)//PS:學生信息全部輸出函數的封裝
{
	LIST *p;
	int i;
	p=h->next;
	if(p==NULL)
		printf("鏈表發生錯誤!!!\n");
	 else
	 {
		 while(p!=NULL)
		 {    printf("學號%s的學生信息:\n",p->data.id);
			 printf("姓名:%s 性別:%s 專業:%s 入學年份:%d\n",p->data.name,p->data.sex,p->data.pro,p->data.year);
			    printf("該學生的選擇的選修課爲:\n");
                 for(i=0;i<N;i++)
			       printf("%s\t",p->data.stu_lesson[i]);
		         p=p->next;
				 printf("\n");
		 }
		    
     }
}
void Show_infor(char *S_s,LIST*h,int flag)//PS:信息查詢輸出函數的封裝
{
	LIST*p;
	p=h->next;
	int i;
	if(p==NULL)
		printf("鏈表出現錯誤!!!\n");
	else
	{
		while(p!=NULL)
		{
			if(strcmp(p->data.id,S_s)==0&&flag==1)
			        break;
		    if(strcmp(p->data.id,S_s)==0&&flag==2)
			        break;
			      p=p->next;

		}
		  printf("學生的姓名 性別 專業 入學年份\n");
		  printf("%s %s %s %d\n",p->data.name,p->data.sex,p->data.pro,p->data.year);
          printf("該學生的選擇的選修課爲:\n");
		              for(i=0;i<N;i++)
		     	         printf("%s\t",p->data.stu_lesson[i]);
	}
}
int Elect_les(STU lesson[])//PS:選修課的錄入
{
	        int i,j;
			

             printf("*******************************\n");
			 printf("*     1---語言文學             *\n");
             printf("*     2---人文地理             *\n");
             printf("*     3---思想政治             *\n");
             printf("*     4---口才藝術             *\n");
			 printf("*     5---經濟管理             *\n");
			 printf("*     6---國際貿易             *\n");
			 printf("*     7---職場禮儀             *\n");
			 printf("*******************************\n");
			 for(i=0;i<M;i++)
			 {
				printf("請輸入第%d位學生的選修的課程:\n",i+1);
			   for(j=0;j<N;j++)
				 {

                   scanf("%s",lesson[i].stu_lesson[j]);
				   getchar();
				 }
			 }

				 if(i==M)
					 return 1;
				 else
					 return 0;

}
LIST* Insert_infor(LIST *in_infor)//PS:學生信息添加函數的封裝 
{
	LIST *p;
	LIST *st;
	LIST *q;
     int s=0;
	double ave=0;
	st=in_infor->next;
    p=(LIST*)malloc(sizeof (LIST));
	printf("請輸入要填加的學生的學號 姓名 性別 專業 入學年份:\n");
	  scanf("%s %s %s %s %d", p->data.id, p->data.name, p->data.sex,
              p->data.pro,&p->data.year);
	    printf("請輸入要填加的學生的成績:\n");
            scanf("%d %d %d",&p->data.stu_score.math,&p->data.stu_score.english,
                       &p->data.stu_score.c_language);

        s=p->data.stu_score.c_language+p->data.stu_score.english+p->data.stu_score.math;
              p->data.stu_score.ave=s/3.0;
	    //st=in_infor->next;
		if(st==NULL)
		{
			 printf("鏈表發生錯誤!\n");
		     return NULL;
		}
		 else
		 {
			 /*in_infor->next=p;
		      p->next=st;
			  return in_infor;*/
			  while(st!=NULL)
              {
                  if(st->data.stu_score.ave<p->data.stu_score.ave)
                         break;
                       st=st->next;
              }
              q=in_infor;
                while(q->next!=st)
                    q=q->next;
                   q->next=p;
                   p->next=st;
                   return in_infor;

	 }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章