图书馆管理系统

发布时间 2023-07-26 16:45:09作者: 第几个梦乘风起

图书馆管理系统,实现的功能有:图书的的增删改查、根据图书的名字,数量(价格)进行排序。

排序方法说明:

  1. 首先定义两个指针find1find2,分别用于遍历链表。
  2. 使用冒泡排序算法,外层循环控制比较轮数,内层循环控制每一轮的比较次数。
  3. 在内层循环中,通过比较当前节点find1和后续节点find2的数据域(假设为num)的大小,如果前者大于后者,则交换它们的数据域。
  4. 内层循环结束后,将find2指向下一个节点,以便进行下一轮的比较。
  5. 外层循环结束后,将find1指向下一个节点,以便进行下一轮的比较。
  6. 最后输出"排序完成"的提示信息。   

当使用冒泡排序算法时,需要两层循环来控制比较轮数和比较次数。

外层循环用于控制比较轮数,每一轮都会将当前最大(或最小)的元素移动到正确的位置。在这个代码中,外层循环是通过遍历链表中的节点实现的,即find1 = find1->next。这样可以确保每一轮都能够遍历整个链表。

内层循环用于控制每一轮的比较次数,它从当前节点开始,依次与后续节点进行比较。在这个代码中,内层循环也是通过遍历链表中的节点实现的,即find2 = find2->next。这样可以确保每一轮都能够比较当前节点与后续节点的大小关系。

因此,外层循环和内层循环的嵌套就可以实现对链表中所有节点的比较操作。具体流程如下:

  1. 外层循环:遍历链表中的每一个节点,直到最后一个节点。

    • 内层循环:从当前节点开始,遍历其后续节点,直到最后一个节点。
      • 比较当前节点与后续节点的数据域大小。
      • 如果当前节点的数据域大于后续节点的数据域,则交换它们的数据域。
    • 结束内层循环。
  2. 结束外层循环,排序完成。

通过这样的嵌套循环结构,可以确保每个节点都与其他节点进行了比较,并根据需要进行了交换操作。最终实现了对链表中节点的升序排序。

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
struct bookInfo
{
	char name[100];
	float price;
	int num;
};

struct Node
{
	struct bookInfo data;
	struct  Node* next;

};

struct Node* list = NULL;

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

	return headNode;
}
struct Node* creatNode(struct  bookInfo data) {
	struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
	if (newNode == NULL) return;
	newNode->data = data;
	newNode->next = NULL;
	return newNode;
}
void insertBook(struct Node* headNode, struct bookInfo data) {
	//根据传来的data,创建一个新的Node结构体,newNode
	struct Node* newNode = creatNode(data);
	newNode->next = headNode->next;
	headNode->next = newNode;
}
void saveInfo(char *filename,struct Node *headNode) {
	//打开文件
	FILE* fp = fopen(filename, "w");
	//写入文件 fwritr fprintf
	struct Node* move = headNode->next;
	while (move != NULL) {
		fprintf(fp, "%s %.1f %d\n", move->data.name, move->data.price, move->data.num);
		move = move->next;
	}
	//关闭文件
	fclose(fp);
}
void readInfo(char* filename, struct Node* headNode) {
	FILE* fp = fopen(filename, "r");
	if (fp == NULL) {
		fp = fopen(filename, "w");
		fclose(fp);
		return;
	}
	struct bookInfo temp;
	while (fscanf(fp, "%s %f %d\n", temp.name, &temp.price, &temp.num) != EOF) {
		insertBook(list, temp);
	}
	fclose(fp);
}
void sort(struct Node* headNode) {
	struct Node* find1 = headNode;
	struct Node* find2 = headNode;
	struct bookInfo temp;
	for (find1 = headNode->next;find1 != NULL;) {
		for (find2 = find1->next;find2 != NULL;) {
			//if (find1->data.num > find2->data.num) {
			//	temp = find1->data;
			//	find1->data = find2->data;
			//	find2->data = temp;
			//}
			if (strcmp(find1->data.name, find2->data.name)>0) {
					temp = find1->data;
					find1->data = find2->data;
					find2->data = temp;
			}
			else {
				break;
			}
			find2 = find2->next;
		}
		find1 = find1->next;
	}
	printf("排序完成\n");
}


void showlist(struct Node* headNode) {
	sort(list);
	struct Node* move = headNode->next;
	while (move != NULL) {
		printf("%s, %.1f, %d\n", move->data.name, move->data.price, move->data.num);
		move = move->next;
	}
}
struct Node* searchByname(struct Node *headNode,char *name)
{
	struct Node* move = headNode->next;
	while (move!=NULL && strcmp(move->data.name,name))
	{
		move = move->next;
	}
	return move;
};

void deletbyName(struct Node* headNode,char * name) {
	struct Node* move = headNode->next;
	struct Node* prev = headNode;
	while (move != NULL && strcmp(move->data.name, name))
	{
		prev = move;
		move = move->next;
	}
	if (move == NULL) {
		return;
	}
	else {
		printf("删除成功\n");
		prev->next = move->next;
		free(move);
		move = NULL;
	}
}
void keyvent() {
	int key = 0;
	scanf("%d", &key);
	struct bookInfo temp;
	struct Node* result;
	switch (key)
	{
	case 0:
		printf("[录入]\n");
		printf("录入的书籍信息,名字,价格,数量\n");
		scanf("%s%f%d", temp.name, &temp.price, &temp.num);
		//printf("%s,%.2f,%d", temp.name, temp.price, temp.num);
		//同步到链表
		insertBook(list, temp);
		//同步到文件
		saveInfo(".//gaohui.txt", list);
		break;
	case 1:
		printf("[速览]\n");
		showlist(list);
		break;
	case 2:
		printf("[借阅]\n");
		printf("借阅的书名是:\n");
		scanf("%s", temp.name);
		result = searchByname(list, temp.name);
		if (result == NULL) {
			printf("未找到相关书籍,无法借阅");
		}
		else {
			if (result->data.num > 0) {
				result->data.num--;
				printf("借阅成功\n");
				saveInfo(".//gaohui.txt", list);
				}
			else {
				printf("没有库存,无法借阅\n");
			}
		}
		break;
	case 3:
		printf("[归还]\n");
		printf("归还的书名是:\n");
		scanf("%s", temp.name);
		result = searchByname(list, temp.name);
		if (result == NULL) {
			printf("该书籍无法归还,请检查是否为图书馆书籍");
		}
		else {
				result->data.num++;
				printf("归还成功\n");
				saveInfo(".//gaohui.txt", list);
		}
		break;
	case 4:
		printf("[查找]\n");
		printf("查询的书名是:\n");
		scanf("%s", temp.name);
		result=searchByname(list, temp.name);
		if (result == NULL) {
			printf("未找到相关书籍");
		}
		else {
			printf("书名 价格 数量\n");
			printf("%s, %.1f, % d\n", result->data.name, result->data.price, result->data.num);
		}
		break;
	case 5:
		printf("[删除]\n");
		scanf("%s", temp.name);
		deletbyName(list, temp.name);
		break;
	case 6:
		printf("[退出系统]\n");
		exit(0);
		break;
	default:
		break;


	}
}
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("--------------------------\n");
	printf("请输入(0-6):\n");
	keyvent();
}
int main() {
	//初始化头部指针
	list = creatHead();
	readInfo(".//gaohui.txt", list);
	while (1) {
		menu();
		system("pause");
		system("cls");
	}
	
}