当前位置: 首页 > article >正文

C语言_图书管理系统_借阅系统管理

 ✨✨ 欢迎大家来到小伞的大讲堂✨✨

🎈🎈养成好习惯,先赞后看哦~🎈🎈

所属专栏:数据结构与算法
小伞的主页:xiaosan_blog

本文所需对顺序表的理解:

注:由于顺序表实现图书管理系统较为简单,本文主要是对顺序表的回顾和对两系统头文件的穿插使用和思路的实现。所以对于图书管理系统,讲述较为简单。如需详解可查看以往顺序表的文章。

顺序表入门  ,顺序表深入

目标需求:

学校图书有数十万本藏书数千位读者每本书具有书号、书名、读者、专业和借阅人等登记项其中各书的书号不重复每位读者具有读者号、姓名、专业和所借图书等登机项,读者都是本校的老师和学生。

出纳台的功能有:

1.注册新读者

2.查询某书的下落

3.借出书给某人

4.收回某人归还的某书 

思路:

图书管理系统

由于顺序表实现图书管理系统较为简单,本文主要是对顺序表的回顾和对两系统头文件的穿插实现。所以对于图书管理系统,讲述较为简单。如需详解可查看以往文章

1. 创建图书(book)结构体

要实现出纳台的功能,我们就得先完成对图书的管理系统,先有图书,再有读者。

按照要求我们需要创建图书结构体(book)

#define _CRT_SECURE_NO_WARNINGS 1

#define MAX 10000

#define MAX_NAME 15
#define MAX_ID 5
#define MAX_AUTHOR 12
#define MAX_PUBLISH 20

struct Book
{
	char name[MAX_NAME];//书名
	int id;//书版号
	char author[MAX_AUTHOR];//作者
	char publish[MAX_PUBLISH];//出版社
	int opp;//借书状态//1被借出//0没有被借出
	char reader_b[15];//借阅本书的读者
	//char tec_b[20];//专业
	time_t start;//借书的时间
	time_t end;//归还书籍时间
	double total;//时间差
};//每一本图书的信息内容
2. 图书管理功能

这是我们要实现的图书功能 

//初始化图书管理系统
void InitContact(struct Contact* ps);

//添加一本图书的信息
void AddContact(struct Contact* ps);

//删除一本图书的信息
void DeleteContact(struct Contact* ps);

//查找指定图书并打印其信息
int SreachContact(const struct Contact* ps);

//修改指定图书的信息
void ModifyContact(struct Contact* ps);

//打印图书管理系统中的全部信息
void ShowContact(const struct Contact* ps);

//通过名字排序图书管理系统中图书的先后顺序
void SortContact(struct Contact* ps);
2.1 初始化图书管理系统

对于图书的存储,我们可利用顺序表,链表等数据结构,因为对于图书管理系统,查阅和添加是很多的,所以我们这里使用顺序表。

顺序表: 

void InitContact(struct Contact* ps)
{
	memset(ps->data, 0, sizeof(ps->data));//将待用内存空间设置为0
	ps->size = 0;//设置图书管理系统最初只有0个元素
}
//图书管理系统类型
struct Contact
{
	struct Book data[MAX];//存放1000本图书的信息
	int size;//记录当前已有图书的本数
};
2.2 添加一本图书的信息

对于编号重复,查询图书的问题,我们创建FindByID和FindByName函数

顺序表ID的查询(FindByID)

int FindByID(int id, const struct Contact* ps)
{
	int i = 0;
	for (i = 0; i < ps->size; i++)
	{
		if (ps->data[i].id == id)
			return i;//找到了返回下标
	}
	return -1;//找不到的情况
}

顺序表name的查询 (FindByName)

//通过名字查找图书,找到了返回下标,没找到返回-1
static int FindByName(char name[MAX_NAME], const struct Contact* ps)
{
	int i = 0;
	for (i = 0; i < ps->size; i++)
	{
		if (strcmp(ps->data[i].name, name) == 0)
			return i;//找到了返回下标
	}
	return -1;//找不到的情况
}

 因为是顺序表的结构,我们很快的知道我们需要插入数组的下标size

注意:

每次的插入大小需要增加 1 ;size++;

图书状态初始化为 0 (未借出状态); opp = 0;

//添加一本图书的信息
void AddContact(struct Contact* ps)
{
	if (ps->size == MAX)
	{
		printf("图书管理系统已满,无法添加!\n");
	}//图书管理系统内图书本数已达上限
	else
	{
		printf("请输入书名:>");
		scanf("%s", ps->data[ps->size].name);
		printf("请输入编号:>");
		int id;
		scanf("%d", &(id));
		int i = FindByID(id, ps);
		if (i != -1) {
			printf("编号重复,请重新添加\n");
			Sleep(1000);
			system("cls");
			return;
		}
		ps->data[ps->size].id = id;
		printf("请输入作者:>");
		scanf("%s", ps->data[ps->size].author);
		printf("请输入出版社:>");
		scanf("%s", ps->data[ps->size].publish);
		ps->size++;//图书管理系统中图书本数+1
		printf("添加成功\n");
		ps->data[ps->size].opp = 0;
	}
	Sleep(1000);
	system("cls");
}
2.3 删除一本图书的信息
//删除一本图书的信息
void DeleteContact(struct Contact* ps)
{
	char name[MAX_NAME];
	printf("请输入要删除图书的书名:>");
	scanf("%s", name);
	//查找
	int pos = FindByName(name, ps);//找到了返回下标,没找到返回-1
	//删除
	if (pos == -1)
	{
		printf("查无此书\n");
	}
	else
	{
		int j = 0;
		for (j = pos; j < ps->size - 1; j++)
		{
			ps->data[j] = ps->data[j + 1];
		}//从要删除的图书位置开始,后一本图书信息覆盖前一本图书信息
		printf("删除成功\n");
		ps->size--;//图书管理系统中图书本数-1
	}
	Sleep(1000);
	system("cls");
}
2.4 查找指定图书并打印其信息
//查找指定图书并打印其信息
int SreachContact(const struct Contact* ps)
{
	char name[MAX_NAME];
	printf("请输入要查找图书的书名:>");
	scanf("%s", name);
	int pos = FindByName(name, ps);//找到了返回下标,没找到返回-1
	if (pos == -1)
	{
		printf("查无此书\n");
		return -1;
	}
	else
	{
		printf("%-15s %-4s    %-5s   %-12s\n", "书名", "编号", "作者", "出版社");
		printf("%-15s %-4d    %-5s   %-12s\n",
			ps->data[pos].name,
			ps->data[pos].id,
			ps->data[pos].author,
			ps->data[pos].publish);
		Sleep(1000);
		system("cls");
		return 1;
	}//打印该图书的信息内容
	Sleep(1000);
	system("cls");
}
2.5 修改指定图书的信息
//修改指定图书的信息
void ModifyContact(struct Contact* ps)
{
	char name[MAX_NAME];
	printf("请输入要修改的图书的书名:>");
	scanf("%s", name);
	int pos = FindByName(name, ps);//找到了返回下标,没找到返回-1
	if (pos == -1)
	{
		printf("查无此书\n");
	}
	else
	{
		printf("请输入书名:>");
		scanf("%s", ps->data[pos].name);
		printf("请输入编号:>");
		scanf("%d", &(ps->data[pos].id));
		printf("请输入作者:>");
		scanf("%s", ps->data[pos].author);
		printf("请输入出版社:>");
		scanf("%s", ps->data[pos].publish);

		printf("修改成功\n");
	}//修改图书信息,即将该图书信息重新录入
	Sleep(1000);
	system("cls");
}
2.6 打印图书管理系统中的全部信息
//打印图书管理系统中的全部信息
void ShowContact(const struct Contact* ps)
{
	if (ps->size == 0)
	{
		printf("图书管理系统为空\n");
	}//图书管理系统中图书本数为0
	else
	{
		//打印信息栏
		int i = 0;
		for (i = 0; i < ps->size; i++)
		{
			printf("%-15s %-4s    %-5s   %-12s\n", "书名", "编号", "作者", "出版社");
			printf("%-15s %-4d    %-5s   %-12s\n",
				ps->data[i].name,
				ps->data[i].id,
				ps->data[i].author,
				ps->data[i].publish);

		}//打印图书管理系统全部信息内容
	}
	Sleep(1000);
	system("cls");
}
2.7 通过名字排序图书管理系统中图书的先后顺序(可选)
//自定义的比较函数
int CmpByName(const void* e1, const void* e2)
{
	return strcmp((const char*)e1, (const char*)e2);
}

//通过名字排序图书管理系统中图书的先后顺序
void SortContact(struct Contact* ps)
{
	qsort(ps->data, ps->size, sizeof(struct Book), CmpByName);//排序
}

借阅系统

与图书管理系统类似,创建reader和borrow结构体 ,本程序的变量名过多,注意区分

struct borrow
{
	struct Book borrow_book [10];
	int limit_date;
};

struct reader
{
	char r_id[10];//学号
	char r_name[10];//姓名
	char r_pos[10];//老师或者学生
	char tec[20];//专业
	struct borrow borrow;//借书情况

};

//初始化图书管理系统
void Init_reader(struct borrow* ps);

void InitBorrow(b_reader* con);
//销毁
void DestroyBorrow(b_reader* con);

//添加读者
void AddB_reader(b_reader* con);

//借书
void borrow_book(b_reader* ps1, struct Contact* ps);

//归还书籍
void receive_b(b_reader* ps1, struct Contact* ps);

void search_B_R(struct Contact* ps);
1. 初始化

这里借用了book 

//初始化
void InitBorrow(b_reader* con) {
	SLInit(con);
}

//初始化借书系统
void Init_reader(struct borrow* ps) {
	memset(ps->borrow_book, 0, sizeof(ps->borrow_book));
	ps->limit_date = 0;
}
2. 销毁

由于引用之前的顺序表结构,这里就直接使用顺序表的销毁函数 

//销毁
void DestroyBorrow(b_reader* con) {

	SLDestroy(con);

}
3.添加读者

这里也存在重复学号的判断,我们只需要判断是否存在 ,判断返回是否为-1,如果是,则存在,返回。反之,则添加读者。

int FindBy_R_ID(b_reader* con, char ID[])
{
	for (int i = 0; i < con->size; i++)
	{
		if (0 == strcmp(con->arr[i].r_id, ID))
		{
			//找到了
			return i;
		}
	}
	//没有找到
	return -1;
}

//添加读者
void AddB_reader(b_reader* con) {

	struct reader info;
	Init_reader(&info.borrow);
	printf("请输入你的学号:>");
	char id[10];
	scanf("%s", id);
	int i = FindBy_R_ID(con, id);
	if (i != -1) {
		printf("学号已注册\n");
		Sleep(1000);
		system("cls");
		return;
	}
	strcpy(info.r_id, id);
	printf("请输入你的姓名:>");
	scanf("%s", info.r_name);
	printf("请输入你的职业:>");
	scanf("%s", info.r_pos);
	printf("请输入你的专业:>");
	scanf("%s", info.tec);
	//往通讯录中添加联系人数据
	printf("创建成功\n");
	SLPushBack(con, info);
	Sleep(1000);
	system("cls");
}
4. 借书系统

借书系统的难点在于对书籍是否存在,是否被借阅,读者是否超过借阅书籍数量,对数据状态的更新。

int FindBy_R_Name(b_reader* con, char name[])
{
	for (int i = 0; i < con->size; i++)
	{
		if (0 == strcmp(con->arr[i].r_name, name))
		{
			//找到了
			return i;
		}
	}
	//没有找到
	return -1;
}

//通过名字查找图书,找到了返回下标,没找到返回-1
static int FindByName(char name[MAX_NAME], const struct Contact* ps)
{
	int i = 0;
	for (i = 0; i < ps->size; i++)
	{
		if (strcmp(ps->data[i].name, name) == 0)
			return i;//找到了返回下标
	}
	return -1;//找不到的情况
}

//借书
void borrow_book(b_reader* ps1,struct Contact* ps) {
	char name[10] = {0};
	printf("请输入你的姓名:>");
	scanf("%s", name);
	//书是否存在
	printf("请输入你要借阅的书籍:>");
	char name_b[15];
	scanf("%s", name_b);
	int b = FindByName(name_b, ps);

	if (b == -1) {
		printf("查无此书\n");
		Sleep(1000);
		system("cls");
		return;
	}
	else {
		//存在
		 
		//书是否被借出
		if (ps->data[b].opp == 1) {
			printf("已被借出\n");
			Sleep(1000);
			system("cls");
			return;
		}
		//读者是否创建
		if (FindBy_R_Name(ps1,name) == -1) {
			printf("是否需要创建读者 y/n \n");
			char y;
			scanf("%c", &y);
			if (y == 'y') {
				AddB_reader(ps1);
			}
			Sleep(1000);
			system("cls");
			return;
		}
		//读者是否超过借书限制
		else {
			int i = FindBy_R_Name(ps1, name);
			if (ps1->arr[i].r_pos == "teacher") {
				if (ps1->arr[i].borrow.limit_date >= 8) {
					printf("你已超过借阅数量\n");
					Sleep(1000);
					system("cls");
					return;
				}
			}
			if (ps1->arr[i].r_pos == "studnt") {
				if (ps1->arr[i].borrow.limit_date >= 4) {
					printf("你已超过借阅数量\n");
					Sleep(1000);
					system("cls");
					return;
				}
			}
			//读者存在,更改内容
			*ps1->arr[i].borrow.borrow_book[ps1->arr->borrow.limit_date].name = *ps->data[b].name;
			*ps1->arr[i].borrow.borrow_book[ps1->arr->borrow.limit_date].author = *ps->data[b].author;
			ps1->arr[i].borrow.borrow_book[ps1->arr->borrow.limit_date].id = ps->data[b].id;
			*ps1->arr[i].borrow.borrow_book[ps1->arr->borrow.limit_date].publish = *ps->data[b].publish;
			strcpy(ps->data[b].reader_b, ps1->arr[i].r_name);
			//strcpy(ps->data[b].tec_b, ps1->arr[i].tec);
			ps->data[b].opp = 1;
			time(&(ps->data[b].start));
			printf("借阅成功\n");
			Sleep(1000);
			system("cls");
			return;
		}
	}
}
5. 归还系统

 归还系统难点在于time函数的使用。

void search_B_R(struct Contact* ps) {
	printf("请输入你要查询的书籍:>");
	char name[15];
	scanf("%s", &name);
	int i = FindByName(name, ps);
	if (i == -1) {
		printf("查无此书\n");
	}
	else {
		if (ps->data[i].opp == 0) {
			printf("无人借阅\n");
		}
		else {
			printf("书籍被%s借阅\n", ps->data[i].reader_b);
		}
	}
	Sleep(1000);
	system("cls");
}

//归还书籍
void receive_b(b_reader* ps1 ,struct Contact* ps) {
	char name_r[10] = { 0 };
	printf("请输入你的姓名:>");
	scanf("%s", name_r);
	int r = FindBy_R_Name(ps1, name_r);
	if (r == -1) {
		printf("查无此人\n");
		Sleep(1000);
		system("cls");
		return;
	}
	printf("请输入你要归还的书:>");
	char name_b[15];
	scanf("%s", name_b);
	int i = FindByName(name_b, ps);
	if (i == -1) {
		printf("查无此书\n");
		Sleep(1000);
		system("cls");
		return;
	}
	else {
		//读者下借阅的书归还
		int j = 0;
		for (j = 0; j < ps1->arr[r].borrow.limit_date; j++)
		{
			if (strcmp(ps1->arr[r].borrow.borrow_book[j].name, name_b) == 0)
				break;
		}
		for (; j < ps1->arr[r].borrow.limit_date-1; j++) {
			ps1->arr[r].borrow.borrow_book[j] = ps1->arr[r].borrow.borrow_book[j + 1];
		}
		ps1->arr[r].borrow.limit_date--;

		ps->data[i].opp = 0;
		*ps->data[i].reader_b = 0;
		time(&(ps->data[i].end));
		ps->data[i].total = (double)(ps->data[i].end - ps->data[i].start);
		if (ps->data[i].total < 1000) {
			printf("您以借阅%.1lf毫秒\n", ps->data[i].total);
		}
		else if (ps->data[i].total < 60000) {
			ps->data[i].total /= 1000;
			printf("您以借阅%.1lf秒\n", ps->data[i].total);
		}
		else if (ps->data[i].total < 3600000) {
			ps->data[i].total /= 60000;
			printf("您以借阅%.1lf分\n", ps->data[i].total);
		}
		else if (ps->data[i].total < 21600000) {
			ps->data[i].total /= 3600000;
			printf("您以借阅%.1lf小时\n", ps->data[i].total);
		}
		else if (ps->data[i].total < 51800000) {
			ps->data[i].total /= 21600000;
			printf("您以借阅%.1lf天\n", ps->data[i].total);
		}
		ps->data[i].end = 0;
		ps->data[i].start = 0;
		ps->data[i].total = 0;
		printf("归还成功,欢迎下次借阅\n");
		Sleep(1000);
		system("cls");
		return;
	}

}

看到这里,我们就完成了图书管理系统_借阅系统!!!

完整代码展示:

SeqList.h

#pragma once

#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include"reader.h"
//定义顺序表的结构

//#define N 100
//
静态顺序表
//struct SeqList
//{
//	int arr[N];
//	int size;//有效数据个数
//};

typedef struct reader SLDataType;
//动态顺序表
typedef struct SeqList
{
	SLDataType* arr;
	int size; //有效数据个数
	int capacity; //空间大小
}SL;

//typedef struct SeqList SL;

//顺序表初始化
void SLInit(SL* ps);
//顺序表的销毁
void SLDestroy(SL* ps);
void SLPrint(SL s);

//头部插入删除 / 尾部插入删除
void SLPushBack(SL* ps, SLDataType x);
void SLPushFront(SL* ps, SLDataType x);

void SLPopBack(SL* ps);
void SLPopFront(SL* ps);

//指定位置之前插入/删除数据
void SLInsert(SL* ps, int pos, SLDataType x);
void SLErase(SL* ps, int pos);
int SLFind(SL* ps, SLDataType x);

SeqList.c

#include"SeqList.h"
void SLInit(SL* ps)
{
	ps->arr = NULL;
	ps->size = ps->capacity = 0;
}
//顺序表的销毁
void SLDestroy(SL* ps)
{
	if (ps->arr) //等价于  if(ps->arr != NULL)
	{
		free(ps->arr);
	}
	ps->arr = NULL;
	ps->size = ps->capacity = 0;
}
void SLCheckCapacity(SL* ps)
{
	//插入数据之前先看空间够不够
	if (ps->capacity == ps->size)
	{
		//申请空间
		//malloc calloc realloc  int arr[100] --->增容realloc
		//三目表达式
		int newCapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;
		SLDataType* tmp = (SLDataType*)realloc(ps->arr, newCapacity * sizeof(SLDataType));//要申请多大的空间
		if (tmp == NULL)
		{
			perror("realloc fail!");
			exit(1);//直接退出程序,不再继续执行
		}
		//空间申请成功
		ps->arr = tmp;
		ps->capacity = newCapacity;
	}
}
//尾插
void SLPushBack(SL* ps, SLDataType x)
{
	温柔的解决方式
	//if (ps == NULL)
	//{
	//	return;
	//}
	assert(ps); //等价与assert(ps != NULL)

	//ps->arr[ps->size] = x;
	//++ps->size;
	SLCheckCapacity(ps);
	ps->arr[ps->size++] = x;
}
//头插
void SLPushFront(SL* ps, SLDataType x)
{
	assert(ps);
	SLCheckCapacity(ps);
	//先让顺序表中已有的数据整体往后挪动一位
	for (int i = ps->size; i > 0; i--)
	{
		ps->arr[i] = ps->arr[i - 1];//arr[1] = arr[0]
	}
	ps->arr[0] = x;
	ps->size++;
}

void SLPrint(SL s)
{
	for (int i = 0; i < s.size; i++)
	{
		printf("%d ", s.arr[i]);
	}
	printf("\n");
}
void SLPopBack(SL* ps)
{
	assert(ps);
	assert(ps->size);
	//顺序表不为空
	//ps->arr[ps->size - 1] = -1;
	--ps->size;
}
void SLPopFront(SL* ps)
{
	assert(ps);
	assert(ps->size);

	//数据整体往前挪动一位
	for (int i = 0; i < ps->size - 1; i++)
	{
		ps->arr[i] = ps->arr[i + 1]; //arr[size-2] = arr[size-1]
	}
	ps->size--;
}
//指定位置的删除
void SLErase(SL* ps, int pos)
{
	assert(ps);
	assert(pos >= 0 && pos < ps->size);

	for (int i = pos; i < ps->size - 1; i++)
	{
		ps->arr[i] = ps->arr[i + 1];
	}
	ps->size--;
}

reader.h

#pragma once
#include"book.h"
#include"SeqList.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef  struct SeqList b_reader;

struct borrow
{
	struct Book borrow_book [10];
	int limit_date;
};

struct reader
{
	char r_id[10];//学号
	char r_name[10];//姓名
	char r_pos[10];//老师或者学生
	char tec[20];//专业
	struct borrow borrow;//借书情况

};

//初始化图书管理系统
void Init_reader(struct borrow* ps);

void InitBorrow(b_reader* con);
//销毁
void DestroyBorrow(b_reader* con);

//添加读者
void AddB_reader(b_reader* con);

//借书
void borrow_book(b_reader* ps1, struct Contact* ps);

//归还书籍
void receive_b(b_reader* ps1, struct Contact* ps);

void search_B_R(struct Contact* ps);

reader.c

#define _CRT_SECURE_NO_WARNINGS  1

#include"reader.h"
#include<Windows.h>

//初始化
void InitBorrow(b_reader* con) {
	SLInit(con);
}

//初始化借书系统
void Init_reader(struct borrow* ps) {
	memset(ps->borrow_book, 0, sizeof(ps->borrow_book));
	ps->limit_date = 0;
}

//销毁
void DestroyBorrow(b_reader* con) {

	SLDestroy(con);

}

int FindBy_R_ID(b_reader* con, char ID[])
{
	for (int i = 0; i < con->size; i++)
	{
		if (0 == strcmp(con->arr[i].r_id, ID))
		{
			//找到了
			return i;
		}
	}
	//没有找到
	return -1;
}

//添加读者
void AddB_reader(b_reader* con) {

	struct reader info;
	Init_reader(&info.borrow);
	printf("请输入你的学号:>");
	char id[10];
	scanf("%s", id);
	int i = FindBy_R_ID(con, id);
	if (i != -1) {
		printf("学号已注册\n");
		Sleep(1000);
		system("cls");
		return;
	}
	strcpy(info.r_id, id);
	printf("请输入你的姓名:>");
	scanf("%s", info.r_name);
	printf("请输入你的职业:>");
	scanf("%s", info.r_pos);
	printf("请输入你的专业:>");
	scanf("%s", info.tec);
	//往通讯录中添加联系人数据
	printf("创建成功\n");
	SLPushBack(con, info);
	Sleep(1000);
	system("cls");
}

int FindBy_R_Name(b_reader* con, char name[])
{
	for (int i = 0; i < con->size; i++)
	{
		if (0 == strcmp(con->arr[i].r_name, name))
		{
			//找到了
			return i;
		}
	}
	//没有找到
	return -1;
}

//通过名字查找图书,找到了返回下标,没找到返回-1
static int FindByName(char name[MAX_NAME], const struct Contact* ps)
{
	int i = 0;
	for (i = 0; i < ps->size; i++)
	{
		if (strcmp(ps->data[i].name, name) == 0)
			return i;//找到了返回下标
	}
	return -1;//找不到的情况
}

//借书
void borrow_book(b_reader* ps1,struct Contact* ps) {
	char name[10] = {0};
	printf("请输入你的姓名:>");
	scanf("%s", name);
	//书是否存在
	printf("请输入你要借阅的书籍:>");
	char name_b[15];
	scanf("%s", name_b);
	int b = FindByName(name_b, ps);

	if (b == -1) {
		printf("查无此书\n");
		Sleep(1000);
		system("cls");
		return;
	}
	else {
		//存在
		 
		//书是否被借出
		if (ps->data[b].opp == 1) {
			printf("已被借出\n");
			Sleep(1000);
			system("cls");
			return;
		}
		//读者是否创建
		if (FindBy_R_Name(ps1,name) == -1) {
			printf("是否需要创建读者 y/n \n");
			char y;
			scanf("%c", &y);
			if (y == 'y') {
				AddB_reader(ps1);
			}
			Sleep(1000);
			system("cls");
			return;
		}
		//读者是否超过借书限制
		else {
			int i = FindBy_R_Name(ps1, name);
			if (ps1->arr[i].r_pos == "teacher") {
				if (ps1->arr[i].borrow.limit_date >= 8) {
					printf("你已超过借阅数量\n");
					Sleep(1000);
					system("cls");
					return;
				}
			}
			if (ps1->arr[i].r_pos == "studnt") {
				if (ps1->arr[i].borrow.limit_date >= 4) {
					printf("你已超过借阅数量\n");
					Sleep(1000);
					system("cls");
					return;
				}
			}
			//读者存在,更改内容
			*ps1->arr[i].borrow.borrow_book[ps1->arr->borrow.limit_date].name = *ps->data[b].name;
			*ps1->arr[i].borrow.borrow_book[ps1->arr->borrow.limit_date].author = *ps->data[b].author;
			ps1->arr[i].borrow.borrow_book[ps1->arr->borrow.limit_date].id = ps->data[b].id;
			*ps1->arr[i].borrow.borrow_book[ps1->arr->borrow.limit_date].publish = *ps->data[b].publish;
			strcpy(ps->data[b].reader_b, ps1->arr[i].r_name);
			//strcpy(ps->data[b].tec_b, ps1->arr[i].tec);
			ps->data[b].opp = 1;
			time(&(ps->data[b].start));
			printf("借阅成功\n");
			Sleep(1000);
			system("cls");
			return;
		}
	}
}

void search_B_R(struct Contact* ps) {
	printf("请输入你要查询的书籍:>");
	char name[15];
	scanf("%s", &name);
	int i = FindByName(name, ps);
	if (i == -1) {
		printf("查无此书\n");
	}
	else {
		if (ps->data[i].opp == 0) {
			printf("无人借阅\n");
		}
		else {
			printf("书籍被%s借阅\n", ps->data[i].reader_b);
		}
	}
	Sleep(1000);
	system("cls");
}

//归还书籍
void receive_b(b_reader* ps1 ,struct Contact* ps) {
	char name_r[10] = { 0 };
	printf("请输入你的姓名:>");
	scanf("%s", name_r);
	int r = FindBy_R_Name(ps1, name_r);
	if (r == -1) {
		printf("查无此人\n");
		Sleep(1000);
		system("cls");
		return;
	}
	printf("请输入你要归还的书:>");
	char name_b[15];
	scanf("%s", name_b);
	int i = FindByName(name_b, ps);
	if (i == -1) {
		printf("查无此书\n");
		Sleep(1000);
		system("cls");
		return;
	}
	else {
		//读者下借阅的书归还
		int j = 0;
		for (j = 0; j < ps1->arr[r].borrow.limit_date; j++)
		{
			if (strcmp(ps1->arr[r].borrow.borrow_book[j].name, name_b) == 0)
				break;
		}
		for (; j < ps1->arr[r].borrow.limit_date-1; j++) {
			ps1->arr[r].borrow.borrow_book[j] = ps1->arr[r].borrow.borrow_book[j + 1];
		}
		ps1->arr[r].borrow.limit_date--;

		ps->data[i].opp = 0;
		*ps->data[i].reader_b = 0;
		time(&(ps->data[i].end));
		ps->data[i].total = (double)(ps->data[i].end - ps->data[i].start);
		if (ps->data[i].total < 1000) {
			printf("您以借阅%.1lf毫秒\n", ps->data[i].total);
		}
		else if (ps->data[i].total < 60000) {
			ps->data[i].total /= 1000;
			printf("您以借阅%.1lf秒\n", ps->data[i].total);
		}
		else if (ps->data[i].total < 3600000) {
			ps->data[i].total /= 60000;
			printf("您以借阅%.1lf分\n", ps->data[i].total);
		}
		else if (ps->data[i].total < 21600000) {
			ps->data[i].total /= 3600000;
			printf("您以借阅%.1lf小时\n", ps->data[i].total);
		}
		else if (ps->data[i].total < 51800000) {
			ps->data[i].total /= 21600000;
			printf("您以借阅%.1lf天\n", ps->data[i].total);
		}
		ps->data[i].end = 0;
		ps->data[i].start = 0;
		ps->data[i].total = 0;
		printf("归还成功,欢迎下次借阅\n");
		Sleep(1000);
		system("cls");
		return;
	}

}

book.h

#pragma once

#define _CRT_SECURE_NO_WARNINGS 1

#define MAX 10000

#define MAX_NAME 15
#define MAX_ID 5
#define MAX_AUTHOR 12
#define MAX_PUBLISH 20

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


enum Option
{
	EXIT,//0
	ADD,//1
	DELETE,//2
	SREACH,//3
	MODIFY,//4
	SHOW,//5
	SORT//6
};//增加代码可读性

struct Book
{
	char name[MAX_NAME];
	int id;
	char author[MAX_AUTHOR];
	char publish[MAX_PUBLISH];
	int opp;//1被借出//0没有被借出
	char reader_b[15];
	//char tec_b[20];//专业
	time_t start;
	time_t end;
	double total;
};//每一本图书的信息内容

//图书管理系统类型
struct Contact
{
	struct Book data[MAX];//存放1000本图书的信息
	int size;//记录当前已有图书的本数
};


//初始化图书管理系统
void InitContact(struct Contact* ps);

//添加一本图书的信息
void AddContact(struct Contact* ps);

//删除一本图书的信息
void DeleteContact(struct Contact* ps);

//查找指定图书并打印其信息
int SreachContact(const struct Contact* ps);

//修改指定图书的信息
void ModifyContact(struct Contact* ps);

//打印图书管理系统中的全部信息
void ShowContact(const struct Contact* ps);

//通过名字排序图书管理系统中图书的先后顺序
void SortContact(struct Contact* ps);

book.c

#define _CRT_SECURE_NO_WARNINGS  1
#include"book.h"
#include<Windows.h>
//初始化图书管理系统
void InitContact(struct Contact* ps)
{
	memset(ps->data, 0, sizeof(ps->data));//将待用内存空间设置为0
	ps->size = 0;//设置图书管理系统最初只有0个元素
}

//通过名字查找图书,找到了返回下标,没找到返回-1
static int FindByName(char name[MAX_NAME], const struct Contact* ps)
{
	int i = 0;
	for (i = 0; i < ps->size; i++)
	{
		if (strcmp(ps->data[i].name, name) == 0)
			return i;//找到了返回下标
	}
	return -1;//找不到的情况
}

int FindByID(int id, const struct Contact* ps)
{
	int i = 0;
	for (i = 0; i < ps->size; i++)
	{
		if (ps->data[i].id == id)
			return i;//找到了返回下标
	}
	return -1;//找不到的情况
}

//添加一本图书的信息
void AddContact(struct Contact* ps)
{
	if (ps->size == MAX)
	{
		printf("图书管理系统已满,无法添加!\n");
	}//图书管理系统内图书本数已达上限
	else
	{
		printf("请输入书名:>");
		scanf("%s", ps->data[ps->size].name);
		printf("请输入编号:>");
		int id;
		scanf("%d", &(id));
		int i = FindByID(id, ps);
		if (i != -1) {
			printf("编号重复,请重新添加\n");
			Sleep(1000);
			system("cls");
			return;
		}
		ps->data[ps->size].id = id;
		printf("请输入作者:>");
		scanf("%s", ps->data[ps->size].author);
		printf("请输入出版社:>");
		scanf("%s", ps->data[ps->size].publish);
		ps->size++;//图书管理系统中图书本数+1
		printf("添加成功\n");
		ps->data[ps->size].opp = 0;
	}
	Sleep(1000);
	system("cls");
}

//删除一本图书的信息
void DeleteContact(struct Contact* ps)
{
	char name[MAX_NAME];
	printf("请输入要删除图书的书名:>");
	scanf("%s", name);
	//查找
	int pos = FindByName(name, ps);//找到了返回下标,没找到返回-1
	//删除
	if (pos == -1)
	{
		printf("查无此书\n");
	}
	else
	{
		int j = 0;
		for (j = pos; j < ps->size - 1; j++)
		{
			ps->data[j] = ps->data[j + 1];
		}//从要删除的图书位置开始,后一本图书信息覆盖前一本图书信息
		printf("删除成功\n");
		ps->size--;//图书管理系统中图书本数-1
	}
	Sleep(1000);
	system("cls");
}

//查找指定图书并打印其信息
int SreachContact(const struct Contact* ps)
{
	char name[MAX_NAME];
	printf("请输入要查找图书的书名:>");
	scanf("%s", name);
	int pos = FindByName(name, ps);//找到了返回下标,没找到返回-1
	if (pos == -1)
	{
		printf("查无此书\n");
		return -1;
	}
	else
	{
		printf("%-15s %-4s    %-5s   %-12s\n", "书名", "编号", "作者", "出版社");
		printf("%-15s %-4d    %-5s   %-12s\n",
			ps->data[pos].name,
			ps->data[pos].id,
			ps->data[pos].author,
			ps->data[pos].publish);
		Sleep(1000);
		system("cls");
		return 1;
	}//打印该图书的信息内容
	Sleep(1000);
	system("cls");
}

//修改指定图书的信息
void ModifyContact(struct Contact* ps)
{
	char name[MAX_NAME];
	printf("请输入要修改的图书的书名:>");
	scanf("%s", name);
	int pos = FindByName(name, ps);//找到了返回下标,没找到返回-1
	if (pos == -1)
	{
		printf("查无此书\n");
	}
	else
	{
		printf("请输入书名:>");
		scanf("%s", ps->data[pos].name);
		printf("请输入编号:>");
		scanf("%d", &(ps->data[pos].id));
		printf("请输入作者:>");
		scanf("%s", ps->data[pos].author);
		printf("请输入出版社:>");
		scanf("%s", ps->data[pos].publish);

		printf("修改成功\n");
	}//修改图书信息,即将该图书信息重新录入
	Sleep(1000);
	system("cls");
}

//打印图书管理系统中的全部信息
void ShowContact(const struct Contact* ps)
{
	if (ps->size == 0)
	{
		printf("图书管理系统为空\n");
	}//图书管理系统中图书本数为0
	else
	{
		//打印信息栏
		int i = 0;
		for (i = 0; i < ps->size; i++)
		{
			printf("%-15s %-4s    %-5s   %-12s\n", "书名", "编号", "作者", "出版社");
			printf("%-15s %-4d    %-5s   %-12s\n",
				ps->data[i].name,
				ps->data[i].id,
				ps->data[i].author,
				ps->data[i].publish);

		}//打印图书管理系统全部信息内容
	}
	Sleep(1000);
	system("cls");
}

//自定义的比较函数
int CmpByName(const void* e1, const void* e2)
{
	return strcmp((const char*)e1, (const char*)e2);
}

//通过名字排序图书管理系统中图书的先后顺序
void SortContact(struct Contact* ps)
{
	qsort(ps->data, ps->size, sizeof(struct Book), CmpByName);//排序
}

test.c

#define _CRT_SECURE_NO_WARNINGS  1
#include<stdio.h>
#include"book.h"
#include"reader.h"
#include <stdlib.h>
#include<Windows.h>

void menu()
{
	printf("|-----------------------|\n");
	printf("|        contact        |\n");
	printf("|   1.book     2.reader |\n");
	printf("|        0.Exit         |\n");
	printf("|-----------------------|\n");
}//打印菜单

void menu_b()
{
	printf("|-----------------------|\n");
	printf("|        contact        |\n");
	printf("|   1.Add     2.Delete  |\n");
	printf("|   3.Search  4.Modify  |\n");
	printf("|   5.Show    6.Sort    |\n");
	printf("|       0.Return        |\n");
	printf("|-----------------------|\n");
}//打印菜单

void menu_r()
{
	printf("|-----------------------|\n");
	printf("|        contact        |\n");
	printf("|   1.Add     2.Search  |\n");
	printf("|   3.Lend    4.receive |\n");
	printf("|       0.Return        |\n");
	printf("|-----------------------|\n");
}//打印菜单


int main()
{
	int input = 0;
	int input_r = 0;
	int input_b = 0;
	//创建图书管理系统
	struct Contact con;//con就是图书管理系统,里面包含1000个元素的数组和size
	//初始化图书管理系统
	InitContact(&con);
	b_reader con_r; 
	InitBorrow(&con_r);
	do
	{
		menu();//主菜单
		printf("请选择:>");
		scanf("%d", &input);
		Sleep(1000);
		system("cls");
		switch (input)
		{
		case 1: {
			do
			{
				menu_b();
				printf("请选择:>");
				scanf("%d", &input_b);
				Sleep(1000);
				system("cls");
				switch (input_b)
				{
				case ADD:
					AddContact(&con);
					break;
				case DELETE:
					DeleteContact(&con);
					break;
				case SREACH:
					SreachContact(&con);
					break;
				case MODIFY:
					ModifyContact(&con);
					break;
				case SHOW:
					ShowContact(&con);
					break;
				case SORT:
					SortContact(&con);
					break;
				case EXIT: {
					printf("退出图书管理系统\n");
					Sleep(1000);
					system("cls");
					break;
				}
				default: {
					printf("选择错误,请重新选择\n");
					Sleep(1000);
					system("cls");
					break;
				}
				}
			} while (input_b);
			break;
		}
		case 2:
		{
			do {
				//读者管理
				menu_r();
				printf("请选择:>");
				scanf("%d", &input_r);
				Sleep(1000);
				system("cls");
				switch (input_r)
				{
				case 1:
					AddB_reader(&con_r);
					break;
				case 2:
					search_B_R(&con);
					break;
				case 3:
					borrow_book(&con_r, &con);
					break;
				case 4:
					receive_b(&con_r, &con);
					break;
				case 0:
					printf("退出读者管理系统\n");
					Sleep(1000);
					system("cls");
					break;
				default:
					printf("选择错误,请重新选择\n");
					Sleep(1000);
					system("cls");
					break;
				}
			} while (input_r);
			break;
		}

		case 0:
		{
			break;
		}
		}
	} while (input);
	return 0;
}

http://www.kler.cn/a/572909.html

相关文章:

  • E22-xxxT22D lora模块配置
  • OpenFeign 学习笔记
  • 并查集—数组实现
  • 【Linux】进程间通信 续
  • 美颜SDK架构揭秘:人脸美型API的底层实现与优化策略
  • 立体仓WMS同MES制造的协同
  • upload-labs Pass5-18 文件上传
  • 观察者模式的C++实现示例
  • 从零开始的kafka学习 (一)| 概念,Java API
  • 从vue源码解析Vue.set()和this.$set()
  • 深入浅出:UniApp 从入门到精通全指南
  • 360图片搜索爬虫|批量爬取搜索图片
  • 关于在vue3中的动态组件component标签上给ref属性动态赋值的问题
  • Java进阶-SpringCloud设计模式-工厂模式的设计与详解
  • 原型链与继承
  • 【RAG 篇】万字长文:向量数据库选型指南 —— Milvus 与 FAISS/Pinecone/Weaviate 等工具深度对比
  • 软考架构师笔记-进程管理
  • 自动驾驶---不依赖地图的大模型轨迹预测
  • AI与.NET技术实操系列
  • Python:函数的各类参数以及函数嵌套