任务1:图书管理系统采用链表数据结构实现一“图书管理系统”,为用户提供管理和查询服务。图书信息包括8数据项:书名,ISBN、作者、出版社、出版时间,入库时间,库存量(图书馆购买此书的数量),借阅数(被借阅的数量等信息。完成以下操作:(1)录入:图书信息(至少10条记录);(2)删除:删除某一本图书的信息;(3)修改:修改某一本图书的信息;(4)查询:根据书名或图书ISBN等查询图书信息(查询方法不限)。(5)借书:借出一本书,该书的借阅数增加一本。(6)还书:还回一本书,该书的借阅数减少一本。(7)统计:统计所有借出的图书数目,即所有图书借阅数之和。(8)输出:输出所查询图书的信息,要求格式清晰,方便查看。程序要添加适当的注释,程序的书写要采用缩进格式。(2)程序要具有健壮性,即当输入非法时,程序也能适当地做出反应。(3)程序要有操作菜单,在程序运行时可根据相应的菜单提示进行操作。
时间: 2023-11-28 07:48:50 浏览: 93
以下是基于链表数据结构实现的图书管理系统的代码实现:
```python
class Book:
def __init__(self, book_name, isbn, author, publisher, publish_time, storage_time, stock, borrow):
self.book_name = book_name
self.isbn = isbn
self.author = author
self.publisher = publisher
self.publish_time = publish_time
self.storage_time = storage_time
self.stock = stock
self.borrow = borrow
class BookLinkedList:
def __init__(self):
self.head = None
self.tail = None
self.size = 0
def append(self, book):
if self.head is None:
self.head = book
self.tail = book
else:
self.tail.next = book
self.tail = book
self.size += 1
def search_by_name(self, name):
current = self.head
while current:
if current.book_name == name:
return current
current = current.next
return None
def search_by_isbn(self, isbn):
current = self.head
while current:
if current.isbn == isbn:
return current
current = current.next
return None
def remove(self, book):
current = self.head
previous = None
while current:
if current == book:
if previous:
previous.next = current.next
else:
self.head = current.next
if current.next is None:
self.tail = previous
self.size -= 1
return True
previous = current
current = current.next
return False
def borrow_book(self, name):
book = self.search_by_name(name)
if book is not None and book.stock > 0:
book.stock -= 1
book.borrow += 1
return True
return False
def return_book(self, name):
book = self.search_by_name(name)
if book is not None:
book.stock += 1
book.borrow -= 1
return True
return False
def total_borrowed(self):
count = 0
current = self.head
while current:
count += current.borrow
current = current.next
return count
def display(self):
current = self.head
while current:
print("书名:", current.book_name)
print("ISBN:", current.isbn)
print("作者:", current.author)
print("出版社:", current.publisher)
print("出版时间:", current.publish_time)
print("入库时间:", current.storage_time)
print("库存量:", current.stock)
print("借阅数:", current.borrow)
print()
current = current.next
def main():
book_list = BookLinkedList()
book1 = Book("Python编程从入门到实践", "978-7-121-29490-3", "Eric Matthes", "人民邮电出版社", "2016-11-1", "2016-11-2", 10, 0)
book_list.append(book1)
book2 = Book("流畅的Python", "978-7-115-49674-4", "Luciano Ramalho", "人民邮电出版社", "2018-1-1", "2018-1-2", 5, 0)
book_list.append(book2)
book3 = Book("从0开始学Python爬虫", "978-7-115-49674-5", "张勇", "人民邮电出版社", "2019-1-1", "2019-1-2", 8, 0)
book_list.append(book3)
book4 = Book("Python数据分析", "978-7-121-32011-1", "Wes McKinney", "机械工业出版社", "2019-10-1", "2019-10-2", 12, 0)
book_list.append(book4)
book5 = Book("Python网络爬虫", "978-7-121-30139-0", "崔庆才", "电子工业出版社", "2019-3-1", "2019-3-2", 6, 0)
book_list.append(book5)
book6 = Book("Python编程艺术", "978-7-121-30256-4", "Stuart Loyer", "电子工业出版社", "2019-4-1", "2019-4-2", 3, 0)
book_list.append(book6)
book7 = Book("Python机器学习", "978-7-111-61084-8", "Sebastian Raschka", "人民邮电出版社", "2017-11-1", "2017-11-2", 7, 0)
book_list.append(book7)
book8 = Book("Python深度学习", "978-7-121-33687-8", "Francois Chollet", "人民邮电出版社", "2018-11-1", "2018-11-2", 4, 0)
book_list.append(book8)
book9 = Book("Python数据可视化", "978-7-121-30673-9", "Hadley Wickham", "机械工业出版社", "2019-7-1", "2019-7-2", 9, 0)
book_list.append(book9)
book10 = Book("Python自然语言处理", "978-7-121-34518-4", "Steven Bird", "人民邮电出版社", "2019-12-1", "2019-12-2", 11, 0)
book_list.append(book10)
while True:
print("1. 录入图书信息")
print("2. 删除图书信息")
print("3. 修改图书信息")
print("4. 查询图书信息")
print("5. 借出图书")
print("6. 归还图书")
print("7. 统计借出图书总数")
print("8. 输出所有图书信息")
print("0. 退出")
choice = input("请输入操作码:")
if choice == "1":
book_name = input("请输入书名:")
isbn = input("请输入ISBN:")
author = input("请输入作者:")
publisher = input("请输入出版社:")
publish_time = input("请输入出版时间:")
storage_time = input("请输入入库时间:")
stock = int(input("请输入库存量:"))
borrow = int(input("请输入借阅数:"))
book = Book(book_name, isbn, author, publisher, publish_time, storage_time, stock, borrow)
book_list.append(book)
print("图书信息录入成功!")
elif choice == "2":
name = input("请输入要删除的图书名:")
book = book_list.search_by_name(name)
if book is not None:
book_list.remove(book)
print("图书信息删除成功!")
else:
print("图书信息不存在!")
elif choice == "3":
name = input("请输入要修改的图书名:")
book = book_list.search_by_name(name)
if book is not None:
book_name = input("请输入书名:")
isbn = input("请输入ISBN:")
author = input("请输入作者:")
publisher = input("请输入出版社:")
publish_time = input("请输入出版时间:")
storage_time = input("请输入入库时间:")
stock = int(input("请输入库存量:"))
borrow = int(input("请输入借阅数:"))
book.book_name = book_name
book.isbn = isbn
book.author = author
book.publisher = publisher
book.publish_time = publish_time
book.storage_time = storage_time
book.stock = stock
book.borrow = borrow
print("图书信息修改成功!")
else:
print("图书信息不存在!")
elif choice == "4":
keyword = input("请输入要查询的书名或ISBN:")
book = book_list.search_by_name(keyword)
if book is not None:
print("查询结果如下:")
print("书名:", book.book_name)
print("ISBN:", book.isbn)
print("作者:", book.author)
print("出版社:", book.publisher)
print("出版时间:", book.publish_time)
print("入库时间:", book.storage_time)
print("库存量:", book.stock)
print("借阅数:", book.borrow)
print()
else:
book = book_list.search_by_isbn(keyword)
if book is not None:
print("查询结果如下:")
print("书名:", book.book_name)
print("ISBN:", book.isbn)
print("作者:", book.author)
print("出版社:", book.publisher)
print("出版时间:", book.publish_time)
print("入库时间:", book.storage_time)
print("库存量:", book.stock)
print("借阅数:", book.borrow)
print()
else:
print("图书信息不存在!")
elif choice == "5":
name = input("请输入要借出的图书名:")
if book_list.borrow_book(name):
print("借出成功!")
else:
print("借出失败!")
elif choice == "6":
name = input("请输入要归还的图书名:")
if book_list.return_book(name):
print("归还成功!")
else:
print("归还失败!")
elif choice == "7":
count = book_list.total_borrowed()
print("借出图书总数为:", count)
elif choice == "8":
book_list.display()
elif choice == "0":
print("谢谢使用!")
break
else:
print("操作码输入有误,请重新输入!")
if __name__ == "__main__":
main()
```
在这个程序中,我们定义了一个 `Book` 类来表示图书信息,包括书名、ISBN、作者、出版社、出版时间、入库时间、库存量和借阅数等信息。然后我们又定义了一个基于链表数据结构的 `BookLinkedList` 类来管理图书信息。
在 `BookLinkedList` 类中,我们实现了以下方法:
- `append(book)`:向链表中添加一本图书。
- `search_by_name(name)`:根据书名查找图书信息。
- `search_by_isbn(isbn)`:根据ISBN查找图书信息。
- `remove(book)`:删除一本图书的信息。
- `borrow_book(name)`:借出一本图书。
- `return_book(name)`:归还一本图书。
- `total_borrowed()`:统计所有借出的图书数目。
- `display()`:输出所有图书信息。
在程序运行时,我们通过一个操作菜单来提供用户管理和查询图书信息的服务,包括录入、删除、修改、查询、借出、归还、统计和输出等功能。
为了保证程序的健壮性,我们在代码中添加了一些错误处理机制,比如在用户输入非法操作码时,程序会提示用户重新输入;在用户查询不存在的图书信息时,程序会给出相应的提示信息。
阅读全文