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

C语言链表实现简单的图书管理系统

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

C语言链表实现简单的图书管理系统

实现功能:

用C语言制作图书管理系统,实现图书进行登记书籍,浏览书籍,借阅书籍,归还书籍,书籍排序,删除书籍信息,查找书籍等功能。

功能展示

1.登记书籍 

 2.浏览书籍

3.借阅书籍

4.归还书籍

5.书籍排序

6.删除书籍

 7.查找书籍

8.退出程序 

 代码如下

#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include

struct bookInfo
{
	char name[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 insertNodeByhead(struct Node* headNode, struct bookInfo data)
{
	struct Node* newNode = createNode(data);
	newNode->next = headNode->next;
	headNode->next = newNode;
}

void deleteNodeByName(struct Node* headNode, char *bookName)
{
	struct Node* posLeftNode = headNode;
	struct Node* posNode = headNode->next;
	while (posNode != NULL && strcmp(posNode->data.name, bookName))
	{
		posLeftNode = posNode;
		posNode = posLeftNode->next;
	}
	if (posNode == NULL)
		return;
	else
	{
		printf("删除成功!n");
		posLeftNode->next = posNode->next;
		free(posNode);
		posNode = NULL;
	}
}
struct Node* searchByName(struct Node* headNode, char* bookName)
{
	struct Node* posNode = headNode->next;
	while (posNode != NULL && strcmp(posNode->data.name, bookName))
	{
		posNode = posNode->next;
	}
	return posNode;
}

void printlist(struct Node* headNode)
{
	struct Node* pMove = headNode->next;
	printf("书名t价格t数量t作者t出版社n");
	while (pMove!=NULL)
	{
		printf("%st%.1ft%dn", pMove->data.name, pMove->data.price, pMove->data.num);
		pMove = pMove->next;
	}
}

void makeMenu()
{
	printf("*************************************n");
	printf("*************图书管理系统************n");
	printf("*——————0.退出系统——————*n");
	printf("*——————1.登记书籍——————*n");
	printf("*——————2.浏览书籍——————*n");
	printf("*——————3.借阅书籍——————*n");
	printf("*——————4.归还书籍——————*n");
	printf("*——————5.书籍排序——————*n");
	printf("*——————6.删除书籍——————*n");
    printf("*——————7.查找书籍——————*n");
    printf("**************************************n");
    printf("请输入(0 ~ 7):");
}

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

void readInfoFromFile(const char* fileName, struct Node* headNode)
{
	FILE* fp = fopen(fileName, "r");
	if (fp == NULL)
	{
		fp = fopen(fileName, "w+");
	}
	struct bookInfo tempData;
	while (fscanf(fp, "%st%ft%dn", tempData.name, &tempData.price, &tempData.num) != EOF)
	{
		insertNodeByhead(list, tempData);
	}
	fclose(fp);
}

void bubbleSortlist(struct Node* headNode)
{
	for (struct Node* p = headNode->next; p != NULL; p = p->next)
	{
		for (struct Node* q = headNode->next; q->next != NULL; q = q->next)
		{
			if (q->data.price > q->next->data.price)
			{
				struct bookInfo tempData = q->data;
				q->data = q->next->data;
				q->next->data = tempData;
			}
		}
	}
	printlist(headNode);
}

void keyDown()
{
	int userKey = 0;
	struct bookInfo tempBook;
	struct Node* result = NULL;
	scanf("%d",&userKey);
	switch (userKey)
	{
	case 0:
		printf("【退出】n");
		printf("退出成功n");
		system("pause");
		exit(0);                  
		break;
	case 1:
		printf("【登记】n");
		printf("输入书籍的信息(name,price,num):");
		scanf("%s%f%d", tempBook.name, &tempBook.price, &tempBook.num);
		insertNodeByhead(list, tempBook);
		saveInfoFile("bookinfo.txt", list
);
		break;
	case 2:
		printf("【浏览】n");
		printlist(list);
		break;
	case 3:
		printf("【借阅】n");    
		printf("请输入借阅的书名:");
		scanf("%s", tempBook.name);
		result = searchByName(list, tempBook.name);
		if (result == NULL)
		{
			printf("没有相关书籍无法借阅!n");
		}
		else
		{
			if (result->data.num > 0)
			{
				result->data.num--;
				printf("借阅成功!n");
			}
			else
			{
				printf("当前书籍无库存,借阅失败!n");
			}
		}
		break;
	case 4:
		printf("【归还】n");     
		printf("请输入归还的书名:");
		scanf("%s", tempBook.name);
		result = searchByName(list, tempBook.name);
		if (result == NULL)
		{
			printf("该书无借出记录n");
		}
		else
		{
			result->data.num++;
			printf("书籍归还成功!n");
		}
		break;
	case 5:
		printf("【排序】n");
		bubbleSortlist(list);
		break;
	case 6:
		printf("【删除】n");
		printf("请输入删除书名:");
		scanf("%s", tempBook.name);
		deleteNodeByName(list, tempBook.name);
		saveInfoFile("bookinfo.txt", list);
		break;
	case 7:
		printf("【查找】n");
		printf("请输入要查询的书名:");
		scanf("%s", tempBook.name);
		result = searchByName(list, tempBook.name);
		if (result == NULL)
		{
			printf("未找到相关信息!n");
		}
		else
		{
			printf("书名t价格t数量t作者t出版社n");
			printf("%st%.1ft%dn", result->data.name, result->data.price, result->data.num);
		}
		break;
	default:
		printf("【error】n");
		break;
	}
}

int main()
{
	list= createHead();
	readInfoFromFile("bookinfo.txt", list);
	while (1)
	{
		makeMenu();
		keyDown();
		system("pause");
		system("cls");
	}
	system("pause");
	return 0;
}

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

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

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