C语言图书管理系统(链表、文件功能齐全)

按键交互

文件存储图书信息

链表完成增删改查

#include
#include
#include

void menu() //菜单、模块界面 
{
	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):\n");
}
//链表存储图书信息:
void InsertNodeByhead(struct Node* headNode, struct bookinfo data);
struct bookinfo
{
	char name[40]; char writer[10]; int total; 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* creatNode(struct bookinfo data)
{
	struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
	newNode->data = data;
	newNode->next = NULL;
	return newNode;
};

//打印链表:定义一个移动指针,从第三个节点打印
void printlist(struct Node* headNode)
{
	struct Node* pMove = headNode->next;
     printf("书名              作者      总数\t价格\t编号\n");
	while (pMove != NULL)
	{
		printf("%-16s  %-8s  %-4d\t%-4.2f\t%03d\n", pMove->data.name, pMove->data.writer, pMove->data.total, pMove->data.price, pMove->data.num);
		pMove = pMove->next;
	}
}
//插入:表头法插入
void InsertNodeByhead(struct Node* headNode, struct bookinfo data)
{
	struct Node* newNode = creatNode(data); //创建新结点
	newNode->next = headNode->next;
	headNode->next = newNode;
}
//指定位置删除:
void delNodeByname(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("删除成功!"); 
		posleftNode->next = posNode->next;
		free(posNode);
		posNode = NULL;
	}
}
//文件操作:
void SaveinfoTofile(const char* fileName, struct Node* headNode) //写操作
{
	FILE* fp = fopen(fileName, "a");
	struct Node* pMove = headNode->next;
	{
		fprintf(fp, "%s\t%s\t%d\t%.2f\t%03d\n", pMove->data.name, pMove->data.writer, pMove->data.total, pMove->data.price, pMove->data.num);
		pMove = pMove->next;
	}
	fclose(fp);
}
void ReadinfoFromfile(const char* fileName)
{
	FILE* fp = fopen(fileName, "r");
	if (fp == NULL) //第一次打开,文件不存在
	{ printf("");}
	 else
	{       
		struct bookinfo tempData1;
		struct bookinfo*tempData = &tempData1;
while (fscanf(fp, "%s\t%s\t%d\t%f\t%03d\n", tempData->name, tempData->writer, &tempData->total, &tempData->price, &tempData->num) != EOF)		{   
			InsertNodeByhead(list, tempData1);

		}
	}
	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.num>q->next->data.num)
			{
				struct bookinfo tempData=q->data;
				q->data=q->next->data;
				q->next->data=tempData;
			} 
		}
	}printlist(headNode);
} 

//用书名查找:
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 keydown() //交互、按键处理 
{
	int key;
	struct bookinfo tempBook; //临时变量:存储书籍信息 
	struct Node*result=NULL;
	scanf("%d", &key);
	switch (key)
	{
	case 0:
		printf("【 退    出 】\n");
		printf("退出成功\n");
		system("pause");
		exit(0);   //关闭整个程序 
		break;
	case 1:
		printf("【 录入书籍 】\n");
		printf("请输入书籍信息(name,writer,total,price,num):");
		scanf("%s %s %d %f %d", tempBook.name, tempBook.writer, &tempBook.total, &tempBook.price, &tempBook.num);
		InsertNodeByhead(list, tempBook); //插入链表 
		SaveinfoTofile("bookinfo.txt", list);
		break;
	case 2:
		printf("【 浏览书籍 】\n");
		printlist(list);
		break;
	case 3:
		int m;
		printf("【 借阅书籍 】\n");
		printf("请输入需借阅的书名和数量:");
		scanf("%s %d",tempBook.name,&m);
		result=searchByname(list,tempBook.name);
		if(result==NULL)
			{printf("没有相关书籍,无法借阅\n");}
		else
		{if(result->data.total>m)
			{ result->data.total-=m;;
			  printf("借阅成功!"); 	}
		 else
		 {
		 	printf("当前书籍库存不足,无法借阅!\n");
		 }
	}
		break;
	case 4:
		int n;
		printf("【 归还书籍 】\n");
		printf("请输入归还的书名和数量:");
		scanf("%s %d",tempBook.name,&n);
		result=searchByname(list,tempBook.name);
		if(result==NULL)
		{printf("该书来源非法!\n");}
		else
		{ result->data.total+=n;
			 printf("归还成功!"); 	}
		break;
	case 5:
		printf("【 删除书籍 】\n");
		printf("请输入书名:");
		scanf("%s",tempBook.name);
		delNodeByname(list,tempBook.name);
		SaveinfoTofile("bookinfo.txt",list); //同步到文件 
		break;
	case 6:
		printf("【 查阅书籍 】\n");
		printf("请输入查找书名:");
		scanf("%s",tempBook.name);
		result=searchByname(list,tempBook.name); 
		if(result==NULL)
		{printf("未找到相关信息");}
		else
		{printf("书名              作者      总数\t价格\t编号\n");
		 printf("%-16s  %-8s  %-4d\t%-4.2f\t%03d\n", result->data.name, result->data.writer, result->data.total, result->data.price, result->data.num);
				}
		break;
	case 7:
		printf("【 书籍排序 】\n");
		bubbleSortlist(list);
		break;
	default:
		printf(" 输入错误! 请重新输入:\n");
		break;
	}
}
int main()
{   list=createhead();
	ReadinfoFromfile("bookinfo.txt");
	
	while (1)
	{
		menu();
		keydown();
		system("pause"); //冻结屏幕,观察执行结果 
		system("cls"); //清屏 
	}
	return 0;
}

你可能感兴趣的:(c语言,链表)