之前参照网上的资料用链表实现了图书管理系统,包括简单的增删改查功能以及借书还书功能,我是VC6.0下写的一个控制台程序,格式参照的网上的。在动手编码之前,你需要理清自己的思路。首先,需要确定图书馆里系统中主要有那几个对象,这里我写了学生对象和图书对象。不妨在纸上写出或画出它们主要包括哪些属性以及其可能的对应关系,这里根据不同人的要求会有所不同。清楚这些之后,就可以设计学生和图书的数据结构,比如这里我用的结构体存储其信息。然后就需要考虑,我想要哪些功能,除了基本的增删改查之外,我还想要哪些功能?比如借书、还书,我怎么表示这之间的关系?可以通过图书的属性来记录该书的状态,及是否被借走,谁借了。主要就是这个思路,图书的增删改查是通过链表实现的,当然也可以用数组实现,只不过那会浪费较多的空间。

// MyLibManSys.cpp : Defines the entry point for the console application.

//

#include "stdafx.h"

#include "iostream"

struct book{

int id;

char title[20];

char author[20];

double price;

char state[20];

int student_id;

char student_name[20];

struct book* next;

};

struct student{

int id;

char name[20];

char sex[10];

char borrow_book[30];

struct student* next;

};

void Print_Book(struct book *head_book);

void Print_Student(struct student *head_student);

struct book *Create_New_Book();/*创建新的图书库*/

struct student *Create_New_Student();/*创建新的学生库*/

struct book *Insert_Book(struct book *head_book,struct book *new_book);/*增加图书,逐个添加*/

//void Insert_Book(struct book *head_book,struct book *new_book);/*增加图书,逐个添加*/

//函数的参数是一个指针时,不要在函数体内部改变指针所指的地址,那样毫无作用,需要修改的只能是指针所指向的内容。即应把指针当作常量

struct student *Insert_Student(struct student *head_student,struct student *new_student);/*增加学生,逐个添加*/

struct book *Search_Book_ById(int id,struct book *head_book);

struct book *Search_Book_ByTitle(char *title,struct book *head_book);

struct book *Search_Book_ByPrice(double price_h,double price_l,struct book *head_book);

//bool Delete_Book(int id,book* head_book);

struct book* Delete_Book(int id,book* head_book);

struct student *Search_Student(int id,struct student *head_student);

struct student* Delete_Student(int id,student* head_student);

void Lent_Book(int id,int student_id,struct book *head_book,struct student *head_student);

void Back_Book(int id,int student_id,struct book *head_book,struct student *head_student);

int main()

{

struct book* head_book,*p_book;

struct student* head_student, *p_student;

int choice, f, id, student_id;

int m = 1;

char name[20],sex[10];

char title[20];

double price_h,price_l,price;

char author[20];

int size_book=sizeof(struct book);

int size_student=sizeof(struct student);

printf("\n欢迎您第一次进入图书管理系统!\n\n");

printf("----->[向导]----->[新建图书库]\n\n");

printf("注意:当输入图书编号为0时,进入下一步.\n\n");

head_book=Create_New_Book();

system("cls");

//Print_Book(head_book);

printf("\n欢迎您第一次进入图书管理系统!\n\n");

printf("----->[向导]----->[新建会员库]\n\n");

printf("注意:当输入会员学号为0时,进入主菜单.\n\n");

head_student=Create_New_Student();

system("cls");

//Print_Student(head_student);

do{

printf("\n\t\t\t〓〓〓〓〓图书管理系统〓〓〓〓〓\n\n");

printf("\n");

printf("\t\t\t[1]:借书办理\t");printf(" [6]:还书办理\n");

printf("\n");

printf("\t\t\t[2]:查询图书\t");printf(" [7]:查询学生\n");

printf("\t\t\t[3]:添加图书\t");printf(" [8]:添加学生\n");

printf("\t\t\t[4]:删除图书\t");printf(" [9]:删除学生\n");

printf("\t\t\t[5]:遍历图书\t");printf("[10]:遍历学生\n\n");

printf("\t\t\t〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓\n\n");

printf("\t\t\t0:退出\n\n");

printf("请选择<0~10>:");

scanf("%d",&choice);

switch(choice){

case 0:

system("cls");

printf("\n\t\t\t〓〓〓〓〓图书管理系统〓〓〓〓〓\n\n");

printf("\n谢谢您的使用!\n\n");

break;

case 1:

system("cls");

printf("\n\t\t\t〓〓〓〓〓图书管理系统〓〓〓〓〓\n\n");

printf("输入借出图书编号:\n");

scanf("%d",&id);

printf("输入借入学生学号:\n");

scanf("%d",&student_id);

Lent_Book(id,student_id,head_book,head_student);

break;

case 2:

system("cls");

printf("\n\t\t\t〓〓〓〓〓图书管理系统〓〓〓〓〓\n\n");

printf("1.按编号查询\n\n");

printf("2.按名称查询\n\n");

printf("3.按价格区间查询\n\n");

printf("0.返回主菜单\n\n");

printf("请选择:");

scanf("%d",&f);

if(f==1){

printf("请输入查询图书编号:");

scanf("%d",&id);

printf("相关信息如下:\n\n");

head_book=Search_Book_ById(id,head_book);

break;

}

else if(f==2){

getchar();

printf("请输入查询图书名称:");

gets(title);

printf("相关信息如下:\n\n");

head_book=Search_Book_ByTitle(title,head_book);

break;

}

else if(f==3){

printf("请输入最高价格:");

scanf("%lf",&price_h);

printf("请输入最低价格:");

scanf("%lf",&price_l);

printf("相关信息如下:\n\n");

head_book=Search_Book_ByPrice(price_h,price_l,head_book);

break;

}

else if(f==0){

break;

}

break;

case 3:

system("cls");

printf("\n\t\t\t〓〓〓〓〓图书管理系统〓〓〓〓〓\n\n");

printf("请输入图书编号:");

scanf("%d",&id);

printf("请输入图书名称:");

scanf("%s",title);

printf("请输入作者名字:");

scanf("%s",author);

printf("请输入单价:");

scanf("%lf",&price);

printf("\n");

struct book *ptr_b;

for(ptr_b=head_book;ptr_b;ptr_b=ptr_b->next)

{

if(ptr_b->id==id)

{

printf("此编号图书已存在\n");

m=0;

break;

}

}

if(m){

p_book=(struct book *)malloc(size_book);

strcpy(p_book->title,title);

p_book->id=id;

p_book->price=price;

p_book->student_id=-1;

strcpy(p_book->author,author);

strcpy(p_book->state,"存在");

strcpy(p_book->student_name,"待定");

// head_book=Insert_Book(head_book,p_book);

Insert_Book(head_book,p_book);

printf("\n添加图书成功!\n\n");

}

break;

case 4:

system("cls");

printf("\n\t\t\t〓〓〓〓〓图书管理系统〓〓〓〓〓\n\n");

printf("输入删除图书编号:\n");

scanf("%d",&id);

/*if(Delete_Book(id,head_book)){

printf("\n删除图书成功!\n\n");

}else{

printf("删除失败");

}*/

head_book = Delete_Book(id,head_book);

break;

case 5:

system("cls");

printf("\n\t\t\t〓〓〓〓〓图书管理系统〓〓〓〓〓\n\n");

Print_Book(head_book);

break;

case 6:

system("cls");

printf("\n\t\t\t〓〓〓〓〓图书管理系统〓〓〓〓〓\n\n");

printf("输入归还图书编号:\n");

scanf("%d",&id);

printf("输入归还学生学号:\n");

scanf("%d",&student_id);

Back_Book(id,student_id,head_book,head_student);

break;

case 7:

system("cls");

printf("\n\t\t\t〓〓〓〓〓图书管理系统〓〓〓〓〓\n\n");

printf("请输入查询学生学号:");

scanf("%d",&id);

printf("相关信息如下:\n\n");

head_student=Search_Student(id,head_student);

break;

case 8:

system("cls");

printf("\n\t\t\t〓〓〓〓〓图书管理系统〓〓〓〓〓\n\n");

printf("请输入学生编号:");

scanf("%d",&id);

printf("请输入学生姓名:");

scanf("%s",name);

printf("请输入学生性别:");

scanf("%s",sex);

printf("\n");

struct student *ptr_s;

for(ptr_s=head_student;ptr_s;ptr_s=ptr_s->next)

{

if(ptr_s->id==id)

{

printf("此学号学生已存在\n");

m=0;

break;

}

}

if(m){

p_student=(struct student *)malloc(size_student);

p_student->id=id;

strcpy(p_student->name,name);

strcpy(p_student->sex,sex);

strcpy(p_student->borrow_book,"无");

head_student=Insert_Student(head_student,p_student);

printf("\n添加学生成功!\n\n");

}

break;

case 9:

system("cls");

printf("\n\t\t\t〓〓〓〓〓图书管理系统〓〓〓〓〓\n\n");

printf("输入删除学生学号:\n");

scanf("%d",&id);

head_student = Delete_Student(id,head_student);

break;

case 10:

system("cls");

printf("\n\t\t\t〓〓〓〓〓图书管理系统〓〓〓〓〓\n\n");

Print_Student(head_student);

}

}while(choice!=0);

return 0;

}

struct book *Create_New_Book(){

struct book *head_book,*p_book;

int id, tag;

double price;

char title[20],author[20];

int size_book=sizeof(struct book);

head_book=NULL;

printf("请输入图书编号:");

scanf("%d",&id);

printf("请输入图书名称:");

scanf("%s",title);

printf("请输入作者名字:");

scanf("%s",author);

printf("请输入单价:");

scanf("%lf",&price);

printf("\n");

while(true){

p_book=(struct book *)malloc(size_book);

strcpy(p_book->title,title);

p_book->id=id;

p_book->price=price;

p_book->student_id=-1;

strcpy(p_book->author,author);

strcpy(p_book->state,"存在");

strcpy(p_book->student_name,"待定");

head_book=Insert_Book(head_book,p_book);

printf("是否继续?继续输入1,退出按任意键\n");

scanf("%d",&tag);

if(tag!=1){

break;

}

printf("请输入图书编号:");

scanf("%d",&id);

printf("请输入图书名称:");

scanf("%s",title);

printf("请输入作者名字:");

scanf("%s",author);

printf("请输入单价:");

scanf("%lf",&price);

printf("\n");

}

return head_book;

}

struct student *Create_New_Student(){

struct student *head_student,*p_student;

int id, tag;

char sex[10];

char name[20];

int size_student=sizeof(struct student);

head_student=NULL;

printf("请输入学生编号:");

scanf("%d",&id);

printf("请输入学生姓名:");

scanf("%s",name);

printf("请输入学生性别:");

scanf("%s",sex);

printf("\n");

while(true){

p_student=(struct student *)malloc(size_student);

p_student->id=id;

strcpy(p_student->name,name);

strcpy(p_student->sex,sex);

strcpy(p_student->borrow_book,"无");

head_student=Insert_Student(head_student,p_student);

printf("是否继续?继续输入1,退出按任意键\n");

scanf("%d",&tag);

if(tag!=1){

break;

}

printf("请输入学生编号:");

scanf("%d",&id);

printf("请输入学生姓名:");

scanf("%s",name);

printf("请输入学生性别:");

scanf("%s",sex);

printf("\n");

}

return head_student;

}

struct book *Insert_Book(struct book *head_book,struct book *new_book){

struct book *p,*q;

p=q=head_book;

if(head_book==NULL){ //单向链表为空的情况

head_book=new_book;

new_book->next = NULL;

}else{

while((new_book->id>p->id)&&(p->next!=NULL)){

q = p;

p = p->next;

}

if(new_book->id<=p->id){

new_book->next=p;

if(head_book==p)

head_book=new_book;

else

q->next = new_book;

}else{

p->next=new_book;

new_book->next=NULL;

}

}

return head_book;

};

struct student *Insert_Student(struct student *head_student,struct student *new_student){

struct student *p,*q;

p=q=head_student;

if(head_student==NULL){ //单向链表为空的情况

head_student=new_student;

new_student->next = NULL;

}else{

while((new_student->id>p->id)&&(p->next!=NULL)){

q = p;

p = p->next;

}

if(new_student->id<=p->id){

new_student->next=p;

if(head_student==p)

head_student=new_student;

else

q->next = new_student;

}else{

p->next=new_student;

new_student->next=NULL;

}

}

return head_student;

}

struct book *Search_Book_ById(int id,struct book *head_book){

struct book *ptr_book = head_book;

int flag=0;

while(ptr_book!=NULL)

{

if(ptr_book->id==id){

printf("图书编号:%d\n",ptr_book->id);

printf("图书名称:%s\n",ptr_book->title);

printf("图书单价:%.2lf\n",ptr_book->price);

printf("图书作者:%s\n",ptr_book->author);

printf("存在状态:%s\n",ptr_book->state);

printf("借书人姓名:%s\n",ptr_book->student_name);

printf("学号:%d\n",ptr_book->student_id);

printf("\n");

flag++;

}

if(flag>0)

{

break;

}

ptr_book = ptr_book->next;

}

if(flag==0){

printf("暂无此图书信息!\n\n");

}

return head_book;

};

struct book *Search_Book_ByTitle(char *title,struct book *head_book){

struct book *ptr_book = head_book;

int flag=0;

while(ptr_book!=NULL)

{

if(strcmp(ptr_book->title,title)==0){

printf("图书编号:%d\n",ptr_book->id);

printf("图书名称:%s\n",ptr_book->title);

printf("图书单价:%.2lf\n",ptr_book->price);

printf("图书作者:%s\n",ptr_book->author);

printf("存在状态:%s\n",ptr_book->state);

printf("借书人姓名:%s\n",ptr_book->student_name);

printf("学号:%d\n",ptr_book->student_id);

printf("\n");

flag++;

}

if(flag>0)

{

break;

}

ptr_book = ptr_book->next;

}

if(flag==0){

printf("暂无此图书信息!\n\n");

}

return head_book;

};

struct book *Search_Book_ByPrice(double price_h,double price_l,struct book *head_book){

struct book *ptr_book = head_book;

int flag=0;

while(ptr_book!=NULL)

{

if(ptr_book->price>=price_l&&ptr_book->price<=price_h){

printf("图书编号:%d\n",ptr_book->id);

printf("图书名称:%s\n",ptr_book->title);

printf("图书单价:%.2lf\n",ptr_book->price);

printf("图书作者:%s\n",ptr_book->author);

printf("存在状态:%s\n",ptr_book->state);

printf("借书人姓名:%s\n",ptr_book->student_name);

printf("学号:%d\n",ptr_book->student_id);

printf("\n");

flag++;

}

ptr_book = ptr_book->next;

}

if(flag==0){

printf("暂无此图书信息!\n\n");

}

return head_book;

}

/*bool Delete_Book(int id,book* head_book){

bool flag=true;

struct book *p,*q;

p=q=head_book;

if(p->id==id&&p->next==NULL){

head_book=NULL;

}

while(p->id!=id&&p->next!=NULL){

q=p;

p=p->next;

}

if(p->id==id){

if(p==head_book){

head_book=p->next;

}else{

q->next=p->next;

}

free(p);

}else{

flag=false;

printf("找不到该书");

}

return flag;

};*/

struct book* Delete_Book(int id,book* head_book){

bool flag=true;

struct book *p,*q;

p=q=head_book;

while(p->id!=id&&p->next!=NULL){

q=p;

p=p->next;

}

if(p->id==id){

if(p==head_book){

head_book=p->next;

}else{

q->next=p->next;

}

free(p);

printf("删除成功!\n");

}else{

flag=false;

printf("找不到该书");

}

return head_book;

};

struct student* Delete_Student(int id,student* head_student){

bool flag=true;

struct student *p,*q;

p=q=head_student;

while(p->id!=id&&p->next!=NULL){

q=p;

p=p->next;

}

if(p->id==id){

if(p==head_student){

head_student=p->next;

}else{

q->next=p->next;

}

free(p);

printf("删除成功!\n");

}else{

flag=false;

printf("找不到该学生");

}

return head_student;

};

struct student *Search_Student(int id,struct student *head_student){

struct student *ptr_student = head_student;

int flag=0;

while(ptr_student!=NULL)

{

if(ptr_student->id==id){

printf("学号:%d\n",ptr_student->id);

printf("姓名:%s\n",ptr_student->name);

printf("性别:%s\n",ptr_student->sex);

printf("借书:%s\n",ptr_student->borrow_book);

printf("\n");

flag++;

}

if(flag>0)

{

break;

}

ptr_student = ptr_student->next;

}

if(flag==0){

printf("暂无此学生信息!\n\n");

}

return head_student;

};

void Lent_Book(int id,int student_id,struct book *head_book,struct student *head_student){

struct book* p=head_book;

struct student* q=head_student;

if(p==NULL||q==NULL){

printf("书本或学生不存在\n");

return;

}

while(p!=NULL&&q!=NULL){

if(p->id!=id){

p=p->next;

}

if(q->id!=student_id){

q=q->next;

}

if(p->id==id&&q->id==student_id){

break;

}

}

if(p==NULL||q==NULL){

printf("书本或学生不存在\n");

return;

}else{

if(strcmp(p->state,"存在")!=0){

printf("书已借出!抱歉!");

return;

}else{

p->student_id=student_id;

strcpy(p->student_name,q->name);

strcpy(q->borrow_book,p->title);

strcpy(p->state,"已借出");

printf("已成功借出!/n");

}

}

};

void Back_Book(int id,int student_id,struct book *head_book,struct student *head_student){

struct book* p=head_book;

struct student* q=head_student;

if(p==NULL||q==NULL){

printf("书本或学生不存在\n");

return;

}

while(p!=NULL&&q!=NULL){

if(p->id!=id){

p=p->next;

}

if(q->id!=student_id){

q=q->next;

}

if(p->id==id&&q->id==student_id){

break;

}

}

if(p==NULL||q==NULL){

printf("书本或学生不存在\n");

return;

}else{

if(strcmp(p->state,"存在")==0){

printf("书未借出!抱歉!");

return;

}else{

p->student_id=-1;

strcpy(p->student_name,"待定");

strcpy(q->borrow_book,"无");

strcpy(p->state,"存在");

printf("已成功归还!/n");

}

}

};

void Print_Book(struct book *head_book){

struct book* p=head_book;

if(p==NULL){

printf("\n无记录\n\n");

return;

}

printf("\n图书编号\t图书名称\t图书单价\t图书作者\n\n");

while (p!=NULL)

{

printf("%d\t\t%s\t\t%.2lf\t\t%s\n\n",p->id,p->title,p->price,p->author);

p = p->next;

}

}

void Print_Student(struct student *head_student){

struct student* p=head_student;

if(p==NULL){

printf("\n无记录\n\n");

return;

}

printf("\n学生姓名\t学生性别\t学生学号\n\n");

while (p!=NULL)

{

printf("%s\t\t%s\t\t%d\n",p->name,p->sex,p->id);

p = p->next;

}

}

代码可以直接运行,这里我都是在控制台上直接显示的,如果想从文件读取和向文件写入学生和图书信息,只需要把相应的printf和scanf部分改为文件操作。这个是很久之前写的,详细的函数以及功能讲解这里就不介绍了。欢迎大家讨论和指导。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

Java用链表写图书管理_C语言链表实现图书管理系统相关推荐

  1. java用链表做学生系统_C语言链表实现学生管理系统

    本文实例为大家分享了C语言链表实现学生管理系统的具体代码,供大家参考,具体内容如下 #include #include #include #include #include #include usin ...

  2. c语言和mysql实现超市的管理_C语言编写的超市管理系统

    展开全部 有一个小型超市,出售N(N>=10)种商品,设计并实e69da5e887aa3231313335323631343130323136353331333262363565现一个系统,完成 ...

  3. c语言链表查找的代码与题目,链表的C语言实现之单链表的查找运算_c语言

    建立了一个单链表之后,如果要进行一些如插入.删除等操作该怎么办?所以还须掌握一些单链表的基本算法,来实现这些操作.单链表的基本运算包括:查找.插入和删除.下面我们就一一介绍这三种基本运算的算法,并结合 ...

  4. JAVA计算机毕业设计写手管理平台Mybatis+源码+数据库+lw文档+系统+调试部署

    JAVA计算机毕业设计写手管理平台Mybatis+源码+数据库+lw文档+系统+调试部署 JAVA计算机毕业设计写手管理平台Mybatis+源码+数据库+lw文档+系统+调试部署 本源码技术栈: 项目 ...

  5. C语言与JAVA内存管理_C语言内存管理

    本章将介绍C语言动态内存管理. C语言编程语言提供了多种功能的内存分配和管理.这些函数可以在头文件中找到. S.N. 函数与说明 1 void *calloc(int num, int size); ...

  6. 算法精解_C语言 链表_单链表(接口定义+类型实现)

    链表可以说是一种最为基础的数据结构.链表由一组元素以一种特定的顺序组合或链接而成,在维护数据的集合时很有用.这一点同我们常用的数组很相似.然而,链表在很多情况下比数组更有优势.特别是在执行插入和删除操 ...

  7. c语言程序设计图书管理,C语言程序设计报告 图书馆信息管理系统

    C语言程序设计报告 图书馆信息管理系统 2011 年第二阶段课程设计成果报告实训项目: 程序设计技术课程设计 学号: 0112959 姓名: 张麦华 专业: 信息管理与信息系统 实训日期:2011 年 ...

  8. c语言链表head的作用,c语言链表的用法

    c语言链表的用法 链表是数据结构中比较基础也是比较重要的类型之一,那么有了数组,为什么我们还需要链表呢!或者说设计链表这种数据结构的初衷在哪里?下面小编就为大家介绍下c语言链表的用法. c语言枚举的用 ...

  9. c语言教学系统管理,在Moodle平台中实现_C语言_课程教学管理系统

    第31卷第3期20113咸宁学院学报JournalofXianningUniversityVol.31,No.3 Mar.2011 文章编号:1006-5342(2011)03-0148-03 C语言 ...

最新文章

  1. 14 集成测试方法之自底向上集成方法
  2. 96. 奇怪的汉诺塔【递推】
  3. 深入理解final关键字
  4. SharePoint designer 文件--新建中没有工作流
  5. Opencv--Mat类型ROI的设置
  6. 关于三甲医院转行IT 医生家属说两句
  7. Nordic Collegiate Programming Contest 2016
  8. sell - 配置service
  9. IIS-扩展接口ISAPI-Filter分析
  10. java 右下角_java右下角弹窗
  11. discuz模板制作中关于css使用的一些指导说明
  12. HTL5 JavaScript里的DOM节点简单思维导图(元素节点,文本节点,节点的增删改查) 高清可打印
  13. 银行统一支付平台(介绍、架构)
  14. 移动端常见芯片名及其对应代号
  15. 学习竞争情报的第一阶段——读书
  16. Elasticsearch Compound queries 学习总结
  17. 用C#识别图像中的文字_MODI OCR
  18. Unity3D数字孪生笔记——Unity常用API篇
  19. 详细教程—使用闲置树莓派打造“小霸王学习机”
  20. ES6的import和export

热门文章

  1. jQuery.Event的一些用法
  2. android 解析网络数据(JSON)
  3. java并发编程实战阅读总结(a)
  4. 10张图22段代码,万字长文带你搞懂虚拟内存模型和malloc内部原理
  5. Android Jni 例程
  6. STM32f103 —— 内部flash读写
  7. 过拟合问题——正则化方法
  8. db2 sql执行历史_5 个免费的在线 SQL 数据库环境,比Navicat 香!
  9. 天池 在线编程 牛郎织女(广度优先搜索)
  10. LeetCode 1758. 生成交替二进制字符串的最少操作数(DP)