鏈表-簡易學生成績管理

原文:http://blog.csdn.net/bdmh/article/details/6112631


看到論壇裏總有學生在尋求鏈表實現學生成績管理的帖子,動手寫一個,也算學習,雖然功能很少,以前還真沒寫過完整的,寫的比較粗糙,比較簡單,不合適的地方歡迎指正。

 

LinkTable.h

  1. class StudentInfo  
  2. {  
  3. public:  
  4.     char code[6];  
  5.     float chinesescore;  
  6.     float mathscore;  
  7.     StudentInfo* next;  
  8.     StudentInfo* prior;  
  9. };  
  10.   
  11. //添加學生信息,如果prior爲NULL,則newinfo爲頭結點  
  12. StudentInfo* AddInfo(StudentInfo* prior,StudentInfo* newinfo);  
  13. //根據編號刪除學生信息  
  14. bool DelStudentInfo(char* code,StudentInfo*& pHead);  
  15. //根據編號查找學生信息  
  16. StudentInfo* FindStudentInfoByCode(char* code,StudentInfo* pHead);  
  17. //修改學生成績信息  
  18. void ModifyStudentInfo(StudentInfo* p);  
  19. //從頭結點開始打印學生信息  
  20. void PrintStudentInfo(StudentInfo* pHead);  
  21. //對指定的節點輸入學生信息  
  22. void InputInfo(StudentInfo* pInfo);  
  23. //獲取最後一個節點  
  24. StudentInfo* GetLastStudentInfo(StudentInfo* pHead);  
  25. //保存鏈表到文件,順序保存  
  26. void SaveStudentInfoToFile(char* filename,StudentInfo* pHead);  
  27. //從文件中讀取鏈表  
  28. void LoadStudentInfoFromFile(char* filename,StudentInfo*& pHead);  
  29. //操作提示  
  30. void ShowOperateInfo(void);  

 

LinkTable.cpp

  1. #include "LinkTable.h"  
  2. #include <stdio.h>  
  3. #include <string.h>  
  4.   
  5. //添加學生信息,如果prior爲NULL,則newinfo爲頭結點  
  6. StudentInfo* AddInfo(StudentInfo* prior,StudentInfo* newinfo)  
  7. {  
  8.     if (!prior)  
  9.     {  
  10.         newinfo->next = NULL;  
  11.         newinfo->prior = NULL;  
  12.     }  
  13.     else  
  14.     {  
  15.         prior->next = newinfo;  
  16.         newinfo->prior = prior;  
  17.         newinfo->next = NULL;  
  18.     }  
  19.     memset(newinfo->code,0,sizeof(newinfo->code));  
  20.     InputInfo(newinfo);  
  21.     return newinfo;  
  22. }  
  23.   
  24. //根據編號刪除學生信息  
  25. bool DelStudentInfo(char* code,StudentInfo*& pHead)  
  26. {  
  27.     StudentInfo* pDel = FindStudentInfoByCode(code,pHead);  
  28.     if (pDel == NULL)  
  29.         return false;  
  30.     else  
  31.     {  
  32.         if (pDel == pHead)  
  33.             pHead = pDel->next;  
  34.         else  
  35.         {             
  36.             pDel->prior->next = pDel->next;  
  37.             if (pDel->next != NULL)  
  38.                 pDel->next->prior = pDel->prior;  
  39.         }  
  40.         delete pDel;  
  41.         pDel = NULL;  
  42.         return true;  
  43.     }  
  44. }  
  45.   
  46. //根據編號查找學生信息  
  47. StudentInfo* FindStudentInfoByCode(char* code,StudentInfo* pHead)  
  48. {  
  49.     StudentInfo* p = pHead;  
  50.     while (p != NULL)  
  51.     {  
  52.         if (strcmp(p->code,code) == 0)  
  53.         {  
  54.             return p;  
  55.         }  
  56.         p = p->next;  
  57.     }  
  58.     return NULL;  
  59. }  
  60. //修改學生成績信息  
  61. void ModifyStudentInfo(StudentInfo* p)  
  62. {  
  63.     printf("%s號學生當前信息:語文:%f,數學:%f/n",p->code,p->chinesescore,p->mathscore);  
  64.     printf("%s","請輸入修改後的語文成績:");  
  65.     scanf("%f",&p->chinesescore);  
  66.     printf("%s","請輸入修改後的數學成績:");  
  67.     scanf("%f",&p->mathscore);  
  68.     printf("修改完畢,%s號學生最新信息:語文:%f,數學:%f/n",p->code,p->chinesescore,p->mathscore);  
  69. }  
  70.   
  71. //從頭結點開始打印學生信息  
  72. void PrintStudentInfo(StudentInfo* pHead)  
  73. {  
  74.     StudentInfo* p = pHead;  
  75.     while (p!=NULL)  
  76.     {  
  77.         printf("%s號學生:語文:%f,數學:%f/n",p->code,p->chinesescore,p->mathscore);  
  78.         p = p->next;  
  79.     }  
  80. }  
  81.   
  82. //對指定的節點輸入學生信息  
  83. void InputInfo(StudentInfo* pInfo)  
  84. {  
  85.     printf("%s","請輸入學生編號:");  
  86.     scanf("%s",pInfo->code);  
  87.     printf("%s","請輸入語文成績:");  
  88.     scanf("%f",&pInfo->chinesescore);  
  89.     printf("%s","請輸入數學成績:");  
  90.     scanf("%f",&pInfo->mathscore);  
  91. }  
  92.   
  93. //獲取最後一個節點  
  94. StudentInfo* GetLastStudentInfo(StudentInfo* pHead)  
  95. {  
  96.     if (pHead == NULL)  
  97.         return NULL;  
  98.     StudentInfo* p = pHead;  
  99.     while (p)  
  100.     {  
  101.         if (p->next == NULL)  
  102.             return p;  
  103.         p = p->next;  
  104.     }  
  105. }  
  106.   
  107. //保存鏈表到文件,順序保存  
  108. void SaveStudentInfoToFile(char* filename,StudentInfo* pHead)  
  109. {  
  110.     FILE* f;  
  111.     f= fopen(filename,"w");  
  112.     StudentInfo* p = pHead;  
  113.     while (p)  
  114.     {  
  115.         fwrite(p->code,sizeof(char),sizeof(p->code),f);  
  116.         fwrite(&p->chinesescore,sizeof(float),1,f);  
  117.         fwrite(&p->mathscore,sizeof(float),1,f);  
  118.         //fwrite("/n",sizeof(char),1,f);  
  119.         p=p->next;  
  120.     }  
  121.     //fwrite("/0",sizeof(char),1,f);  
  122.     fclose(f);  
  123. }  
  124.   
  125. //從文件中讀取鏈表  
  126. void LoadStudentInfoFromFile(char* filename,StudentInfo*& pHead)  
  127. {  
  128.     FILE* f;  
  129.     f= fopen(filename,"r");  
  130.     if (f == NULL)  
  131.         return;  
  132.     fseek(f, 0, SEEK_SET);   
  133.     StudentInfo* p = NULL;  
  134.     StudentInfo* newp = NULL;  
  135.     int c;  
  136.     //c = fgetc(f);  
  137.     while (!feof(f))  
  138.     {  
  139.         //因爲不能完全依賴feof判斷文件是否到結尾,所以用fgetc的返回值來判斷,  
  140.         //當讀取到結尾時,feof實際並沒有獲得到達結尾的信息,所以這裏用fgetc繼續讀取一個字符,來判斷是否結束  
  141.         //如果不是-1(結尾),那麼回退一個文件位置,如果是-1,退出  
  142.         c = fgetc(f);  
  143.         if (c != -1)  
  144.             fseek(f, -1, SEEK_CUR);  
  145.         else  
  146.             return;  
  147.         if (pHead == NULL)  
  148.         {  
  149.             pHead = new StudentInfo;  
  150.             pHead->next = NULL;  
  151.             pHead->prior = NULL;  
  152.             memset(pHead->code,0,sizeof(pHead->code));  
  153.             fread(pHead->code,sizeof(char),sizeof(pHead->code),f);  
  154.             fread(&pHead->chinesescore,sizeof(float),1,f);  
  155.             fread(&pHead->mathscore,sizeof(float),1,f);  
  156.             p = pHead;  
  157.         }  
  158.         else  
  159.         {  
  160.             newp = new StudentInfo;  
  161.             newp->next = NULL;  
  162.             newp->prior = NULL;  
  163.             newp->prior = p;  
  164.             memset(newp->code,0,sizeof(newp->code));  
  165.             fread(newp->code,sizeof(char),sizeof(newp->code),f);  
  166.             fread(&newp->chinesescore,sizeof(float),1,f);  
  167.             fread(&newp->mathscore,sizeof(float),1,f);  
  168.             p->next = newp;  
  169.             p = newp;  
  170.         }     
  171.           
  172.     }  
  173.     fclose(f);  
  174. }  
  175.   
  176. //操作提示  
  177. void ShowOperateInfo(void)  
  178. {  
  179.     printf("%s/n","1:增加信息");  
  180.     printf("%s/n","2:刪除信息");  
  181.     printf("%s/n","3:查找信息");  
  182.     printf("%s/n","4:修改信息");  
  183.     printf("%s/n","5:保存信息");  
  184.     printf("%s/n","9:打印信息");  
  185.     printf("%s/n","0:結束");  
  186. }  

 

Main方法

  1. #include "LinkTable.h"  
  2.   
  3. int main()  
  4. {  
  5.     ShowOperateInfo();  
  6.     int i;  
  7.     //curInfo當前節點,用來添加節點用  
  8.     StudentInfo* curInfo = new StudentInfo;  
  9.     StudentInfo* pHead = NULL;    
  10.     LoadStudentInfoFromFile("e://link.txt",pHead);  
  11.     //將curInfo指向最後一個節點  
  12.     if (pHead != NULL)  
  13.         curInfo = GetLastStudentInfo(pHead);  
  14.     char code[10]={0};  
  15.     while (true)  
  16.     {  
  17.         printf("%s","請輸入操作號:");  
  18.         scanf("%d",&i);  
  19.         switch (i)  
  20.         {  
  21.             //退出  
  22.         case 0:  
  23.             exit(0);  
  24.             //添加  
  25.         case 1:  
  26.             if (pHead == NULL)  
  27.             {  
  28.                 pHead = new StudentInfo;      
  29.                 curInfo = pHead = AddInfo(NULL,pHead);  
  30.             }  
  31.             else  
  32.             {  
  33.                 StudentInfo* newInfo = new StudentInfo;  
  34.                 curInfo = AddInfo(curInfo,newInfo);  
  35.             }  
  36.             break;  
  37.             //刪除  
  38.         case 2:           
  39.             printf("%s","請輸入要刪除的學生編號:");  
  40.             scanf("%s",code);  
  41.             DelStudentInfo(code,pHead);  
  42.             curInfo = GetLastStudentInfo(pHead);  
  43.             break;  
  44.             //查找  
  45.         case 3:  
  46.             printf("%s/n","請輸入要查找的學生編號:");  
  47.             scanf("%s",code);  
  48.             {  
  49.                 StudentInfo* p = FindStudentInfoByCode(code,pHead);  
  50.                 if (p == NULL)  
  51.                     printf("編號%s不存在",code);  
  52.                 else  
  53.                 {  
  54.                     printf("%s號學生:語文:%f,數學:%f/n",p->code,p->chinesescore,p->mathscore);  
  55.                 }  
  56.             }  
  57.             break;  
  58.             //修改  
  59.         case 4:  
  60.             printf("%s/n","請輸入要修改的學生編號:");  
  61.             scanf("%s",code);  
  62.             {  
  63.                 StudentInfo* p = FindStudentInfoByCode(code,pHead);  
  64.                 if (p == NULL)  
  65.                     printf("編號%s不存在",code);  
  66.                 else  
  67.                 {  
  68.                     ModifyStudentInfo(p);  
  69.                 }  
  70.             }  
  71.             break;  
  72.         case 5:  
  73.             SaveStudentInfoToFile("e://link.txt",pHead);  
  74.             break;  
  75.             //打印  
  76.         case 9:  
  77.             PrintStudentInfo(pHead);  
  78.             break;  
  79.         default:  
  80.             exit(0);  
  81.         }  
  82.     }  
  83. }  

 

運行結果:

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