【C语言】图书管理系统

程序名称:图书管理系统
编译语言:C语言
编译器:Dev C++
编译器获取和安装步骤详见:https://blog.csdn.net/qq_35284513/article/details/112351910

这是一个用C语言编写的一个简单的图书管理系统。这个管理系统没有使用数据库,使用的是C语言的结构体和数据结构中的单链表作为存储,同时频繁使用了C语言中的指针操作。这个程序可以作为一个C语言阶段性的一个程序,也可以作为大一C语言课程设计的一个期末作业,熟练编写一个这样的小程序,可以整合C语言学习过程中的所有关键知识点和语法。

下面开始进入正题,与大家分享图书管理系统的具体代码以及效果演示。

首先定义这个系统的结构体头文件,便于之后引用的方便。
注意:自己编写的头文件,其名称一定要记住,之后的引用一定要与所起的头文件的文件名一致!

文件名:book_struct.h

#include<stdio.h>
struct book{
	char bid[20];	//编号 
	char bname[50];	//书名 
	char wname[30];	//作者 
	char publish[50];	//出版社
	double price;		//价格
	int isborrow;		//是否借阅 
	char bperson[30];	//借阅人
	char ldate[30];	//借阅日期 
	struct book *next;
	
}book;

文件名:welcome.c
#include<stdio.h>

void welcome(){
	printf("*******************************************************\n");
	printf("*                                                     *\n");
	printf("*                                                     *\n");
	printf("*              欢迎使用图书管理系统                   *\n");
	printf("*                                                     *\n");
	printf("*                                                     *\n");
	printf("*******************************************************\n");
	printf("*              请选择需要使用的功能                   *\n");
	printf("*                                                     *\n");
	printf("*              1.新建图书管理表格                     *\n");
	printf("*              2.查询图书                             *\n");
	printf("*              3.增加图书                             *\n");
	printf("*              4.删除图书                             *\n");
	printf("*              5.图书借阅                             *\n");
	printf("*              6.图书归还                             *\n");
	printf("*              7.修改图书信息                         *\n");
	printf("*              0.退出                                 *\n");
	printf("*                                                     *\n");
	printf("*******************************************************\n");
}

文件名:select_function.c

#include<stdio.h>
void select_function(){
	printf("*******************************************************\n");
	printf("*              请选择需要使用的功能                   *\n");
	printf("*                                                     *\n");
	printf("*              1.新建图书管理表格                     *\n");
	printf("*              2.查询图书                             *\n");
	printf("*              3.增加图书                             *\n");
	printf("*              4.删除图书                             *\n");
	printf("*              5.图书借阅                             *\n");
	printf("*              6.图书归还                             *\n");
	printf("*              7.修改图书信息                         *\n");
	printf("*              0.退出                                 *\n");
	printf("*                                                     *\n");
	printf("*******************************************************\n");
	return ;
} 

文件名:create_lib.c

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include"book_struct.h"

struct book *create_lib() {	//初始化 
	struct book *head,*p,*s;
	char id[50];
	char name[50];
	char writer[30];
	char publish[50];	//出版社
	double price1;		//价格
	int borrow;		//是否借阅
	char person[30];	//借阅人
	char date[30];	//归还日期

	p = NULL;
	head = NULL;
		
	while(1) {
		printf("请输入书的编号:");
		scanf("%s",id);
		printf("请输入书名:");
		scanf("%s",name);
		printf("请输入作者名:");
		scanf("%s",writer);
		printf("请输入图书出版社:");
		scanf("%s",publish);
		printf("请输入图书价格:");
		scanf("%lf",&price1);
		printf("是否已被借阅:");
		scanf("%d",&borrow);
		if(borrow) {
			printf("请输入借阅者:");
			scanf("%s",person);
			printf("请输入借阅日期:");
			scanf("%s",date);
		}
		
		//建立节点
		s=(struct book *)malloc(sizeof(struct book));	
		
		//录入节点信息
		strcpy(s->bid,id);
		strcpy(s->bname,name);
		strcpy(s->wname,writer);
		strcpy(s->publish,publish);
		s->price=price1;
		s->isborrow=borrow;

		if(borrow==0) {
			strcpy(s->bperson,"NULL");
			strcpy(s->ldate,"NULL");
		} else {
			strcpy(s->bperson,person);
			strcpy(s->ldate,date);
		}

		//插入节点
		if(head){
			p->next=s;
			s->next=NULL; 
			p=s;
		}else{
			head=s;
			head->next=NULL;
			p=head;
		}

		//是否继续录入
		printf("是否继续录入书籍信息(Y/N):");
		char ch[3];
		scanf("%s",ch);
		if(strcmp(ch,"N")==0||strcmp(ch,"n")==0){
			break;
		}
	}
	return head;
}

文件名:print_book.c

#include<stdio.h>
#include"book_struct.h"
void print_book(struct book *head) {
	while(head) {
		printf("书籍编号:%s\n",head->bid);
		printf("书籍名称:%s\n",head->bname);
		printf("书籍作者:%s\n",head->wname);
		printf("出 版 社:%s\n",head->publish);
		printf("书籍价格:%.2lf\n",head->price);
		printf("是否借阅:");
		if(head->isborrow) {
			printf("已借阅\n");
			printf("借阅者:%s\n",head->bperson);
			printf("借阅日期:%s\n",head->ldate);
		} else {
			printf("未借阅\n");
		}
		head=head->next;
	}
	return ;
}

文件名:insert_lib.c

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<string.h>
#include"book_struct.h"
struct book *insert_book(struct book *head) {
	struct book *p,*s;
	char id[50];
	char name[50];
	char writer[30];
	char publish[50];	//出版社
	double price1;		//价格
	int borrow;		//是否借阅
	char person[30];	//借阅人
	char date[30];	//归还日期

	s=(struct book *)malloc (sizeof(struct book));
	printf("请输入书的编号:");
	scanf("%s",id);
	strcpy(s->bid,id);
	printf("请输入书名:");
	scanf("%s",name);
	strcpy(s->bname,name);
	printf("请输入作者名:");
	scanf("%s",writer);
	strcpy(s->wname,writer);
	printf("请输入图书出版社:");
	scanf("%s",publish);
	strcpy(s->publish,publish);
	printf("请输入图书价格:");
	scanf("%lf",&price1);
	s->price=price1;
	printf("是否已被借阅:");
	scanf("%d",&borrow);
	s->isborrow=borrow;
	if(borrow) {
		printf("请输入借阅者:");
		scanf("%s",person);
		strcpy(s->bperson,person);
		printf("请输入借阅日期:");
		scanf("%s",date);
		strcpy(s->ldate,date);
	} else {
		strcpy(s->bperson,"NULL");
		strcpy(s->ldate,"NULL");
	}

	//插入节点
	for(p=head;p->next!=NULL;p=p->next);	//找到尾节点 
	p->next = s;							//在链表尾部插入新增节点 
	s->next=NULL;
	p=s; 
	printf("插入成功\n");
	return head;
	
}

文件名:delete_lib.c

#include<stdio.h>
#include"book_struct.h"
#include<malloc.h>
struct book *delete_book(struct book *head,char id[]) {
	struct book *p,*q;
	q=head;
	for(p=head; strcmp(p->next->bid,id)!=0; p=p->next) {
		if(strcmp(head->bid,id)==0) {
			break;
		}
	}	//找到待删除节点的位置

	if(strcmp(head->bid,id)==0) {
		p=head;
		head=p->next;
		free(p);
		printf("删除成功!\n");
	} else {
		if(p) {
			q=p->next;
			p->next=q->next;
			free(q);
			printf("删除成功!\n");
		} else {
			printf("此书不存在!\n");
		}
	}
	return head;
}

文件名:borrow_lib.c

#include<stdio.h>
#include<stdlib.h>
#include"book_struct.h"
void book_borrow(struct book *head,char id[]){
	struct book *p;
	for(p=head; strcmp(p->bid,id)!=0; p=p->next) {
		if(strcmp(head->bid,id)==0) {
			break;
		}
	}	//找到待删除节点的位置
	if(p){
		p->isborrow=1;
		strcpy(p->ldate,"NULL"); 
		printf("请输入借阅人:");
		scanf("%s",p->bperson);
		printf("借阅成功!\n");
	} else{
		printf("借阅失败!\n");
	}
	//return head;
}

文件名:return_lib.c

#include<stdio.h>
#include"book_struct.h"
void book_return(struct book *head,char id[]){
	struct book *p;
	for(p=head;strcmp(p->bid,id)!=0;p=p->next){
		if(strcmp(head->bid,id)==0){
			break;
		}
	}	//找到待删除节点的位置
	if(p){
		p->isborrow=0;
		printf("请输入归还日期:");
		scanf("%s",p->ldate);
		printf("归还成功!\n");
	} else{
		printf("归还失败!\n");
	}
	//return head;
}

文件名:update_lib.c

#include<stdio.h>
#include"book_struct.h"
void update_book(struct book *head,char id[]) {
	struct book *p;
	
	char name[50];
	char writer[30];
	char publish[50];	//出版社
	double price1;		//价格
	int borrow;		//是否借阅
	char person[30];	//借阅人
	char date[30];	//归还日期

	for(p=head;strcmp(p->bid,id)!=0;p=p->next){
		if(strcmp(head->bid,id)==0){
			break;
		}
	}	//找到待删除节点的位置
	
	if(p) {
		printf("请输入书名:");
		scanf("%s",name);
		strcpy(p->bname,name);
		
		printf("请输入作者名:");
		scanf("%s",writer);
		strcpy(p->wname,writer);
		
		printf("请输入图书出版社:");
		scanf("%s",publish);
		strcpy(p->publish,publish);
		
		printf("请输入图书价格:");
		scanf("%lf",&price1);
		p->price=price1;
		
		printf("是否已被借阅:");
		scanf("%d",&borrow);
		p->isborrow=borrow;
		
		if(borrow) {
			printf("请输入借阅者:");
			scanf("%s",person);
			printf("请输入借阅日期:");
			scanf("%s",date);
			strcpy(p->bperson,person);
			strcpy(p->ldate,date);
		}else{
			strcpy(p->bperson,"NULL");
			strcpy(p->ldate,"NULL");
		}
		
		printf("修改成功!\n");
	}else{
		printf("修改失败!\n");
	}
	//return head;
}

文件名:start.c (主函数)

#include<stdio.h>
#include"book_struct.h"
int main() {
	welcome();
	int num;
	struct book *head,*p,*s,*q;
	head=NULL;
	p=head;
	q=head;
	
	while(1) {
		printf("请选择要使用的功能:\n");
		scanf("%d",&num);
		if(num==0)
			return 0;
		char id[30];
		switch (num) {
			case 1:
				printf("创建一个新的图书管理表格\n");
				head = create_lib();
				break;
			case 2:
				printf("所有书籍信息\n");
				print_book(head);
				break;
			case 3:
				printf("录入新增图书信息\n");
				head = insert_book(head);
				break;
			case 4:
				printf("请输入要删除图书的ID:");
				scanf("%s",id);
				head = delete_book(head,id);
				break;
			case 5:
				printf("请输入要借阅图书的ID:");
				scanf("%s",id);
				book_borrow(head,id);
				break;
			case 6:
				printf("请输入要归还图书的ID:");
				scanf("%s",id);
				book_return(head,id);
				break;
			case 7:
				printf("请输入要修改图书信息的ID:");
				scanf("%s",id);
				update_book(head,id);
				break; 
		}
		int i;
		printf("退出请按0,按其他数字键继续:");
		scanf("%d",&i);
		if(i==0) {
			break;
		}
		select_function();
	}
	return 0;
}

以上是这个图书管理系统的11个文件中的所有代码和最后实现成功的。在给文件起名字时,也要按照C语言的起名格式来命名,否则会产生很多问题。在最后我会附上这个程序源代码、操作说明和视频演示的下载链接。

这个图书管理系统能够实现核心的功能,也是一个基础框架,之后可以在这个的基础上做一些代码和算法上的优化,同时也可以做一些界面的美化,实现操作的流畅性和方便性。

万事开头难,希望这个小小的程序,能够给各位带来语言编程的入门,带来一些编译运行成功的成就感!

源代码下载传送门:
链接:https://pan.baidu.com/s/1t0IK6M1gRb8YcCbI2dBEDA
提取码:vqfa

csdn下载地址:
https://download.csdn.net/download/qq_35284513/15436191?spm=1001.2014.3001.5501

如果这篇文章对您有所帮助,请您点个赞,时间会见证成长,我们一起努力!

  • 14
    点赞
  • 64
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

White白小纯

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值