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

c语言课设 图书管理系统 链表动态 开辟内存

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

c语言课设 图书管理系统 链表动态 开辟内存

#define _CRT_SECURE_NO_WARNINGS
#include 
#include 
#include 
#include 
#define LEN sizeof(struct book)
struct book {
	char book_name[20];
	int number;
	int count;
	struct book* next;
};  
int n;                                                       
struct book* creat()
{
	struct book* head;
	struct book* p1, * p2;
	n = 0;
	p1 = p2 = (struct book*)malloc(LEN);
	printf("*****书的编号*****书名*****剩余数量*****n");
	scanf("%d%s%d", &p1->number, &p1->book_name, &p1->count);
	head = NULL;
	while (p1->number != 0)
	{
		n++;
		printf("*****书的编号*****书名*****剩余数量*****n");
		if (n == 1)head = p1;
		else p2->next = p1;
		p2 = p1;
		p1 = (struct book*)malloc(LEN);
		scanf("%d%s%d", &p1->number, &p1->book_name, &p1->count);
	}
	p2->next = NULL;
	printf("录入成功!n");
	return(head);
}
void print(struct book* head)
{
	struct book* p;
	p = head;
	if (head != NULL)
	{	printf("*****书的编号*****书名*****剩余数量*****n");
		do
		{
			printf("      %d          %s          %dn", p->number, p->book_name, p->count);
			p = p->next;
		}while (p != NULL);
	}
	else printf("没有这本书!n");
}
struct book* del(int num, int count, struct book* head)
{
	int flag = 0;
	struct book* p1, * p2;
	p2 = NULL;
	do
	{
		p1 = head;
		if (head == NULL)
		{
			printf("没有这本书 !n");
			goto end;
		}
		while (p1->number != num && p1->next != NULL)    //当编号不相等(没找到)且下一个链表不是空的时
		{
			p2 = p1;
			p1 = p1->next;                                        //转到下一个链表(p2还在上一个链表)
		}
		if (p1->number == num && p1->count > count)
		{

			printf("借阅编号为N0.%d  本数为:%dn", num, count);  //删除完成
			p1->count -= count; 
			flag++;
			break;//标记,如果为0表示不执行删除操作
		}
		else if (p1->number == num && p1->count == count) {
			if (p1 == head) 
				head = p1->next;//如果第一个链表就是要找的将头指针指向下一个链表(删除本链表)
			else 
				p2->next = p1->next;//第N个是要找的链表,将p2所指的链表与p1后的链表相连(删除p1的链表)
		}
	} while (p1->next != NULL); 
	if (flag != 0)
		printf("删除完成 !n");
end:
	return(head);
}
struct book* add(int num, struct book* head) {
	int flag = 0;
	struct book* p;
	p = head; 

	if (num != 0)
		printf("*****书的编号*****书名*****剩余数量*****n");
	while (p != NULL)
	{
		if (p->number == num)
		{
			printf("      %d          %s          %dn", p->number, p->book_name,p->count);
			printf("请输入要还书个数:");
			int new_count = 0;
			scanf("%d", &new_count);
			p->count += new_count;
			flag = 1;
			goto end;
		}
		p = p->next;
	}
		if (flag == 0) {
		printf("没有这本书!n");
	}
	system("cls");
	end:
	return(p);
}
void number(struct book* head)
{
	int num, a = 0;
	struct book* p;
	printf("*****请输入 "0" 结束输入*****n");
	do
	{
		p = head;
		printf("请输入要查找的书的编号:");
		scanf("%d", &num);
		if (num != 0)
			printf("*****书的编号*****书名*****作者*****剩余数量*****n");
		while (p != NULL)
		{
			if (p->number == num)
			{
				printf("      %d          %s          %dn", p->number, p->book_name,p->count);
				a++;
			}
			p = p->next;
		}
		if (a == 0 && num != 0)
			printf("没有这个书!n");
		a = 0;
	} while (num != 0);
	system("cls");
}
void search(struct book* head)
{
	int a;
	struct book* p;
	p = head;
	do
	{
		printf("1.按书编号查找n2.退出n");
		scanf("%d", &a);
		system("cls");
		switch (a) {
		case 1:number(p); break;
		case 2:break;
		}
	} while (a != 2);
}
struct book* sort(struct book* head)
{
	struct book* p1, * p2;
	int i, j;
	char string[20];
	for (i = 0; i < n - 1; i++)
	{
		p1 = head;
		int temp2, temp1;
		for (j = n - 1; j > 0; j--)
			while (p1->next != NULL)
			{
				p2 = p1; 
				p1 = p1->next;
				if (p2->number > p1->number)
				{
					temp1 = p1->number;
					p1->number = p2->number;
					p2->number = temp1;
					strcpy(string, p1->book_name);
					strcpy(p1->book_name, p2->book_name);
					strcpy(p2->book_name, string);
					temp2 = p1->count;
					p1->count = p2->count;
					p2->count = temp2;
				}
			}
	}
	return(head);
}
int main() {
	struct book* head = NULL;
	int num;
	int a;
	int count;
	while (1) {

		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("|_______________________________________________|n");
		printf("     ********请您选择需要使用的功能********      n");
		int choice = 0;
		scanf("%d", &choice);
		system("cls");
		//1.录入图书信息
		if (choice == 1) {
			printf("*****请输入 "0 0 0 " 结束输入*****n");
			head = creat();
			print(head);
		}
		//2.删除图书信息
		else if (choice == 2) {
			printf("*****请输入 "0" 结束输入*****n");
			 do{
				printf("请输入要删除书本编号和本数:");
				scanf("%d%d", &num, &count);
				head = del(num, count, head);
				print(head);
			 } while (num != 0);
			printf("nnnnnn");
		}
		//3.借书 
		else if (choice == 3) {
			printf("*****请输入 "0" 结束输入*****n");
			while (number != 0) {
				printf("请输入要借出书本编号和本数:");
				scanf("%d%d", &num, &count);
				head = del(num, count, head);
				print(head);
			} 
			printf("nnnnnn");
		}
		//4.还书
		else if (choice == 4) {
				printf("请输入书编号:");
				scanf("%d", &num);
				head = add(num, head);
				print(head);
		}
		//5.查找图书信息
		else if (choice == 5) {
			search(head); 
			printf("nnnnnn");
		}
		//排序
		else if (choice == 6) {
			head = sort(head); 
			print(head);
			getchar();
			printf("nnnnnn");
		}
		//7.退出系统
		else if (choice == 7)
		{
			break;
		}
		else {
			printf("   ********您输入的数据非法请重新输入********     n");
		}
	}
	return 0;
}

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

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

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