| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 4778 人关注过本帖
标题:图书馆管理系统之链表
只看楼主 加入收藏
hnuhsg1226
Rank: 9Rank: 9Rank: 9
来 自:中国
等 级:蜘蛛侠
威 望:2
帖 子:314
专家分:1314
注 册:2011-3-27
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:11 
图书馆管理系统之链表
输入那块应该有很大问题,请各路英雄指点迷津
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

struct books{
        char booknum[20];        //书号
        char bookname[20];       //书名
        char authorname[20];     //作者
        char cbs[20];            //出版社
        char price[5];           //价格
        struct books *next;
        struct books *prior;  
};

struct books *head;       //头结点
struct books *last;       //尾结点

int menu(void);    //菜单选项
void inputs(char *prompt,char *s,int count);//输入格式
void enter(void);                           //信息输入
void dls_store(struct books *i,struct books **head,struct books **last);//按书号大小建立链表
struct books *find(char *bn);                      //按书名检索
void search(void);                                 //按书名查找
void modify(void);                                 //修改信息
void display(struct books *p);                       //显示格式
void list(void);
void del(struct books **head,struct books **last);           //删除信息
void save(void);             //以文件形式保存信息
void load(void);              //从文件中载入信息


void inputs(char *prompt,char *s,int count)
{
     char p[255];
     do{
          printf(prompt);
          gets(p);
          if(strlen(p) > count)  //限定输入字符长度
          {
                printf("\nToo long\n");
          }
     }while(strlen(p) > count);      
     strcpy(s,p);
}

void enter(void)       //输入信息
{
    system("cls");//清屏
   
    struct books *p;   
    p=(struct books *)malloc(sizeof(struct books));
          char ch;         
          inputs("请输入书的编号:",p->booknum,20);
          inputs("请输入书名:",p->bookname,20);
          inputs("请输作者名:",p->authorname,20);
          inputs("请输入出版社:",p->cbs,20);
          inputs("请输入图书价格:",p->price,5);
          printf("\n");
          dls_store(p,&head,&last);
          free(p);
}
                  
void dls_store(struct books *i,struct books **head,struct books **last)   //按书号顺序插入表中
{
     struct books *p,*old;
     if((*last) == NULL)
     {
          i->prior = NULL;
          i->next = NULL;
          (*head) = i;
          (*last) = i;
          return;
     }
     p = (struct books *)malloc(sizeof(struct books));
     p = (*head);
     old = NULL;
     while(p!=NULL)
     {
           if(strcmp(p->booknum,i->booknum) < 0)   
           {
                  old = p;
                  p = p->next;
           }
           else
           {
                  if(p->prior)
                  {
                       p->prior->next = i;
                       i->next = p;
                       i->prior = p->prior;
                       p->prior = i;
                       return;
                  }
                  i->next = p;          //放表头
                  i->prior = NULL;
                  p->prior = i;
                  *head = i;
                  return;
           }
     }//end while
     old->next = i;
     i->next = NULL;
     p->prior = i;
     *last = i;
     free(p);
}
           
struct books *find(char *bn)     //按书名搜索关键字
{
    struct books *p;
    p = head;
   
    while(p!=NULL)
    {
          if(strcmp(bn,p->bookname) == 0)
          {
                return p;
          }
          p = p->next;
    }
    return NULL;
}

void search(void)
{
     char name[20];
     struct books *p;
     printf("请输入你要查询的书名:");
     gets(name);
     p = find(name);
     if(p == NULL)  printf("没有找到你查询的信息.\n");
     else display(p);
}

void modify(void)  //根据书名查找并修改信息
{
     char name[20];
     struct books *p;
     printf("请输入你要查询的书名:");
     gets(name);
     p = find(name);
     if(p == NULL)  printf("没有找到你查询的信息.\n");
     else
     {
          inputs("请输入书的编号:",p->booknum,20);
          inputs("请输入书名:",p->bookname,20);
          inputs("请输作者名:",p->authorname,20);
          inputs("请输入出版社:",p->cbs,20);
          inputs("请输入图书价格:",p->price,5);
          printf("\n");
     }
}

void display(struct books *p)
{
     printf("%s\t",p->booknum);        //书号
     printf("%s\t",p->bookname);       //书名
     printf("%s\t",p->authorname);     //作者
     printf("%s\t",p->cbs);            //出版社
     printf("%s\t",p->price);
     printf("\n");
}

void list(void)
{
     system("cls");
     struct books *p;
     p = head;
     printf("书号\t书名\t作者\t出版社\t价格\n");
     while(p != NULL)
     {
             display(p);
             p = p->next;
     }
     printf("\n");
}

void del(struct books **head,struct books **last)
{
     struct books *i;
     
     char s[50];
     inputs("输入书名:",s,20);
     i = find(s);
     if(i)
     {
          if(*head == i)
          {
                *head = i->next;
                if(*head) (*head)->prior = NULL;
                else *last = NULL;
          }
          else
          {
                 i->prior->next = i->next;
                 if(i != *last)
                 {
                      i->next->prior = i->prior;
                 }
                 else
                 {
                     *last = i->prior;
                 }
          }
          free(i);
     }//end  if(i)
}

void save(void)
{
     struct books *p;
     FILE *fp;
     if((fp=fopen("books","wb")) == NULL)
     {
            printf("open file error.\n");
            exit(1);
     }               
     p = head;
     while(p!=NULL)
     {
            fwrite(p,sizeof(struct books),1,fp);
            p = p->next;
     }
     fclose(fp);
}

void load(void)
{
     struct books *p;
     FILE *fp;
     fp=fopen("books","rb");
     if(fp == NULL)
     {
           printf("open file error.\n");
           exit(1);
     }
     while(head)
     {
           p = head->next;
           free(p);
           head = p;
     }
     head = last = NULL;
     printf("\n 载入信息 \n");
     while(!feof(fp))
     {      
            p = (struct books *)malloc(sizeof(struct books));
            if(1 != fread(p,sizeof(struct books),1,fp)) break;
            dls_store(p,&head,&last);
     }
     fclose(fp);
}
                    
int menu(void)
{
    char s[20];
    int c;
    system("cls");
    printf("\t\t*****************图书信息管理系统*****************\n");
    printf("\t\t*\t\t|1.图书信息输入                   *\n");
    printf("\t\t*\t\t|2.图书信息浏览                   *\n");
    printf("\t\t*\t\t|3.图书信息查询                   *\n");
    printf("\t\t*\t\t|4.图书信息修改                   *\n");
    printf("\t\t*\t\t|5.图书信息删除                   *\n");
    printf("\t\t*\t\t|6.存储图书信息                   *\n");
    printf("\t\t*\t\t|7.载入图书信息                   *\n");
    printf("\t\t*\t\t|8.退出系统                       *\n");
    printf("\t\t*****************图书信息管理系统*****************\n\n\n");
    printf("\t\t请输入以上序号进行选择:\n");
    do{
          gets(s);
          c = atoi(s);
    }while(c<0||c>8);
    return c;
}

int main(void)
{
    head = last = NULL;
    while(1)
    {
        switch(menu())
        {
            case 1:enter();system("pause");//录入  
                   break;
            case 2:list();system("pause");//浏览
                   break;
            case 3:search();system("pause");
                   break;
            case 4:modify();system("pause");
                   break;
            case 5:del(&head,&last);system("pause");
                   break;
            case 6:save();system("pause");
                   break;
            case 7:load();system("pause");
                   break;
            case 8:exit(0);
        }//end switch
    }//end while
    return 0;
}
搜索更多相关主题的帖子: 图书馆 出版社 
2011-04-03 16:10
hnuhsg1226
Rank: 9Rank: 9Rank: 9
来 自:中国
等 级:蜘蛛侠
威 望:2
帖 子:314
专家分:1314
注 册:2011-3-27
收藏
得分:0 
我觉得是dls_stor()函数的问题

我的地盘
2011-04-03 19:28
bccn250
Rank: 5Rank: 5
等 级:职业侠客
帖 子:154
专家分:379
注 册:2010-5-25
收藏
得分:5 
enter 和 dls_store 函数里的 free 去掉试试
2011-04-03 20:21
hnuhsg1226
Rank: 9Rank: 9Rank: 9
来 自:中国
等 级:蜘蛛侠
威 望:2
帖 子:314
专家分:1314
注 册:2011-3-27
收藏
得分:0 
以下是引用bccn250在2011-4-3 20:21:21的发言:

enter 和 dls_store 函数里的 free 去掉试试

不成,不是那里的问题,是这2个函数写的有问题,现在我就是找不出,主要是自己能力不足

我的地盘
2011-04-03 20:49
bccn250
Rank: 5Rank: 5
等 级:职业侠客
帖 子:154
专家分:379
注 册:2010-5-25
收藏
得分:15 
不知道你指的是哪方面的错误,

我个人现在所看到的错误:连续输入两次两本书的资料后,执行到  p->prior = i; 这步卡了, 是不是这个错误

p-prior 是干什么用的?
2011-04-03 21:20
hnuhsg1226
Rank: 9Rank: 9Rank: 9
来 自:中国
等 级:蜘蛛侠
威 望:2
帖 子:314
专家分:1314
注 册:2011-3-27
收藏
得分:0 
以下是引用bccn250在2011-4-3 21:20:01的发言:

不知道你指的是哪方面的错误,

我个人现在所看到的错误:连续输入两次两本书的资料后,执行到  p->prior = i; 这步卡了, 是不是这个错误

p-prior 是干什么用的?

p->prior是向前寻值的指针,与next相反,你说到点子上了,本来我是在enter( )函数里用的是无限输入,后面每次输2个都是错误,就换成一个,还是不行,就觉得是dls_store( )函数的问题了

我的地盘
2011-04-03 21:26
hnuhsg1226
Rank: 9Rank: 9Rank: 9
来 自:中国
等 级:蜘蛛侠
威 望:2
帖 子:314
专家分:1314
注 册:2011-3-27
收藏
得分:0 
我找到错误了,是dls_store( )函数的倒数第三句p->prior=i应改为i->prior = old

我的地盘
2011-04-03 21:37
hnuhsg1226
Rank: 9Rank: 9Rank: 9
来 自:中国
等 级:蜘蛛侠
威 望:2
帖 子:314
专家分:1314
注 册:2011-3-27
收藏
得分:0 
我来共享下,我改好的源码:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

struct books{
        char booknum[20];        //书号
        char bookname[20];       //书名
        char authorname[20];     //作者
        char cbs[20];            //出版社
        char price[5];           //价格
        struct books *next;
        struct books *prior;  
};

struct books *head;       //头结点
struct books *last;       //尾结点

int menu(void);    //菜单选项
void inputs(char *prompt,char *s,int count);//输入格式
void enter(void);                           //信息输入
void dls_store(struct books *i,struct books **head,struct books **last);//按书号大小建立链表
struct books *find(char *bn);                      //按书名检索
void search(void);                                 //按书名查找
void modify(void);                                 //修改信息
void display(struct books *p);                       //显示格式
void list(void);
void del(struct books **head,struct books **last);           //删除信息
void save(void);             //以文件形式保存信息
void load(void);              //从文件中载入信息


void inputs(char *prompt,char *s,int count)
{
     char p[255];
     do{
          printf(prompt);
          gets(p);
          if(strlen(p) > count)  //限定输入字符长度
          {
                printf("\nToo long\n");
          }
     }while(strlen(p) > count);      
     strcpy(s,p);
}

void enter(void)       //输入信息
{
    system("cls");//清屏
    char ch;
    struct books *p;
    while(1)
    {               
          p=(struct books *)malloc(sizeof(struct books));
          char ch;         
          inputs("请输入书的编号:",p->booknum,20);
          inputs("请输入书名:",p->bookname,20);
          inputs("请输作者名:",p->authorname,20);
          inputs("请输入出版社:",p->cbs,20);
          inputs("请输入图书价格:",p->price,5);
          printf("\n");
          dls_store(p,&head,&last);
          printf("是否继续输入(Y/N):");
          ch = getchar();
          if(ch == 'N') break;
          fflush(stdin);
    }
}
                  
void dls_store(struct books *i,struct books **head,struct books **last)   //按书号顺序插入表中
{
     struct books *p,*old;
     if((*last) == NULL)
     {
          i->prior = NULL;
          i->next = NULL;
          (*head) = i;
          (*last) = i;
          return;
     }
     p = (struct books *)malloc(sizeof(struct books));
     p = (*head);
     old = NULL;
     while(p!=NULL)
     {
           if(strcmp(p->booknum,i->booknum) < 0)   
           {
                  old = p;
                  p = p->next;
           }
           else
           {
                  if(p->prior)
                  {
                       p->prior->next = i;
                       i->next = p;
                       i->prior = p->prior;
                       p->prior = i;
                       return;
                  }
                  i->next = p;          //放表头
                  i->prior = NULL;
                  p->prior = i;
                  *head = i;
                  return;
           }
     }//end while
     old->next = i;
     i->next = NULL;
     i->prior = old;
     *last = i;
}
           
struct books *find(char *bn)     //按书名搜索关键字
{
    struct books *p;
    p = head;
   
    while(p!=NULL)
    {
          if(strcmp(bn,p->bookname) == 0)
          {
                return p;
          }
          p = p->next;
    }
    return NULL;
}

void search(void)
{
     char name[20];
     struct books *p;
     printf("请输入你要查询的书名:");
     gets(name);
     p = find(name);
     if(p == NULL)  printf("没有找到你查询的信息.\n");
     else display(p);
}

void modify(void)  //根据书名查找并修改信息
{
     char name[20];
     struct books *p;
     printf("请输入你要查询的书名:");
     gets(name);
     p = find(name);
     if(p == NULL)  printf("没有找到你查询的信息.\n");
     else
     {
          inputs("请输入书的编号:",p->booknum,20);
          inputs("请输入书名:",p->bookname,20);
          inputs("请输作者名:",p->authorname,20);
          inputs("请输入出版社:",p->cbs,20);
          inputs("请输入图书价格:",p->price,5);
          printf("\n");
     }
}

void display(struct books *p)
{
     printf("%s\t",p->booknum);        //书号
     printf("%s\t",p->bookname);       //书名
     printf("%s\t",p->authorname);     //作者
     printf("%s\t",p->cbs);            //出版社
     printf("%s\t",p->price);
     printf("\n");
}

void list(void)
{
     system("cls");
     struct books *p;
     p = head;
     printf("书号\t书名\t作者\t出版社\t价格\n");
     while(p != NULL)
     {
             display(p);
             p = p->next;
     }
     printf("\n");
}

void del(struct books **head,struct books **last)
{
     struct books *i;
     
     char s[50];
     inputs("输入书名:",s,20);
     i = find(s);
     if(i)
     {
          if(*head == i)
          {
                *head = i->next;
                if(*head) (*head)->prior = NULL;
                else *last = NULL;
          }
          else
          {
                 i->prior->next = i->next;
                 if(i != *last)
                 {
                      i->next->prior = i->prior;
                 }
                 else
                 {
                     *last = i->prior;
                 }
          }
          free(i);
     }//end  if(i)
}

void save(void)
{
     struct books *p;
     FILE *fp;
     if((fp=fopen("books","wb")) == NULL)
     {
            printf("open file error.\n");
            exit(1);
     }               
     p = head;
     while(p!=NULL)
     {
            fwrite(p,sizeof(struct books),1,fp);
            p = p->next;
     }
     fclose(fp);
}

void load(void)
{
     struct books *p;
     FILE *fp;
     fp=fopen("books","rb");
     if(fp == NULL)
     {
           printf("open file error.\n");
           exit(1);
     }
     while(head)
     {
           p = head->next;
           free(p);
           head = p;
     }
     head = last = NULL;
     printf("\n 载入信息 \n");
     while(!feof(fp))
     {      
            p = (struct books *)malloc(sizeof(struct books));
            if(1 != fread(p,sizeof(struct books),1,fp)) break;
            dls_store(p,&head,&last);
     }
     fclose(fp);
}
                    
int menu(void)
{
    char s[20];
    int c;
    system("cls");
    printf("\t\t*****************图书信息管理系统*****************\n");
    printf("\t\t*\t\t|1.图书信息输入                   *\n");
    printf("\t\t*\t\t|2.图书信息浏览                   *\n");
    printf("\t\t*\t\t|3.图书信息查询                   *\n");
    printf("\t\t*\t\t|4.图书信息修改                   *\n");
    printf("\t\t*\t\t|5.图书信息删除                   *\n");
    printf("\t\t*\t\t|6.存储图书信息                   *\n");
    printf("\t\t*\t\t|7.载入图书信息                   *\n");
    printf("\t\t*\t\t|8.退出系统                       *\n");
    printf("\t\t*****************图书信息管理系统*****************\n\n\n");
    printf("\t\t请输入以上序号进行选择:\n");
    do{
          gets(s);
          c = atoi(s);
    }while(c<0||c>8);
    return c;
}

int main(void)
{
    head = last = NULL;
    while(1)
    {
        switch(menu())
        {
            case 1:enter();system("pause");//录入  
                   break;
            case 2:list();system("pause");//浏览
                   break;
            case 3:search();system("pause");
                   break;
            case 4:modify();system("pause");
                   break;
            case 5:del(&head,&last);system("pause");
                   break;
            case 6:save();system("pause");
                   break;
            case 7:load();system("pause");
                   break;
            case 8:exit(0);
        }//end switch
    }//end while
    return 0;
}

我的地盘
2011-04-03 21:38
bccn250
Rank: 5Rank: 5
等 级:职业侠客
帖 子:154
专家分:379
注 册:2010-5-25
收藏
得分:0 
恭喜恭喜

我也找到了,只是比你慢了一步,所以不发了
2011-04-03 22:13
bccn250
Rank: 5Rank: 5
等 级:职业侠客
帖 子:154
专家分:379
注 册:2010-5-25
收藏
得分:0 
自己也算是学习
2011-04-03 22:13
快速回复:图书馆管理系统之链表
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019699 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved