C语言课程设计——图书馆查询系统

这是一个简单的图书管理系统,用户可以查询图书信息、借取和归还图书,并查看图书热度排行榜。系统使用C语言实现,通过文件存储图书数据,支持添加图书、按名称查找、借阅和归还操作,以及对图书热度进行冒泡排序展示。
摘要由CSDN通过智能技术生成

为了方便图书查询,我们编写了这个图书馆查询系统,通过将图书的信息输入进这个系统,我们便可以直到图书的相关情况。图书管理员存入图书信息,使用者可以通过选项实现查询图书借阅信息,借取图书,归还图书,查看图书热度榜等操作。

#define _CRT_SECURE_NO_WARNINGS
#include "stdio.h"
#include "string.h"
#include "windows.h"
#include "stdlib.h"
struct book
{
	char book_name[20];
	char book_num[20];
	char date[11]="0000/00/00"; 
	int flag=1;
	int heat=0;
};
void search(struct book *a,char b[20],int n)
{
	int t = 0;
	for (int i = 0; i < n; i++)
	{
		if (strcmp(b, a[i].book_name) == 0)
		{
			t++;
			printf("\n查找结果为:(图书名称 图书编号 上次借出日期 状态)\n%s %s %s ", a[i].book_name, a[i].book_num,a[i].date);         //查找书籍数据
			if (a[i].flag==1)
			{
				printf("该书未借出\n\n");
			}
			else
			{
				printf("该书已借出\n\n");
			}
		}
	}
	if (t==0)
	{
		printf("没有该图书信息\n");
	}
}
void borrow(struct book *a,int n)
{
	printf("请输入您要借取的图书编号:\n");
	char name[20];
	scanf("%s", name);
	int x = 0;
	for (int i = 0; i < n; i++)
	{
		if (strcmp(name, a[i].book_num) == 0)
		{
			if (a[i].flag==1)
			{
				x++;
				a[i].flag = 0;
				a[i].heat++;
				printf("\n请填写借出日期:(如2020/10/31)\n");
				scanf("%s", a[i].date);
				printf("\n恭喜你成功借阅!\n\n");
			}
			else
			{
				printf("\n这本书已经借出!\n\n");
			}
		}
	}
	if (x==0)
	{
		printf("\n该书不存在!\n\n");
	}
}
void returnbook(struct book* a, int n)
{
	printf("请输入您要归还的图书编号:\n");
	char name[20];
	int t = 0;
	scanf("%s", name);
	for (int i = 0; i < n; i++)
	{
		if (strcmp(name, a[i].book_num) == 0&&a[i].flag==0)
		{
			t++;
			a[i].flag = 1;
			printf("\n恭喜你成功归还!\n\n");
		}
	}
	if (t==0)
	{
		printf("\n该书未借出或图书编号错误!\n\n");
	}
}
void chart(struct book *a,int n)
{
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < n-1; j++)
		{
			if (a[j].heat<a[j+1].heat)
			{
				struct book x = a[j+1];
				a[j + 1] = a[j];
				a[j] = x;                          //冒泡排序
			}
		}
	}
	for (int i = 0; i < 5; i++)
	{
		printf("NO.%d\t%s\t%d\n\n", i+1, a[i].book_name, a[i].heat);
	}
}
void sure()
{
	int x;
	printf("点击1和回车键返回菜单\n");
	scanf("%d", &x);
	if (x==1)
	{
		system("cls");
	}
}
void menu()
{
	printf("-=======================================================================================================-\n");
	printf("|                                 **********1.图书库*****************                                   |\n");
	printf("|                                 **********2.查询图书***************                                   |\n");
	printf("|                                 **********3.借取图书***************                                   |\n");
	printf("|                                 **********4.归还图书***************                                   |\n");
	printf("|                                 **********5.图书热度排行榜*********                                   |\n");
	printf("|                                 **********6.退出系统***************                                   |\n");
	printf("-=======================================================================================================-\n");
	
}
int main()
{
	printf("==================================欢迎光临图书管理系统==================================\n\n");
	int total;//文件中书籍数量
	printf("请输入图书总量:\n");
	scanf("%d", &total);
	printf("\n请输入图书库图书数据:(如三体 01)\n");
	FILE* fp;
	if ((fp = fopen("book.txt", "w+")) == NULL)
	{
		printf("\nCan't open this file!\n");
		exit(0);
	}
	struct book a[50], b[50];
	for (int i = 0; i < total; i++)
	{
		scanf("%s%s", a[i].book_name, a[i].book_num);
	}
	fwrite(a, sizeof(struct book), total, fp);
	rewind(fp);
	fread(b, sizeof(struct book), total, fp);
	printf("\n输入的图书数据为:(图书名称 图书编号 上次借出日期 状态 热度)\n");
	for (int i = 0; i < total; i++)
	{
		printf("%s\t%s\t%s\t%d\t%d\n", b[i].book_name, b[i].book_num, b[i].date, b[i].flag, b[i].heat);
	}
	fclose(fp);                                               //建立文件,输入书籍数据
	printf("\n5秒后进入菜单界面...");
	Sleep(5000);
	system("cls");
	loop: int choose;
	menu();
	printf("你想进行的操作是:\n");
	scanf("%d", &choose);
	int add;                        //添加的图书量
	struct book c[50], d[50];
	char find[20];                 //要查找的数据
	struct book data[55];
	if ((fp = fopen("book.txt", "a+")) == NULL)
	{
		printf("\nCan't open this file!\n");
		exit(0);
	}
	fread(data, sizeof(struct book), total, fp);
	fclose(fp);                     //将文件中的数据存入data
	switch (choose)
	{
	 case 1:
		system("cls");
		printf("/***图书库***/\n\n");
		printf("请输入要添加的图书量:\n");
		scanf("%d", &add);
		total = total + add;          //新的总量
		if ((fp = fopen("book.txt", "a+")) == NULL)
		{
			printf("\nCan't open this file!\n");
			exit(0);
		}
		printf("\n请输入需要添加的图书数据:(如三体 01)\n");
		for (int i = 0; i < add; i++)
		{
			scanf("%s%s", c[i].book_name, c[i].book_num);
		}
		fseek(fp, 0, SEEK_END);
		fwrite(c, sizeof(struct book), add, fp);
		rewind(fp);
		fread(d, sizeof(struct book), total, fp);
		printf("\n更新后的图书数据为:(图书名称 图书编号 上次借出日期 状态 热度)\n");
		for (int i = 0; i < total; i++)
		{
			printf("%s\t%s\t%s\t%d\t%d\n", d[i].book_name, d[i].book_num, d[i].date, d[i].flag, d[i].heat);
		}
		fclose(fp);         //向文件中添加书籍数据
		sure();
		goto loop;
	 case 2:
		system("cls");
		printf("/***查询图书***/\n\n");
		printf("请输入您要查找的图书名称:\n");
		scanf("%s", find);
		search(data,find,total);
		sure();
		goto loop;
	 case 3:
		system("cls");
		printf("/***借取图书***/\n\n");
		borrow(data,total);
		if ((fp = fopen("book.txt", "w+")) == NULL)
		{
			printf("\nCan't open this file!\n");
			exit(0);
		}
		fwrite(data, sizeof(struct book), total, fp);
		rewind(fp);
		fclose(fp);                                  //向文件中添加书籍数据
		sure();
		goto loop;
	 case 4:
		system("cls");
		printf("/***归还图书***/\n\n");
		returnbook(data,total);
		if ((fp = fopen("book.txt", "w+")) == NULL)
		{
			printf("\nCan't open this file!\n");
			exit(0);
		}
		fwrite(data, sizeof(struct book), total, fp);
		rewind(fp);
		fclose(fp);                                  //向文件中添加书籍数据
		sure();
		goto loop;
	 case 5:
		system("cls");
		printf("/***图书热度排行榜***/\n\n");
		chart(data,total);
		sure();
		goto loop;
	 case 6:
		system("cls");
		printf("-=======================================================================================================-\n");
		printf("|                                 ************************************                                  |\n");
		printf("|                                 ************************************                                  |\n");
		printf("|                                 **********感谢您的使用!************                                  |\n");
		printf("|                                 ************************************                                  |\n");
		printf("|                                 ************************************                                  |\n");
		printf("-=======================================================================================================-\n");
		break;
	 default:
		 system("cls");
		 printf("请输入正确的序号!\n\n");
		 sure();
		 goto loop;
	}
	return 0;
}

  以上代码在Visual Studio 下运行

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值