栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

(C++)使用链表编写图书管理系统

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

(C++)使用链表编写图书管理系统

一、程序实现功能:

1.录入书籍:将书籍录入图书管理系统

2.浏览书籍:查看图书管理系统里的所有书籍

3.借阅书籍:书籍存在可以借阅,库存-1,书的库存不足则无法借阅

4.归还书籍:库存+1,如果该书不是图书馆里的书籍,则无法录入

5.删除书籍:以书名为基础从图书管理系统中删除该书籍

6.查找书籍:按书名查找书籍,显示书籍的基本信息

7.排序书籍:按价格将书籍排序(降序)

二、要求

使用函数、指针和链表编写。

三、程序功能图

 

四、具体函数

 

五、程序代码

#include
#include
#include
 
struct bookinfo
{
	char name[20];   //书名
	char author[10]; //作者
	char date[20];   //出版日期
	float price;     //价格
	int num;         //数量
};
 
struct Node
{
	struct bookinfo data;
	struct Node* next;
};


struct Node* list = NULL;


struct Node* createhead()
{
	
	struct Node* headNode = (struct Node*)malloc(sizeof(struct Node));
    headNode->next = NULL;
	return headNode;
}


struct Node* createNode(struct bookinfo data)
{
	struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
	newNode->data = data;
	newNode->next = NULL;
	return newNode;
}

void printList();
void display_menu();
void savebookfile();
void insertbook();
void readbookfile();
void deletebook();
struct Node* searchbook();
void sortbook();
void selectkey();
 

void printList(struct Node* headNode)
{
	struct Node* Bmove = headNode->next;
	printf("书名t作者t出版日期t价格t库存n");
	while(Bmove != NULL)
	{
		printf("%st%st%st%.1ft%dn",Bmove->data.name,Bmove->data.author,Bmove->data.date,Bmove->data.price,Bmove->data.num);
		Bmove = Bmove->next;
	}

}
 

void display_menu()
{
	char str[100];
	FILE *fp;
	char *txt;
	fp = fopen("menu.txt","r");
	txt = fgets(str,100,fp);
	while(txt != NULL)
	{
    printf("%s",str);
	txt = fgets(str,100,fp);
	}
	fclose(fp);
}
 

void savebookfile(const char* filename,struct Node* headNode)
{
	FILE* fp = fopen(filename,"w");
    struct Node* Bmove = headNode->next;
	while(Bmove != NULL)
	{
		fprintf(fp,"%st%st%st%.1ft%dn",Bmove->data.name,Bmove->data.author,Bmove->data.date,Bmove->data.price,Bmove->data.num);
	    Bmove = Bmove->next;
	}
	fclose(fp);
}


void insertbook(struct Node* headNode,struct bookinfo data)
{
	struct Node* newNode = createNode(data);
	newNode->next = headNode->next;
	headNode->next = newNode;

}


void readbookfile(const char* filename, struct Node* headNode)
{   
	FILE* fp = fopen(filename,"r");
    if(fp == NULL)
	{
		fp = fopen(filename,"w+");
	}
	struct bookinfo tempinfo;
	while(fscanf(fp, "%st%st%st%.1ft%dn",tempinfo.name,&tempinfo.author,&tempinfo.date,&tempinfo.price,&tempinfo.num ) != EOF)
	{
		insertbook(list,tempinfo);
	}
	fclose(fp);
}


void deletebook(struct Node* headNode,char *bookname)
{
	struct Node* leftNode = headNode;
	struct Node* rightNode = headNode->next;
	while(rightNode != NULL && strcmp(rightNode->data.name,bookname))
	{
		leftNode = rightNode;
		rightNode = leftNode->next;
	}
	if(leftNode == NULL)
	{
		return;
	}
	else
	{
        printf("删除书籍成功!n");
		leftNode->next = rightNode->next;
		free(rightNode);
		rightNode = NULL;
	}
}


struct Node* searchbook(struct Node* headNode, char* bookname)
{
	struct Node* rightNode = headNode->next;
	while (rightNode != NULL && strcmp(rightNode->data.name, bookname))
	{
		rightNode = rightNode->next;
	}
	return rightNode;
}
 

void sortbook(struct Node* headNode)
{
	for(struct Node* i = headNode->next; i != NULL; i = i->next)
	{
		for(struct Node* j = headNode->next;j->next != NULL;j = j->next)
		{
			
			if (j->data.price < j->next->data.price) 
			{
				
				struct bookinfo tempdata = j->data;
				j->data = j->next->data;
				j->next->data = tempdata;
			}
		}
	}
	
	printList(headNode);
}
 

void selectkey()
{
	int userkey = 0;
	struct bookinfo tempbook;  //生成一个临时的变量存储书籍信息
	struct Node* searchname = NULL; //生成一个临时变量存储查找的书名
	struct Node* borrowbook = NULL; //生成一个临时变量存储要借阅的书名
    struct Node* returnbook = NULL; //生成一个临时变量存储要归还的书名
	scanf("%d",&userkey);
	switch(userkey)
	{
	case 1:
		printf("[ 录入书籍 ]n");
		printf("输入书籍的信息(name,author,date,price,num):");
        scanf("%s%s%s%f%d",tempbook.name,&tempbook.author,&tempbook.date,&tempbook.price,&tempbook.num);
		insertbook(list,tempbook);
		
		savebookfile("bookinfo.txt",list);
		break;
	case 2:
		printf("[ 浏览书籍 ]n");
		printList(list);
		break;
	case 3:
		printf("[ 借阅书籍 ]n");   
		printf("请输入要借阅的书名:");
        scanf("%s",tempbook.name);
		borrowbook = searchbook(list,tempbook.name);
		if(borrowbook == NULL)
		{
			printf("不存在该书,无法借阅!n");
		}
		else
		{
			if(borrowbook->data.num > 0)
			{
				borrowbook->data.num--;
				printf("借阅成功!n");
                printList(list);
			}
			else
			{
				printf("当前书籍库存不足,借阅失败!n");
			}
		}
		break;
	case 4:
		printf("[ 归还书籍 ]n");  //库存+1
		printf("请输入要归还的书名:");
        scanf("%s",tempbook.name);
		returnbook = searchbook(list,tempbook.name);
		if(returnbook == NULL)
		{
			printf("该书不是图书馆里的书籍!n");
		}
		else
		{
			returnbook->data.num++;
			printf("书籍归还成功!n");
			printList(list);
		}
		break;
	case 5:
		printf("[ 删除书籍 ]n");
		printf("请输入要删除的书名:");
		scanf("%s",tempbook.name); 
        deletebook(list,tempbook.name);    
		printList(list);
		break;
	case 6:
		printf("[ 查找书籍 ]n");
		printf("请输入要查询的书名:");
		scanf("%s",tempbook.name);
		searchname = searchbook(list,tempbook.name);
		if(searchname == NULL)
		{
			printf("不存在该书,请加购录入!n");
		}
		else
		{
			
			printf("书名t作者t出版日期t价格t库存n");
			printf("%st%st%st%.1ft%dn",searchname->data.name,searchname->data.author,searchname->data.date,searchname->data.price,searchname->data.num);
		}
		break;
	case 7:
		printf("[ 排序书籍 ]n"); 
        sortbook(list);
		break;

	case 8:
		printf("[ 退出系统 ]n");
		printf("退出成功n");
		system("pause");
		exit(0);             
		break;
	default:
		printf("[ 错误 ]n");
		break;
	}
}
 

int main()
{
	list = createhead();
	readbookfile("bookinfo.txt",list);
	while(1)
	{
		display_menu();
		selectkey();
		system("pause");
		system("cls");
	}
    system("pause");
	return 0;
}

 六、效果

1.录入书籍

 

 2.浏览书籍

 3.借阅书籍

存在该书时,借阅成功,库存-1:

不存在该书时,无法借阅:

 4.归还书籍

当图书管理系统里本不存在该书,则归还失败:

 5.查找书籍

不存在该书时,则查找失败:

 6.排序书籍

再录入书籍:

排序(降序):

 

7.删除书籍

以上为该程序的所有功能。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/629226.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号