C語言連結串列實現的簡易學生成績管理系統

zzobin發表於2011-12-17

          當初準備用陣列去實現,發現比較麻煩,對比下,使用連結串列去實現。效果還不錯。實現查詢,修改,刪除,插入,求總和,平均值,排名功能。在TC上執行通過。

         程式碼及註釋如下:

       

                /*--------------------------------------------*/
             /*The name of program:the system management of score*/
               /*---------------------------------------------*/
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#define Max 20
struct  List                     /*節點結構宣告*/
{
     int Number;
     char Name[Max];
     int Chinese;
     int English;
     int Math;
     int Physical;
     int Chemsitry;
     struct  List *next;
};
     typedef struct List Node;
     typedef Node *Link;
struct  student                    /*結構體陣列宣告*/
{
     int Number_;
     char Name_[Max];
     int Chinese_;
     int English_;
     int Math_;
     int Physical_;
     int Chemsitry_;
}student[10]={{0,"Alan",85,90,98,93,86},{1,"Danie",76,70,80,74,75},{2,"Helen",95,98,96,95,94},{3,"Bill",65,60,80,70,74},{4,"Peter",79,65,86,79,80}};
int M=5;       /*初始學生總數*/

      /*釋放連結串列*/
void Free_List(Link Head)
{
     Link Pointer;
     while(Head!=NULL)
     {
         Pointer=Head;
         Head=Head->next;/*消掉下一個節點*/
         free(Pointer);
     }
}
        /*建立連結串列*/
Link Create_List(Link Head)
{
     Link New;
     Link Pointer;
     int i;
     Head=(Link)malloc(sizeof(Node));/*記憶體配置*/
     if(Head==NULL)
     printf("allocate failure!\n");/*配置失敗*/
     else
     {
 Head->Number=student[0].Number_;  /*初始化*/
 strcpy(Head->Name,student[0].Name_);
 Head->Chinese=student[0].Chinese_;
 Head->English=student[0].English_;
 Head->Math=student[0].Math_;
 Head->Physical=student[0].Physical_;
 Head->Chemsitry=student[0].Chemsitry_;
        Head->next=NULL;
 Pointer=Head;
 for(i=1;i<10;i++)
 {
    New=(Link)malloc(sizeof(Node));
    New->Number=student[i].Number_;
    strcpy(New->Name,student[i].Name_);
    New->Chinese=student[i].Chinese_;
    New->English=student[i].English_;
    New->Math=student[i].Math_;
    New->Physical=student[i].Physical_;
    New->Chemsitry=student[i].Chemsitry_;
    New->next=NULL;
    Pointer->next=New;
    Pointer=New;
 }
     }  
           return Head;
}

           /*查詢成績*/
Link List_Search(int Key,Link Head)
{
      Link Pointer;
      Pointer=Head;
      while(Pointer!=NULL)
      {
          if(Pointer->Number==Key)
          {
             printf("your Number is:%d\n",Pointer->Number);
             printf("your Name   is:%s\n",Pointer->Name);
             printf("your Chinese is:%d\n",Pointer->Chinese);
             printf("your English is:%d\n",Pointer->English);
             printf("your Math   is:%d\n",Pointer->Math);
             printf("your Physical is:%d\n",Pointer->Physical);
             printf("your Chemsitry is:%d\n",Pointer->Chemsitry);
          }
             Pointer=Pointer->next;
      }
             return Head;
}

             /*修改成績*/
Link List_Modify(char *s,int Key,Link Head)   
{
        Link Pointer;
        int newscore1,newscore2,newscore3,newscore4,newscore5;
        Pointer=Head;
        while(Pointer!=NULL)
        {
            if(Pointer->Number==Key)
           {
             if(strcmp(s,"Chinese")==0)
              {
                printf("**please input new score:");
                scanf("%d",&newscore1);
                student[Key].Chinese_=newscore1;
                break;
              }
             else if(strcmp(s,"English")==0)
              {  
                printf("**please input new score:");
                scanf("%d",&newscore2);
                student[Key].English_=newscore2;
                break;
              }
             else if(strcmp(s,"Math")==0)
              {
                printf("**please input new score:");
                scanf("%d",&newscore3);
                student[Key].Math_=newscore3;
                break;
              }
             else if(strcmp(s,"Physical")==0)
              {
                printf("**please input new score:");
                scanf("%d",&newscore4);
                student[Key].Physical_=newscore4;
                break;
              }
             else if(strcmp(s,"Chemsitry")==0)
              {
                printf("**please input new score:");
                scanf("%d",&newscore5);
                student[Key].Chemsitry_=newscore5;
                break;
              }
            }
              Pointer=Pointer->next;
         }
              return Head;
}
 
         /*刪除某人的成績*/         
Link Delete_List(int Key,Link Head)
{
      Link Pointer;
      Link Back;
      Pointer=Head;
      while(1)
      {
         if(Pointer->next==NULL)
         {
            printf("Not Found!\n");
            break;
         }
         if(Head->Number==Key)
         {
            Head=Pointer->next;
            free(Pointer);
            break;
         }
         Back=Pointer;
         Pointer=Pointer->next;
         if(Pointer->Number==Key)
         {
            Back->next=Pointer->next;
            free(Pointer);
            break;
         }
     }
          
         return Head;
}

        /*插入某人的成績*/
Link Insert_List(int Key,Link Head,Link New)
{
       Link Pointer;
       Pointer=Head;
       while(1)
       {
           if(Pointer==NULL)
           {
             New->next=Head;
             Head=New;
             break;
           }
           if(Pointer->Number==Key)
           {
             New->next=Pointer->next;
             Pointer->next=New;
              break;
           }
        

          Pointer=Pointer->next;
       }
          return Head;
}

       /*求某人總分*/
int total(int Key,Link Head)
{
      Link Pointer;
      int total_;
      Pointer=Head;
      while(Pointer!=NULL)
      {
         {
            if(Pointer->Number==Key)
            total_=Pointer->Chinese+Pointer->English+Pointer->Math+Pointer->Physical+Pointer->Chemsitry;
         }
            Pointer=Pointer->next;
      }
            return total_;
}

     /*求某人平均分*/
float average(int Key,Link Head)
{
     float average_;
     average_=total(Key,Head)/5;
     return  average_;
}

 /*求全體平均分*/

float Average(int M,Link Head)
{   
    int Key;
    float Average_=0.0;
    for(Key=0;Key<M;Key++)
    {
       Average_+=total(Key,Head)/M;
    }
        return  Average_;
}

     /*某人排名*/
int paiming(int Key,int M,Link Head)
{  
   int i,j,k,c;
   int a[10]={0};
   c=total(Key,Head);
   for(i=0;i<M;i++)
   {
     a[i]=total(i,Head);
   }
    j=M;
   for(k=0;k<M;k++)
   {
      if(c>a[k])
      j--;
   }
      return  j;
}


      /*主函式*/
void main()
{
    Link Head;
    Link New;
    int selection,total_,Key1,paiming_;
    int Index1,Index2,Index3,Index4,Index5,Index6;
    float average_,Average_;
    char *subject;
                                           
   
    while(1)                                 /*使用者選單*/
    {
        printf("=========================\n");
        printf("==Simple student score management system==\n");
        printf("==1.Search student score==\n");
        printf("==2.Modify student score==\n");
        printf("==3.Delete student score==\n");
        printf("==4.Insert student score==\n");
        printf("==5.The total score of someone==\n");
        printf("==6.The average score of someone==\n");
        printf("==7.paiming                   ==\n");
        printf("==8.The average of class==\n");
        printf("==9.Quit                ==\n");
        printf("=========================\n");
        printf("Please input your choose:");
        scanf("%d",&selection);       /*度取使用者選項*/
        Head=Create_List(Head);
        if(Head!=NULL)
       {
        switch(selection)
        {
            case 1:
           
            printf("**Please input the student number:");
            scanf("%d",&Index1);
            Head=List_Search(Index1,Head);/*呼叫List_Search函式*/
            Free_List(Head);              /*呼叫Free_List函式*/
            break;

            case 2:
           
            printf("**Please input the student number:");
            scanf("%d",&Index2);
            printf("Which subject do you want to Modify:");
            scanf("%s",subject);
            Head=List_Modify(subject,Index2,Head);/*呼叫List_Modify函式*/
            Free_List(Head);                      /*呼叫Free_List函式*/
            break;

            case 3:
           
            printf("While number do you want to delete:");
            scanf("%d",&Index3);
            Head=Delete_List(Index3,Head);   /*呼叫Delete_List函式*/
            Free_List(Head);                 /*呼叫Free_List函式*/
            student[Index3].Number_=-1;        /*置刪除學生學號不存在*/
            strcpy(student[Index3].Name_," ");/*名字為空*/
            student[Index3].Chinese_=0;       /*分數為0*/
            student[Index3].English_=0;
            student[Index3].Math_=0;
            student[Index3].Physical_=0;
            student[Index3].Chemsitry_=0;
            M--;           /*刪除後人數減1*/
            break;
     
            case 4:
           
            New=(Link)malloc(sizeof(Node));   /*記憶體配置*/
            printf("**Please input the student number:");
            scanf("%d",&New->Number);
            printf("Please input student name:");
            scanf("%s",New->Name);
            printf("Please input the score of Chinese  ,English   ,Math   ,Physical   ,Chemsitry   :");
            scanf("%d  %d  %d   %d   %d",&New->Chinese,&New->English,&New->Math,&New->Physical,&New->Chemsitry); 
            printf("Please input the number for Insert:");
            scanf("%d",&Key1);
            Head=Insert_List(Key1,Head,New);   /*呼叫Insert_List函式*/
            Free_List(Head);                   /*呼叫Free_List函式*/
            student[Key1].Number_=New->Number;
            strcpy(student[Key1].Name_,New->Name);
            student[Key1].Chinese_=New->Chinese;
            student[Key1].English_=New->English;
            student[Key1].Math_=New->Math;
            student[Key1].Physical_=New->Physical;
            student[Key1].Chemsitry_=New->Chemsitry;
            M++;       /*插入後人數加1*/
            break;

            case 5:
          
            printf("**Please input the student number:");
            scanf("%d",&Index4);
            total_=total(Index4,Head);   /*呼叫total函式*/
            Free_List(Head);             /*呼叫Free_List函式*/
            printf("the total of the student is:%d",total_);
            break;
     
            case 6:
           
            printf("**Please input the student number:");
            scanf("%d",&Index5);
            average_=average(Index5,Head); /*呼叫average函式*/
            Free_List(Head);               /*呼叫Free_List函式*/
            printf("the average of the student is:%f",average_);
            break;

            case 7:
           
            printf("**Please input the student number:");
            scanf("%d",&Index6);
            paiming_=paiming(Index6,M,Head);  /*呼叫paiming函式*/
            Free_List(Head);    /*呼叫Free_List函式*/
            printf("the paiming of the student is:%d",paiming_);
            break;

            case 8:
           
            Average_=Average(M,Head);   /*呼叫Average函式*/
            Free_List(Head);  /*呼叫Free_List函式*/
            printf("the average of the whole class is:%f",Average_);
            break;

            case 9:
            exit(1);  /*退出使用者選單*/
            break;
         }
       }
    }
}
   

 

相關文章