C链表和文件操作实战--图书管理系统


这个貌似是我们大一的C语言课程设计,当时是从网上下的代码,然后修改了一下.现在想想好惭愧,哎我好想重新开始,以前自己的确失去了好多东西,希望今后的自己好好......

时间过的真快,现在都大三了,马上也要滚蛋了,再滚蛋之前好好修炼内功.于是我又重新拾起了久违的C语言,用了几天时间认真看完了Programming in c,个人觉得这本书讲得真好,

收益亮多.回想起大一学的C语言,我们用的教材是学校自己编的,往事不堪回首啊....记得上学期我们学习数字信号处理,我么用的也是学校自己编的书,当时上课最有特色的地方就是:我们那个老师每节课都要花好长时间纠正书里的错误,他经常说的一句话是-还好当时我没参加编写这本书,不然还不被骂死.......

我始终想不明白的是有那么多经典的好书,学校不用,非要自己去编那么操蛋的书.这学期我们学习XML应用教材,当时老师说本来想用那本书的,觉得很好.最后学校不同意,理由竟是:那本是高职的书,我们是本科....我听完泪流满面啊.我们的大学,我们的老师,你们什么时候才能不浮躁???

哎,本来我不想说什么,但已经到这个地步了,就再多废话几句吧.我们学了微机原理,老师没在课上编过.调试过一个程序,那么经典的debug工具都不提...;我们学了计算机网络,天天讲各种包,老师从没在课上用抓包软件抓过一个包,分析过一个包...;我们学了组网技术,老师给我的模拟器不知道是哪一年的,ospf都不支持,天天说cisco,没介绍过packettracer,  dynamips...泪流满面啊,我们的学校很差,但真的差到这个地步了吗? 老师们,你们每天都在想什么了,能不浮躁吗? 天天说上课学生不听讲,天天说上课人数少,为什么呀?我们学生是有问题,但你们呢? 你们真的对得起你们拿的工资吗?

哎,话多了.一个学生的牢骚而已....

stop,不说了,呵呵.

主要是想起了大一做的课程设计,所以自己动手做了下,不过我这个事简化版,呵呵,温故而知新.

  1. #include <stdio.h>   
  2. #include <string.h>   
  3. #include <stdlib.h>   
  4. #include <conio.h>   
  5. #define  books "f:\\books.txt"   
  6. #define booksbak  "f:\\booksbak.txt"   
  7. struct bookinfo  
  8. {  
  9.     char isbn[20];  
  10.     char title[30];  
  11.     char author[20];  
  12.     int count;  
  13. };  
  14.   
  15. struct book  
  16. {  
  17.     struct bookinfo onebook;  
  18.     struct book *next;  
  19. };  
  20.   
  21. struct book *searchBook ( struct book *listptr,char isbn[])  
  22. {  
  23.     while(listptr!=(struct book *)0)  
  24.         if (strcmp(listptr->onebook.isbn,isbn)==0)  
  25.             return listptr;  
  26.         else  
  27.             listptr=listptr->next;  
  28.         return (struct book *)0;  
  29. }  
  30.   
  31.   
  32. void MainSearchbook(struct book *firstptr)  
  33. {  
  34.     struct book *ptr;  
  35.     char isbnno[20];  
  36.     printf("请输入ISBN:");  
  37.     scanf("%s",&isbnno);  
  38.     ptr=searchBook(firstptr,isbnno);  
  39.     if (ptr!=(struct book *)0)  
  40.     {  
  41.         printf("找到了!!!\n");  
  42.         printf("ISBN:%s\n",ptr->onebook.isbn);  
  43.         printf("Title:%s\n",ptr->onebook.title);  
  44.         printf("Author:%s\n",ptr->onebook.author);  
  45.     }  
  46.     else  
  47.         printf("sorry,not found!!!\n");  
  48. }  
  49.   
  50.   
  51.   
  52. int addBook(struct book *listptr,struct bookinfo note)  
  53. {  
  54.     while(listptr->next!=0)  
  55.         listptr=listptr->next;  
  56.     listptr->next=(struct book *)malloc(sizeof(struct book));  
  57.     listptr->next->onebook=note;  
  58.     listptr->next->next=0;  
  59.     return 0;  
  60.       
  61. }  
  62.   
  63. void MainAdd(struct book *listptr,FILE *fp)  
  64. {  
  65.     int ok;  
  66.     struct bookinfo note;  
  67.     printf("请输入ISBN:");  
  68.     scanf("%s",¬e.isbn);  
  69.     printf("请输入Title:");  
  70.     scanf("%s",¬e.title);  
  71.     printf("请输入Author:");  
  72.     scanf("%s",¬e.author);  
  73.     ok=addBook(listptr,note);  
  74.     if (ok==0)  
  75.     {     
  76.         //将加入的图书写到文件中保存   
  77.         fprintf(fp,"\n%s %s %s %d",note.isbn,note.title,note.author,0);  
  78.         printf("添加图书成功!!!\n");  
  79.     }  
  80.     else  
  81.         printf("添加图书失败!!!\n");  
  82. }  
  83.   
  84.   
  85. int removeBook(struct book *listptr,char isbn[])  
  86. {  
  87.     while(listptr->next!=(struct book *)0)  
  88.     {  
  89.         if (strcmp(listptr->next->onebook.isbn,isbn)==0)  
  90.         {  
  91.             listptr->next=listptr->next->next;  
  92.             return 0;  
  93.         }  
  94.         else  
  95.             listptr=listptr->next;  
  96.     }  
  97.           
  98.         return -1;  
  99. }  
  100.   
  101. void MainRemove(struct book *listptr,FILE *fp)  
  102. /************************************************************************/  
  103. /* 删除书籍函数,通过ISBN删除链表节点,同时删除文件中对应信息              */  
  104. /* 删除文件中一行,用的是笨方法,把需要的信息写到新文件,删除旧文件,重命名..*/  
  105. /************************************************************************/  
  106. {  
  107.     char isbnno[20];  
  108.     int ok;  
  109.     struct bookinfo onebook;  
  110.     printf("请输入ISBN:");  
  111.     scanf("%s",&isbnno);  
  112.     ok=removeBook(listptr,isbnno);  
  113.     if (!ok)  
  114.     {  
  115.         FILE *fpbak;  
  116.         if ((fpbak=fopen(booksbak,"a+"))==NULL)  
  117.             printf("文件打开失败!!!\n");  
  118.         fseek(fp,0,SEEK_SET);   //移到文件开始   
  119.         while((fscanf(fp,"%s %s %s %d\n",&onebook.isbn,&onebook.title,&onebook.author,&onebook.count))!=EOF)  
  120.         {  
  121.             if (strcmp(onebook.isbn,isbnno)!=0)  
  122.             {  
  123.                 fprintf(fpbak,"%s %s %s %d\n",onebook.isbn,onebook.title,onebook.author,onebook.count);  
  124.             }  
  125.         }  
  126.         fclose(fp);  
  127.         fclose(fpbak);  
  128.         if (remove(books))   //删除失败返回非0   
  129.         {  
  130.             printf("删除文件失败!!!\n");  
  131.             return ;  
  132.         }  
  133.         else  
  134.             if (rename(booksbak,books))  //重命名失败返回非0值   
  135.             {  
  136.                 printf("重命名失败!!!\n");  
  137.                 return ;  
  138.             }  
  139.   
  140.         printf("删除成功!!!\n");  
  141.     }  
  142.     else  
  143.         printf("查无此书!!!");  
  144. }  
  145.   
  146. int  choice(void)  
  147. {     
  148.     int c;  
  149.     printf("1.查看图书\n");  
  150.     printf("2.添加图书\n");  
  151.     printf("3.删除图书\n");  
  152.     printf("4.退出程序\n");  
  153.     printf("请选择序号:");  
  154.     return c=getchar();  
  155.     //return c=getche();   
  156.     printf("\n\n");  
  157.   
  158. }  
  159.   
  160. int addEntry(FILE *fp,struct book *firstptr)  
  161. /************************************************************************/  
  162. /*        主要用来加载文件中存放的图书信息                              */  
  163. /************************************************************************/  
  164.   
  165. {  
  166.     struct bookinfo onebook;  
  167.     while((fscanf(fp,"%s %s %s %d\n",&onebook.isbn,&onebook.title,&onebook.author,&onebook.count))!=EOF)  
  168.     {  
  169.         while(firstptr->next!=0)  
  170.             firstptr=firstptr->next;  
  171.         firstptr->next=(struct book *)malloc(sizeof(struct book));  
  172.         firstptr->next->onebook=onebook;  
  173.         firstptr->next->next=0;  
  174.     }  
  175.     return 0;  
  176.   
  177. }  
  178.   
  179.   
  180. int main(int argc,char *argv[])  
  181. {  
  182.     int ch;  
  183.     struct book first;  
  184.     strcpy(first.onebook.isbn,"123456");  
  185.     strcpy(first.onebook.title,"Programming C");  
  186.     strcpy(first.onebook.author,"yhb");  
  187.     first.next=0;  
  188.     struct book *firstptr=&first;   //链表头指针   
  189.     FILE *fp;  
  190.     if ((fp=fopen(books,"a+"))==NULL)  
  191.         printf("文件打开失败!!!");  
  192.     addEntry(fp,firstptr);  
  193.     while(1)  
  194.     {  
  195.         system("CLS");   //清屏   
  196.         /************************************************************************/  
  197.         /*          想想这里为什么要清空缓冲区?                                 */  
  198.         /*由于上一次(choice函数)的getchar(),还有一个'\n'留在缓冲区....          */  
  199.         /*可以把这句话注释掉看看,没有这句话会遇到麻烦                           */  
  200.         /*如果不用fflush,可以将上面的getchar()换成getche()                      */  
  201.         /*比较getchar(),getch(),getche()......                                  */  
  202.         /************************************************************************/  
  203.         fflush(stdin);   
  204.         ch=choice()-48;  
  205.         switch (ch)  
  206.         {  
  207.         case 1:  
  208.             MainSearchbook(firstptr);  
  209.             break;  
  210.         case 2:  
  211.             MainAdd(firstptr,fp);  
  212.             break;  
  213.         case 3:  
  214.             MainRemove(firstptr,fp);  
  215.             break;  
  216.         case 4:  
  217.             printf("谢谢使用...\n");  
  218.             exit(0);  
  219.         default:  
  220.             printf("请输入正确序号!");  
  221.         }  
  222.         system("PAUSE");  
  223.     }  
  224.     return 0;  
  225. }  

bookinfo结构体的count成员,本来是想做最近热门图书的, 通过用户查看书籍,统计书籍浏览的次数.发现更新文件中的数据很麻烦,于是就取消了.呵呵

books.txt里面的数据大致如下所示:可以直接通过编辑books.txt增加书籍.

  1. 123 python yhb 2  
  2. 456 linux lwy 4  
  3. 789 c yl 5  

文件操作用fread,fwrite应该更方便点.

相关内容