链表-简易学生成绩管理

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


看到论坛里总有学生在寻求链表实现学生成绩管理的帖子,动手写一个,也算学习,虽然功能很少,以前还真没写过完整的,写的比较粗糙,比较简单,不合适的地方欢迎指正。

 

LinkTable.h

[cpp]  view plain copy
  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

[cpp]  view plain copy
  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方法

[cpp]  view plain copy
  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. }  

 

运行结果:

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值