1 Star 1 Fork 0

Zongyan/图书管理系统

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
图书管理系统.c 6.42 KB
一键复制 编辑 原始数据 按行查看 历史
Zongyan 提交于 2023-05-04 16:26 . 图书管理系统
#include <stdio.h>
#include <stdlib.h>
#define _CRT_SECURE_NO_WARNINGS
//3、数据的设计
//3.1程序用什么东西处理数据
//3.2数据的结构----->图书信息
struct bookInfo //存放的书籍信息
{
char name[20]; //书名
float price; //价格
int num; //数量
/*
char version[20]; //出版社
char data[20]; //日期
*/
};
//1、写界面-----菜单---->模块
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;
}
/*
//插入
// 表头法插入------表头不存放数据,在表头与下一个节点之间插入一个newNode
void insertNodeByhead(struct Node* headNode, struct bookInfo data)
{
struct Node* newNode = createNode(data);
//先连接后断开----插入
newNode->next = headNode->next;
headNode->next = newNode;
}
*/
//尾插法
//找表尾
void insertNodeByTail(struct Node* headNode, struct bookInfo data)
{
struct Node* pMove = headNode;
while(pMove->next != NULL)
{
pMove = pMove->next;
}
struct Node* newNode = createNode(data);
pMove->next = newNode;
}
//指定位置删除
//找指定位置
void deleteNodeByName(struct Node* headNode, char *bookName)
{
struct Node* posLeftNode = headNode; //删除节点的左边left节点定义
struct Node* posNode = headNode->next;//删除右边
//用书籍名字符串比较方式
while (posNode->data.name != NULL && strcmp(posNode->data.name, bookName))
{
posLeftNode = posNode;
posNode = posLeftNode->next;
}
//讨论查找后的结果
if (posNode == NULL)
{
printf("没有该书,删除失败!\n");
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数量\n");
while (pMove != NULL)
{
//剥洋葱
printf("%s\t%.1f\t%d\n", pMove->data.name, pMove->data.price, pMove->data.num);
pMove = pMove->next;
}
}
void makeMeau()
{
printf("-----------------------------------------\n");
printf("--------欢迎来到图书管理系统--------\n");
printf("\t0.退出系统\n");
printf("\t1.登记书籍\n");
printf("\t2.浏览书籍\n");
printf("\t3.借阅书籍\n");
printf("\t4.归还书籍\n");
printf("\t5.书籍排序\n");
printf("\t6.删除书籍\n");
printf("\t7.查找书籍\n");
printf("-----------------------------------------\n");
printf("请输入0-7:");
}
//文件操作---->对list
void saveFile(const char* fileName, struct Node* headNode) //存----//写
{
FILE* fp = fopen(fileName, "w");
struct Node* pMove = headNode->next;
while (pMove != NULL)
{
fprintf(fp, "%s\t%.1f\t%d\n", pMove->data.name, pMove->data.price, pMove->data.num);
pMove = pMove->next;
}
fclose(fp);
}
void readFile(const char* fileName, struct Node* headNode)//读
{
FILE* fp = fopen(fileName, "r");
if (fp == NULL)
{
fp = fopen(fileName, "w+"); //第一次打开不存在文件,就创建文件
}
struct bookInfo tempData;
while (fscanf(fp, "%s\t%f\t%d\n", tempData.name, &tempData.price, &tempData.num) != EOF)
{
insertNodeByTail(list, tempData);
}
fclose(fp);
}
//冒泡排序
void sortList(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 Node* result = NULL; //查找结果
struct bookInfo tempBook; //产生一个临时变量存储书籍信息
scanf("%d", &userkey);
switch (userkey)
{
case 0:
printf("【退出】\n");
printf("退出成功\n");
system("pause");
exit(0); //回车关闭整个程序
break;
case 1:
printf("【登记】\n");
printf("请输入书籍信息(书名&价格&数量):");
scanf("%s%f%d", &tempBook.name, &tempBook.price, &tempBook.num);
insertNodeByTail(list, tempBook);
saveFile("bookFile.txt", list);
break;
case 2:
printf("【浏览】\n");
printList(list);
break;
case 3:
printf("【借阅】\n"); //书籍存在则可以借阅,借阅后数量-1;书籍不存在则提示:书籍不存在
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("书籍借阅成功,剩余该书%d本\n", result->data.num);
}
else
{
printf("当前书籍无库存,借阅失败!\n");
}
}
break;
case 4:
printf("【归还】\n"); //当前书籍的数量+1
printf("请输入需归还的书籍:");
scanf("%s", tempBook.name);
result = searchByname(list, tempBook.name);
if (result == NULL)
{
printf("非此图书馆书籍,归还失败!\n");
}
else
{
result->data.num++;
printf("书籍归还成功,该书库存%d本\n", result->data.num);
}
break;
case 5:
printf("【排序】\n");
sortList(list);
break;
case 6:
printf("【删除】\n");
printf("请输入需删除书籍的书名:");
scanf("%s", tempBook.name);
deleteNodeByName(list, tempBook.name);
saveFile("bookFile.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数量\n");
printf("%s\t%.1f\t%d\n", result->data.name, result->data.price, result->data.num);
}
break;
default:
printf("【Error】\n");
break;
return 0;
}
}
//2、交互处理
int main()
{
list = createHead();
readFile("bookFile.txt", list);
/*测试删除作用
for (int i = 0; i < 3; i++)
{
insertNodeByTail(list, i);
}
printList(list);
deleteNodeByData(list, 0);//删除第一个
printList(list);
*/
while (1)
{
makeMeau();
keyDown();
system("pause"); //防止闪频
system("cls"); //清除
}
system("pause");
return 0;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C
1
https://gitee.com/Zongyan_03126/library-management-system.git
[email protected]:Zongyan_03126/library-management-system.git
Zongyan_03126
library-management-system
图书管理系统
master

搜索帮助