Week7——图书管理系统
1.(图书管理系统)我们对于图书馆以后会买多少书是未知的,所以如果定义数组就有可能在将来的某一天,数组的大小不够用,本题需要用动态链表做。自己定义关于图书的结构体,该结构体包括:书号,书名,书类别(类别如通信,英语,计算机等),馆藏量,剩余量,本题需要的主要函数如下:初始化:程序运行之初,应该创建一个包含一定数量结点的链表。添加:当有新书进来时,需要创建一个结点,将新书信息存到这个结点当中,并将这个结点放在链表末尾。删除:删除指点信息的书目。排序:根据指定的关键字进行排序。查找:根据指定的要求进行查找。借书:将所借书的剩余量减1。还书:将所还的书的剩余量加1。初始的馆藏量可以通过键盘输入,也可以结合文件读写操作从预先写好的一个文件中读入。
整个程序的菜单如下:
0.退出
0.返回上一级菜单(意思就是不想添加了,所以只有返回)
1.添加
1.添加图书
0.返回上一级菜单(意思同上)
2.删除 1.根据书号删除(输入书号,如果该书号的书就删除,否则提示没有该书)
2.根据书名删除(输入书名,如果该书号的书就删除,否则提示没有该书)
0.返回上一级菜单(意思同上)
1.根据书号排序
3.排序
2.根据藏书量排序
0.返回上一级菜单(意思同上)
4.查找 1.根据书名查找
2.根据书号查找
0.返回上一级菜单(意思同上)
5.借书
1.借书(输入书号,如果没有该书的书号则进行提示,该书剩余量为0时,则应该提示已借完,否则然后将馆藏量减1)
0.返回上一级菜单(意思同上)
6.还书
1.还书(输入书号,如果没有该书的书号则进行提示,否则然后将馆藏量加1)
程序这里写代码片
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<algorithm>
#include<stack>
#include<queue>
#include<cstdlib>
using namespace std;
typedef struct library List;
typedef struct library Position;
struct library
{
int offical_number;
char book_name[100];
int book_num;
List *next;
};
void Primary_menu();
List *Add_book();
void Delete_book();
void Sort_book();
void Find_book();
void Use_book();
void Bake_book_menu();
void Exmine_library();
int menu();
List *Head, *Head1;
int main()
{
//std::ios::sync_with_stdio(false);
puts(" **************************");
puts(" **Week7——图书管理系统**");
puts(" **************************");
Primary_menu();
return 0;
}
void Primary_menu()
{
int flag = 1;
while (true)
{
if (!flag) break;
flag = 1;
switch (menu())
{
case 1:
Head1 = Add_book();
Head = Head1->next; break;//aldery;
case 2:
Delete_book(); break;//aldery
case 3:
Sort_book(); break;
case 4:
Find_book(); break;
case 5:
Use_book(); break;
case 6:
Bake_book_menu(); break;
case 7:
Exmine_library(); break;
default:
puts(" Goodbay\n"); flag = 0;
}
}
}
int menu()
{
puts(" 0,End Program");
puts(" 1.Add_book");
puts(" 2,Delete_book");
puts(" 3,Sort_book");
puts(" 4,Find_book");
puts(" 5,Lent_book");
puts(" 6,Back_book");
puts(" 7 output library information");
puts(" Please intput'0--7'");
int n, cn;
do
{
cin >> n;
cn = n;
} while (n < 0 || n>7);
return cn;
}
List *Add_book()
{
List *head, *p, *tail;
head = (List*)malloc(sizeof(List));
head->next = NULL;
puts(" 输入的图书序号为'-1'时,则输入结束");
puts(" 请输入图书序号 书名 馆藏量\n 中间用空格隔开");
p = head;
while (1)
{
tail = (List*)malloc(sizeof(List));
cin >> tail->offical_number;
getchar();
if (tail->offical_number == -1) break;
cin >> tail->book_name >> tail->book_num;
p->next = tail;
p = tail;
}
p->next = NULL;
return head;
}
Position *Findnum(int x)
{
Position *L = Head;
if (L == NULL)
return false;
else
{
while (L)
{
if (L->offical_number == x)
return L;
L = L->next;
}
}
return false;
}
void delete1()
{
puts(" please intput position");
int pos;
cin >> pos;
Position *P = Findnum(pos);
List *L = Head;
if (P == false)
{
puts(" Delete error,please intput right position");
return;
}
if (L == NULL || pos == NULL)
{
puts(" Delete error,please intput right position");
return;
}
else
{
if (P == L)
{
L->book_num--;
if (!L->book_num)
{
L = L->next;
Head = L;
}
puts(" Delete success");
return;
}
else
{
while (L->next != NULL&&L->next != P)
L = L->next;
if (L->next != P)
{
puts(" Delete error,please intput right position");
return;
}
else
{
L->next->book_num--;
if (!L->next->book_num)
{
L->next = L->next->next;
}
puts(" Delete success");
}
}
}
}
Position *Findchar(char *x)
{
Position *L = Head;
if (L == NULL)
return false;
else
{
while (L)
{
if (!strcmp(L->book_name, x))
return L;
L = L->next;
}
}
return false;
}
void delete2()
{
puts("");
puts(" Please intput Book_name");
char pos[100];
cin >> pos;
Position *P = Findchar(pos);
List *L = Head;
if (P == false)
{
puts(" Delete error,please intput right position");
return;
}
if (L == NULL || pos == NULL)
{
puts(" Delete error,please intput right position");
return;
}
else
{
if (P == L)
{
L->book_num--;
if (!L->book_num)
{
L = L->next;
Head = L;
}
puts(" Delete success");
return;
}
else
{
while (L->next != NULL&&L->next != P)
L = L->next;
if (L->next != P)
{
puts(" Delete error,please intput right position");
return;
}
else
{
L->next->book_num--;
if (!L->next->book_num)
{
L->next = L->next->next;
}
puts(" Delete success");
}
}
}
}
void Delete_book()
{
puts(" 1.根据书号删除输入书号");
puts(" 2.根据书名删除输入书名\n*************");
puts(" 输入'0'返回上级菜单");
int n;
cin >> n;
if (n == 1)
delete1();
else if (n == 2)
delete2();
else if (n == 0)
Primary_menu();
else
{
puts(" Please inpute right number");
Delete_book();
}
}
void swap_num(int &a, int &b)
{
int temp = a;
a = b;
b = temp;
}
void swap_char(char *a, char *b)
{
char str[100];
strcpy(str, a);
strcpy(a, b);
strcpy(b, str);
}
void Sort_offical_num()
{
List *p, *q, *min;
for (p = Head; p != NULL; p = p->next)
{
min = p;
for (q = p->next; q != NULL; q = q->next)
{
if (q->offical_number < min->offical_number)
{
min = q;
}
}
if (min != p)
{
swap_num(min->book_num, p->book_num);
swap_num(min->offical_number, p->offical_number);
swap_char(min->book_name, p->book_name);
}
}
puts(" 排序成功");
}
void Sort_book_num()
{
List *p, *q, *min;
for (p = Head; p != NULL; p = p->next)
{
min = p;
for (q = p->next; q != NULL; q = q->next)
{
if (q->book_num < min->book_num)
{
min = q;
}
}
if (min != p)
{
swap_num(min->book_num, p->book_num);
swap_num(min->offical_number, p->offical_number);
swap_char(min->book_name, p->book_name);
}
}
puts(" 排序成功");
}
void Sort_book()
{
puts(" 1.根据书号排序");
puts(" 2.根据藏书量排序");
puts(" 0.返回上一级菜单");
int n;
cin >> n;
if (n == 1)
Sort_offical_num();
else if (n == 2)
Sort_book_num();
else if (!n)
void Sort_book();
else
menu();
}
void Find_book_name()
{
puts(" Please intput Book_name");
char Book_name[100];
cin >> Book_name;
List *L = Head;
int flag = 1;
if (L == NULL)
{
puts(" Library don't have this book.\nplease find again");
Find_book();
}
else
{
while (L)
{
if (!strcmp(Book_name, L->book_name))
{
flag = 0;
puts(" Libarary book information");
cout << L->offical_number << " ";
cout << L->book_name << " ";
cout << L->book_num << endl;
Find_book();
}
L = L->next;
}
}
if (flag == 1)
{
puts(" Library don't have this book.\nplease find again");
Find_book();
}
}
void Find_book_offical()
{
puts(" Please intput Book_offical");
int Book_offical;
cin >> Book_offical;
List *L = Head;
int flag = 1;
if (L == NULL)
{
puts(" Library don't have this book.\nplease find again");
Find_book();
}
else
{
while (L)
{
if (Book_offical == L->offical_number)
{
flag = 0;
puts(" Libarary book information");
cout << L->offical_number << " ";
cout << L->book_name << " ";
cout << L->book_num << endl;
Find_book();
}
L = L->next;
}
}
if (flag == 1)
{
puts(" Library don't have this book.\nplease find again");
Find_book();
}
}
void Find_book()
{
puts(" 1.根据书名查找");
puts(" 2.根据书号查找");
puts(" 0.返回上一级菜单");
int n;
cin >> n;
if (!n)
menu();
else if (n == 1)
Find_book_name();
else if (n == 2)
Find_book_offical();
else
menu();
}
void Read_book_offical()
{
puts(" Please intput Book_offical");
int Book_offical;
cin >> Book_offical;
List *L = Head;
if (L == NULL)
{
puts(" Library don't have this book.\nplease find again");
Find_book();
}
else
{
while (L)
{
if (Book_offical == L->offical_number)
{
if (L->book_num == 0)
puts(" This book don't have more.\nPlease lent other book");
else
{
puts(" Congratulation you have borrow this book ");
L->book_num--;
return;
}
}
}
}
puts(" Library don't have this book.\nplease find again");
Find_book();
}
void Use_book()
{
puts(" 1.借书 输入书号");
puts(" 0.返回上一级菜单");
int n;
cin >> n;
if (!n)
Primary_menu();
else if (n == 1)
{
Read_book_offical();
}
else
{
puts(" Please input right way");
Use_book();
}
}
void Back_book()
{
puts(" Pleae int Book_offical");
puts(" if you import '-1',return last menu");
int Book_offical;
cin >> Book_offical;
if (Book_offical == -1)
return;
List *L = Head;
if (L == NULL)
{
puts(" Library don't have this book.\nplease find again");
Find_book();
}
else
{
while (L)
{
if (Book_offical == L->offical_number)
{
puts(" return book sucess");
L->offical_number++;
return;
}
}
}
puts(" Library don't have this book.\nplease find again");
Back_book();
}
void Bake_book_menu()
{
puts(" 1.还书");
puts(" 0 Go to last menu");
int n;
cin >> n;
if (!n)
return;
else if (n == 1)
Back_book();
else
{
puts(" Intput error,please intput again");
Bake_book_menu();
}
}
void Exmine_library()
{
List *L;
L = Head;
puts(" oooooooooooooooooo");
puts("");
while (L)
{
printf(" ");
cout << L->offical_number << " " << L->book_name << " " << L->book_num << endl;
L = L->next;
}
puts("");
puts(" 0000000000000000000");
return;
}
运行实例
**************************
**Week7——图书管理系统**
**************************
0, End Program
1.Add_book
2, Delete_book
3, Sort_book
4, Find_book
5, Lent_book
6, Back_book
7 output library information
Please intput'0--7'
1
输入的图书序号为'-1'时,则输入结束
5241 my_love 8848
1314 never_say_never 888
520 no_say 1111
- 1
0, End Program
1.Add_book
2, Delete_book
3, Sort_book
4, Find_book
5, Lent_book
6, Back_book
7 output library information
Please intput'0--7'
2
1.根据书号删除输入书号
2.根据书名删除输入书名
*************
输入'0'返回上级菜单
1
please intput position
5241
Delete success
0, End Program
1.Add_book
2, Delete_book
3, Sort_book
4, Find_book
5, Lent_book
6, Back_book
7 output library information
Please intput'0--7'
7
oooooooooooooooooo
5241 my_love 8847
1314 never_say_never 888
520 no_say 1111
0000000000000000000
0, End Program
1.Add_book
2, Delete_book
3, Sort_book
4, Find_book
5, Lent_book
6, Back_book
7 output library information
Please intput'0--7'
2
1.根据书号删除输入书号
2.根据书名删除输入书名
*************
输入'0'返回上级菜单
2
Please intput Book_name
my_love
Delete success
0, End Program
1.Add_book
2, Delete_book
3, Sort_book
4, Find_book
5, Lent_book
6, Back_book
7 output library information
Please intput'0--7'
7
oooooooooooooooooo
5241 my_love 8846
1314 never_say_never 888
520 no_say 1111
0000000000000000000
0, End Program
1.Add_book
2, Delete_book
3, Sort_book
4, Find_book
5, Lent_book
6, Back_book
7 output library information
Please intput'0--7'
5
1.借书 输入书号
0.返回上一级菜单
0
0, End Program
1.Add_book
2, Delete_book
3, Sort_book
4, Find_book
5, Lent_book
6, Back_book
7 output library information
Please intput'0--7'
3
1.根据书号排序
2.根据藏书量排序
0.返回上一级菜单
1
排序成功
0, End Program
1.Add_book
2, Delete_book
3, Sort_book
4, Find_book
5, Lent_book
6, Back_book
7 output library information
Please intput'0--7'
7
oooooooooooooooooo
520 no_say 1111
1314 never_say_never 888
5241 my_love 8846
0000000000000000000
0, End Program
1.Add_book
2, Delete_book
3, Sort_book
4, Find_book
5, Lent_book
6, Back_book
7 output library information
Please intput'0--7'
3
1.根据书号排序
2.根据藏书量排序
0.返回上一级菜单
2
排序成功
0, End Program
1.Add_book
2, Delete_book
3, Sort_book
4, Find_book
5, Lent_book
6, Back_book
7 output library information
Please intput'0--7'
7
oooooooooooooooooo
1314 never_say_never 888
520 no_say 1111
5241 my_love 8846
0000000000000000000
0, End Program
1.Add_book
2, Delete_book
3, Sort_book
4, Find_book
5, Lent_book
6, Back_book
7 output library information
Please intput'0--7'
4
1.根据书名查找
2.根据书号查找
0.返回上一级菜单
1
Please intput Book_name
my_love
Libarary book information
5241 my_love 8846
1.根据书名查找
2.根据书号查找
0.返回上一级菜单
2
Please intput Book_offical
5241
Libarary book information
5241 my_love 8846
1.根据书名查找
2.根据书号查找
0.返回上一级菜单
0
0, End Program
1.Add_book
2, Delete_book
3, Sort_book
4, Find_book
5, Lent_book
6, Back_book
7 output library information
Please intput'0--7'
5
0, End Program
1.Add_book
2, Delete_book
3, Sort_book
4, Find_book
5, Lent_book
6, Back_book
7 output library information
Please intput'0--7'
6
1.还书(输入书号,如果没有该书的书号则进行提示,否则然后将馆藏量加1
0 Go to last menu
5241
Intput error, please intput again
1.还书(输入书号,如果没有该书的书号则进行提示,否则然后将馆藏量加1
0 Go to last menu
0
0, End Program
1.Add_book
2, Delete_book
3, Sort_book
4, Find_book
5, Lent_book
6, Back_book
7 output library information
Please intput'0--7'
6
1.还书(输入书号,如果没有该书的书号则进行提示,否则然后将馆藏量加1
0 Go to last menu
1
Pleae int Book_offical
- 1, return last menu
1314
return book sucess
0, End Program
1.Add_book
2, Delete_book
3, Sort_book
4, Find_book
5, Lent_book
6, Back_book
7 output library information
Please intput'0--7'
y