//
// main.c
// 高级语言课程设计图书管理系统
//
// Created by 蔡星旖 on 2022/8/1.
//
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
using namespace std;
//管理员用户:账号:888888, 密码:123456
//读者用户:账号自主设置, 密码:自主设置
//图书信息
struct Book
{
int num;
char name[100];
char author[100];
char pub[100];
double price;
int cnt;
};
struct Stu
{
int num;
int password;
int cnt;
struct Book book;
};
struct SNode
{
struct Stu stu;
struct SNode* next;
};
typedef struct Node
{
struct Book data;
struct Node* next;
}Node, *LinkList;
struct Node* list = NULL;
struct SNode* Slist = NULL;
//创建图书表头
struct Node* createHead(void)
{
//动态内存申请
struct Node* headNode = (struct Node*)malloc(sizeof(struct Node));
headNode -> next = NULL;
return headNode;
}
//创建用户信息表头
struct SNode* createSHead(void)
{
//动态内存申请
struct SNode* headNode = (struct SNode*)malloc(sizeof(struct SNode));
headNode -> next = NULL;
return headNode;
}
//创建节点
struct Node* creatNode(struct Book data)
{
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode -> data = data;
newNode -> next = NULL;
return newNode;
}
//创建读者账户信息
struct SNode* creatReaderNode(struct Stu data)
{
struct SNode* newNode = (struct SNode*)malloc(sizeof(struct SNode));
newNode -> stu = data;
newNode -> next = NULL;
return newNode;
}
//打印链表
void printList(struct Node* headNode)
{
struct Node* pMove = headNode -> next;
printf("图书编号\t\t\t书名\t\t\t作者\t\t\t出版社\t\t\t价格\t\t\t库存\n");
while(pMove != NULL)
{
printf("%d\t\t\t%s\t\t\t%s\t\t\t%s\t\t\t%.1f\t\t\t%d\n", pMove ->data.num, pMove ->data.name, pMove ->data.author, pMove -> data.pub, pMove ->data.price, pMove -> data.cnt);
pMove = pMove -> next;
}
}
//插入:头插法插入书籍信息
void insertNodeByHead(struct Node* headNode, struct Book data)
{
struct Node* newNode = creatNode(data);
newNode -> next = headNode -> next;
headNode -> next = newNode;
}
//插入:头插法插入读者账户信息
void insertNodeReader(struct SNode* headNode, struct Stu a)
{
struct SNode* newNode = creatReaderNode(a);
newNode -> next = headNode -> next;
headNode -> next = newNode;
}
//写文件操作
void save(const char* fileName, struct Node* headNode)
{
FILE * fp = fopen(fileName,"w");
struct Node* pMove = headNode -> next;
while(pMove != NULL)
{
fprintf(fp, "%d\t%s\t%s\t%s\t%.1lf\t%d\n", pMove -> data.num, pMove -> data.name, pMove ->data.author, pMove -> data.pub, pMove -> data.price, pMove -> data.cnt);
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 Book tmp;
while(fscanf(fp, "%d\t%s\t%s\t%s\t%lf\t%d\n", &tmp.num, tmp.name, tmp.author, tmp.pub, &tmp.price, &tmp.cnt) != EOF)
{
insertNodeByHead(list, tmp);
}
fclose(fp);
}
void save2(const char* fileName, struct SNode* headNode)
{
FILE * fp = fopen(fileName,"w");
struct SNode* pMove = headNode -> next;
while(pMove != NULL)
{
fprintf(fp,"%d\t%d\t%d\t%d\t%s\t%s\t%s\t%.lf\t%d\n", pMove ->stu.num, pMove ->stu.password, pMove ->stu.cnt, pMove ->stu.book.num, pMove ->stu.book.name, pMove ->stu.book.author, pMove ->stu.book.pub, pMove ->stu.book.price, pMove ->stu.book.cnt);
pMove = pMove -> next;
}
fclose(fp);
}
void readFile2(const char* fileName, struct SNode* headNode)
{
FILE * fp = fopen(fileName,"r");
if(fp == NULL)
{
//第一次打开程序文件是不存在的
fp = fopen(fileName, "w+");
}
struct Stu tmp;
while(fscanf(fp, "%d\t%d\t%d\t%d\t%s\t%s\t%s\t%lf\t%d\n",&tmp.num, &tmp.password, &tmp.cnt, &tmp.book.num, tmp.book.name, tmp.book.author, tmp.book.pub, &tmp.book.price, &tmp.book.cnt) != EOF)
{
insertNodeReader(Slist, tmp);
}
fclose(fp);
}
//检验密码是否正确
int denglu(struct SNode* headNode,int zhanghao, int mima)
{
struct SNode* pMove = headNode ->next;
while(pMove != NULL)
{
if(pMove ->stu.num == zhanghao)
{
if(pMove ->stu.password == mima)
return 1;
else
return 0;
}
pMove = pMove -> next;
}
if(pMove == NULL) return 2;
return 0;
}
//修改密码
int xiugai(struct SNode* headNode,int zhanghao, int mima1, int newmima)
{
struct SNode* pMove = headNode ->next;
while(pMove != NULL)
{
if(pMove ->stu.num == zhanghao)
{
if(pMove ->stu.password == mima1)
{
pMove -> stu.password = newmima;
return 1;
}
else
return 0;
}
pMove = pMove -> next;
}
if(pMove == NULL) return 2;
return 0;
}
//指定书籍编号删除
void deletebynum(struct Node* headNode, int num)
{
struct Node* LDNode = headNode;
struct Node* DNode = headNode -> next;
while(DNode != NULL && DNode -> data.num != num)
{
LDNode = DNode;
DNode = DNode -> next;
}
if(DNode == NULL)
return ;
else
{
LDNode -> next = DNode -> next;
free(DNode);
DNode = NULL;
}
}
//指定作者删除
void deletebyname(struct Node* headNode, char* bookname)
{
struct Node* LDNode = headNode;
struct Node* DNode = headNode -> next;
while(DNode != NULL && strcmp(bookname, DNode ->data.name) )
{
LDNode = DNode;
DNode = DNode -> next;
}
if(DNode == NULL)
return ;
else
{
LDNode -> next = DNode -> next;
free(DNode);
DNode = NULL;
}
}
//按书籍编号进行排序
void BubbleSortBynum(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 Book tmp = q->data;
q->data = q->next->data;
q->next->data = tmp;
}
}
}
printList(headNode);
}
//按书籍价格进行排序,交换数据,并不改变节点之间的指针关系
void BubbleSortByprice(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 Book tmp = q->data;
q->data = q->next->data;
q->next->data = tmp;
}
}
}
printList(headNode);
}
//按书籍库存数量进行排序
void BubbleSortBycnt(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.cnt > q -> next -> data.cnt)
{
struct Book tmp = q->data;
q->data = q->next->data;
q->next->data = tmp;
}
}
}
printList(headNode);
}
//两个链表划分的过程
LinkList Split(LinkList head)
{
if (head == NULL) {
return head;
}
LinkList prev = NULL;
LinkList slow = head;
LinkList fast = head;
// 注意这里了要判断fast.next 是否为空
while (fast && fast -> next) {
fast = fast -> next -> next;
prev = slow;
slow = slow -> next;
}
prev -> next = NULL; // 断开链表,返回后面一段的第一个节点
return slow;
}
//两个有序链表归并的过程
LinkList merge(LinkList left, LinkList right) {
LinkList head = new Node;
LinkList cur = head;
while (left != NULL && right != NULL) {
if (left -> data.num > right -> data.num) {
cur -> next = right;
right = right -> next;
} else {
cur -> next = left;
left = left -> next;
}
cur = cur -> next;
}
while (left != NULL) {
cur -> next = left;
cur = cur -> next;
left = left -> next;
}
while (right != NULL) {
cur -> next = right;
cur = cur -> next;
right = right -> next;
}
return head -> next;
}
//按书籍编号进行归并排序
LinkList sortList(LinkList head) {
if (head == NULL || head -> next == NULL) {
// 为空 或 只有一个链表节点均不用排序
return head;
}
LinkList mid = Split(head);
LinkList left = sortList(head);
LinkList right = sortList(mid);
return merge(left, right);
}
//按书名查询书籍
struct Node* SearchByname(struct Node* headNode, char* Bookname )
{
struct Node* pMove = NULL;
for(pMove = headNode -> next; pMove != NULL; pMove = pMove -> next)
{
if(strcmp(pMove -> data.name, Bookname) == 0)
{
break;
}
}
return pMove;
}
//按书籍编号查询书籍
struct Node* SearchBynum(struct Node* headNode, int num )
{
struct Node* pMove = NULL;
for( pMove = headNode -> next; pMove != NULL; pMove = pMove -> next)
{
if(pMove -> data.num == num)
{
break;
}
}
return pMove;
}
//找到借阅的指针
struct SNode* borrow2(struct SNode* headNode,int zhanghao)
{
struct SNode* pMove = headNode ->next;
while(pMove != NULL)
{
if(pMove ->stu.num == zhanghao)
{
return pMove;
}
}
return pMove;
}
//按书名借书
void borrowByname(struct Node* headNode, struct SNode* headSNode, char* name, int zhanghao)
{
struct Node* tmp = SearchByname(headNode, name);
if(tmp != NULL)
{
struct SNode* reader = borrow2(Slist, zhanghao);
if(tmp -> data.cnt > 0)
{
reader -> stu.book = tmp->data;
reader -> stu.cnt = 1;
tmp -> data.cnt --;
save("bookinfo.txt", list);
save2("reader.txt", Slist);
cout << "借书成功" << endl;
}
else
{
cout << "馆内该图书已全部借出" << endl;
}
}
else
{
cout << "图书馆内没有您要借阅的这本图书!" << endl;
}
}
//按图书编号借书
void borrowBynum(struct Node* headNode, struct SNode* headSNode, int num, int zhanghao)
{
struct Node* tmp = SearchBynum(headNode, num);
if(tmp != NULL)
{
struct SNode* reader = borrow2(Slist, zhanghao);
if(tmp -> data.cnt > 0)
{
reader -> stu.book = tmp->data;
reader -> stu.cnt ++;
tmp -> data.cnt --;
save("bookinfo.txt", list);
save2("reader.txt", Slist);
cout << "借书成功" << endl;
}
else
{
cout << "馆内该图书已全部借出" << endl;
}
}
else
{
cout << "图书馆内没有您要借阅的这本图书!" << endl;
}
}
//按书名还书
void sendByname(struct Node* headNode, struct SNode* headSNode, char* name, int zhanghao)
{
struct Node* tmp = SearchByname(headNode, name);
if(tmp != NULL)
{
struct SNode* reader = borrow2(Slist, zhanghao);
reader -> stu.cnt = 0;
reader -> stu.book ={0, "/0", "/0", "/0", 0, 0};
tmp->data.cnt ++;
cout << "还书成功" << endl;
save("bookinfo.txt", list);
save2("reader.txt", Slist);
}
else
{
cout << "此书不是本图书馆藏书!" << endl;
}
}
//按图书编号还书
//按书名还书
void sendBynum(struct Node* headNode, struct SNode* headSNode, int num, int zhanghao)
{
struct Node* tmp = SearchBynum(headNode, num);
if(tmp != NULL)
{
struct SNode* reader = borrow2(Slist, zhanghao);
reader -> stu.cnt = 0;
reader -> stu.book ={0, "/0", "/0", "/0", 0, 0};
tmp->data.cnt ++;
cout << "还书成功" << endl;
save("bookinfo.txt", list);
save2("reader.txt", Slist);
}
else
{
cout << "此书不是本图书馆藏书!" << endl;
}
}
//图书馆界面、菜单
void allmenu(void)
{
printf("-------------------------\n");
printf("欢迎来到图书管理系统\n");
printf("\t0.退出系统\n");
printf("\t1.登记书籍\n");
printf("\t2.浏览书籍\n");
printf("\t3.查询书籍\n");
printf("\t4.书籍排序\n");
printf("\t5.删除书籍\n");
printf("-------------------------\n");
printf("请输入0~7选择您需要的业务\n");
}
//菜单按键
void allkeydown(void) {
int k = 0;
//临时变量存储书籍信息
struct Book tmpBook;
scanf("%d", &k);
if (k == 0)
{
printf("\t退出\n");
printf("\t退出成功\n");
system("pause");
exit(0);
}
else if (k == 1)
{
printf("\t登记\n");
printf("请输入书籍的信息:编号 书名 作者 出版社 价格 数量:\n");
scanf("%d%s%s%s%lf%d", &tmpBook.num, tmpBook.name, tmpBook.author, tmpBook.pub, &tmpBook.price, &tmpBook.cnt);
insertNodeByHead(list, tmpBook);
save("bookinfo.txt", list);
cout << "添加成功" << endl;
}
else if (k == 2)
{
printf("\t浏览\n");
printList(list);
}
else if (k == 3)
{
printf("\t查询\n");
printf("请输入您要选择的查询方式:\n");
printf("1.按书名查找\n");
printf("2.按书籍编号查找\n");
int k2;
scanf("%d", &k2);
if (k2 == 1)
{
printf("请输入您要查找的书名:\n");
scanf("%s", tmpBook.name);
struct Node *p = SearchByname(list, tmpBook.name);
if (p != NULL)
{
printf("您要查找的书籍已找到!\n");
printf("您要查找的书籍信息如下:\n");
printf("%d\t%s\t%s\t%s\t%.1lf\t%d\n", p->data.num, p->data.name, p->data.author, p->data.pub,
p->data.price, p->data.cnt);
}
else
{
printf("很抱歉, 您要查找的书籍未找到\n");
}
}
else if (k2 == 2)
{
printf("请输入您要查询书籍的编号:\n");
scanf("%d", &tmpBook.num);
struct Node *p = SearchBynum(list, tmpBook.num);
if (p != NULL)
{
printf("您要查找的书籍已找到!\n");
printf("您要查找的书籍信息如下:\n");
printf("%d\t%s\t%s\t%s\t%.1lf\t%d\n", p->data.num, p->data.name, p->data.author, p->data.pub,
p->data.price, p->data.cnt);
}
else
{
printf("很抱歉, 您要查找的书籍未找到\n");
}
}
else
{
printf("您的输入有误, 查找失败!\n");
}
}
else if(k == 4)
{
printf("\t排序\n");
printf("请选择您要排序的方式:\n");
printf("1.按书籍的序号进行排序\n");
printf("2.按书籍的价格进行排序\n");
printf("3.按书籍的库存数量进行排序\n");
int m = 0;
scanf("%d", &m);
if (m == 1)
{
BubbleSortBynum(list);
}
else if (m == 2)
{
BubbleSortByprice(list);
}
else if (m == 3)
{
BubbleSortBycnt(list);
}
else
{
printf("输入错误\n");
}
}
else if(k == 5)
{
printf("\t删除\n");
printf("请选择删除书籍的方式:\n");
printf("1.按书籍的编号删除\n");
printf("2.按书名删除\n");
int k1;
scanf("%d", &k);
if (k == 1)
{
printf("请输入您要删除书籍的编号:\n");
int num1 = 0;
scanf("%d", &num1);
deletebynum(list, num1);
save("bookinfo.txt", list);
}
else if (k == 2)
{
printf("请输入您要删除书籍的书名:\n");
char ch[100];
scanf("%s", ch);
deletebyname(list, ch);
save("bookinfo.txt", list);
printf("删除成功!\n");
}
else
{
printf("您的输入有误, 删除失败!\n");
}
}
else
{
printf("\t输入错误\n");
}
}
void secondmenu(void)
{
printf("-------------------------\n");
printf("欢迎来到图书管理系统\n");
printf("0.退出系统\n");
printf("1.浏览图书\n");
printf("2.查找图书\n");
printf("3.借阅图书\n");
printf("4.归还图书\n");
printf("-------------------------\n");
}
void secondkey(int zhanghao) {
int k = 0;
cin >> k;
struct Book tmpBook;
if (k == 0)
{
printf("\t退出\n");
printf("\t退出成功\n");
system("pause");
exit(0);
}
else if (k == 1)
{
printf("\t浏览\n");
printList(list);
}
else if (k == 2)
{
printf("\t查询\n");
printf("请输入您要选择的查询方式:\n");
printf("1.按书名查找\n");
printf("2.按书籍编号查找\n");
int k2;
scanf("%d", &k2);
if (k2 == 1)
{
printf("请输入您要查找的书名:\n");
scanf("%s", tmpBook.name);
struct Node *p = SearchByname(list, tmpBook.name);
if (p != NULL)
{
printf("您要查找的书籍已找到!\n");
printf("您要查找的书籍信息如下:\n");
printf("%d\t%s\t%s\t%s\t%.1lf\t%d\n", p->data.num, p->data.name, p->data.author, p->data.pub,
p->data.price, p->data.cnt);
}
else
{
printf("很抱歉, 您要查找的书籍未找到\n");
}
}
else if (k2 == 2)
{
printf("请输入您要查询书籍的编号:\n");
scanf("%d", &tmpBook.num);
struct Node *p = SearchBynum(list, tmpBook.num);
if (p != NULL)
{
printf("您要查找的书籍已找到!\n");
printf("您要查找的书籍信息如下:\n");
printf("%d\t%s\t%s\t%s\t%.1lf\t%d\n", p->data.num, p->data.name, p->data.author, p->data.pub,
p->data.price, p->data.cnt);
}
else
{
printf("很抱歉, 您要查找的书籍未找到\n");
}
}
}
else if (k == 3)
{
printf("\t借阅\n");
cout << "请选择您要借阅的方式!" << endl;
cout << "1.按书名借阅" << endl;
cout << "2.按图书编号借阅" << endl;
int key = 0;
cin >> key;
if (key == 1)
{
cout << "请输入您要借阅的书名" << endl;
char tmpbook[100];
cin >> tmpbook;
borrowByname(list, Slist, tmpbook, zhanghao);
}
else if (key == 2)
{
cout << "请输入您要借阅图书的编号" << endl;
int num;
cin >> num;
borrowBynum(list, Slist, num, zhanghao);
}
}
else if (k == 4)
{
printf("\t归还\n");
cout << "请您选择归还的方式" << endl;
cout << "1.按书名归还 " << endl;
cout << "2.按图书编号归还" << endl;
int key = 0;
cin >> key;
if (key == 1)
{
cout << "请输入您要归还的书名" << endl;
char tmpbook[100];
cin >> tmpbook;
sendByname(list, Slist, tmpbook, zhanghao);
} else if (key == 2)
{
cout << "请输入您要归还图书的编号" << endl;
int num;
cin >> num;
sendBynum(list, Slist, num, zhanghao);
}
}
else
{
printf("您的输入有误, 查找失败!\n");
}
}
void mainmenu(void)
{
printf("-------------------------\n");
printf("欢迎来到图书管理系统\n");
printf("请登陆您的账户\n");
printf("如果您是新用户请选择注册您的账号\n");
printf("0.注册账号\n");
printf("1.登陆管理员账号\n");
printf("2.登陆读者账号\n");
printf("3.修改密码\n");
printf("-------------------------\n");
}
void mainkey()
{
int k = 0;
cin >> k;
if(k == 0)
{
cout << "请输入您要注册的账号和密码, 要求都是整数" << endl;
int zhanghao, mima;
cin >> zhanghao >> mima;
struct Stu tmp;
tmp = {zhanghao, mima, 0, 0, "/0", "/0", "/0", 0.0, 0};
insertNodeReader(Slist,tmp);
save2("reader.txt", Slist);
cout << "注册成功!请您登陆" << endl;
}
else if(k == 2)
{
cout << "请输入您的账号和密码" << endl;
int zhanghao, mima;
cin >> zhanghao >> mima;
int res = denglu(Slist,zhanghao, mima);
if(res == 1)
{
cout << "登陆成功" << endl;
while(1)
{
secondmenu();
secondkey(zhanghao);
system("pause");
system("cls");
}
}
else if(res == 0)
{
cout << "您输入的密码错误" << endl;
}
else if(res == 2)
{
cout << "您输入的账户错误或您还未注册账户" << endl;
}
}
else if(k == 1)
{
cout << "请输入管理员账号和密码:" << endl;
int num, password;
cin >> num >> password;
if(num == 888888 && password == 123456)
{
cout << "登陆成功" << endl;
while(1)
{
allmenu();
allkeydown();
system("pause");
system("cls");
}
}
else
{
cout << "登陆错误" << endl;
}
}
else if(k == 3)
{
cout << "请输入您原来的账号,密码,以及您要修改的新密码,以便验证您的身份" << endl;
int zhanghao, mima, newmima;
cin >> zhanghao >> mima >> newmima;
int res = xiugai(Slist, zhanghao, mima, newmima);
if(res == 1)
{
cout << "修改成功!" << endl;
save2("reader.txt", Slist);
}
else
{
cout << "您输入的账号或密码有误请您检查后重试!" << endl;
}
}
}
int main(int argc, const char * argv[])
{
list = createHead();
Slist = createSHead();
readFile("bookinfo.txt", list);
readFile2("reader.txt", Slist);
while(1)
{
mainmenu();
mainkey();
system("pause");
system("cls");
}
return 0;
}
标签:课程设计,语言,一下,next,headNode,printf,pMove,data,struct
From: https://www.cnblogs.com/cxy8/p/17169514.html