用go语言怎么完成图书管理信息系统程序功能:参考学生管理系统对象改变为图书即可。扩展:1、实际应用价值2、检索算法实现怎么用代码实现
由于题目比较抽象,我假设这是一个简单的图书借阅管理系统,下面给出一个示例程序,实现基本的增加、删除、查询、借阅、归还等功能。
package main
import (
"fmt"
)
type Book struct {
id int
name string
author string
borrowed bool
}
var books []Book
func main() {
for {
var op int
fmt.Println("1. Add a book")
fmt.Println("2. Delete a book")
fmt.Println("3. Search for a book")
fmt.Println("4. Borrow a book")
fmt.Println("5. Return a book")
fmt.Println("6. List all books")
fmt.Println("7. Exit")
fmt.Print("Please select an operation: ")
fmt.Scanln(&op)
switch op {
case 1:
addBook()
case 2:
deleteBook()
case 3:
searchBook()
case 4:
borrowBook()
case 5:
returnBook()
case 6:
listBooks()
case 7:
fmt.Println("Goodbye!")
return
default:
fmt.Println("Invalid operation!")
}
}
}
func addBook() {
var book Book
fmt.Print("Please enter book id: ")
fmt.Scanln(&book.id)
fmt.Print("Please enter book name: ")
fmt.Scanln(&book.name)
fmt.Print("Please enter book author: ")
fmt.Scanln(&book.author)
book.borrowed = false
books = append(books, book)
fmt.Println("Book added successfully!")
}
func deleteBook() {
var id int
fmt.Print("Please enter book id: ")
fmt.Scanln(&id)
for i, book := range books {
if book.id == id {
books = append(books[:i], books[i+1:]...)
fmt.Println("Book deleted successfully!")
return
}
}
fmt.Println("Book not found!")
}
func searchBook() {
var keyword string
fmt.Print("Please enter keyword: ")
fmt.Scanln(&keyword)
found := false
for _, book := range books {
if book.name == keyword || book.author == keyword {
fmt.Printf("Book id: %d, name: %s, author: %s, borrowed: %t\n", book.id, book.name, book.author, book.borrowed)
found = true
}
}
if !found {
fmt.Println("Book not found!")
}
}
func borrowBook() {
var id int
fmt.Print("Please enter book id: ")
fmt.Scanln(&id)
for i, book := range books {
if book.id == id {
if book.borrowed {
fmt.Println("Book already borrowed!")
} else {
books[i].borrowed = true
fmt.Println("Book borrowed successfully!")
}
return
}
}
fmt.Println("Book not found!")
}
func returnBook() {
var id int
fmt.Print("Please enter book id: ")
fmt.Scanln(&id)
for i, book := range books {
if book.id == id {
if !book.borrowed {
fmt.Println("Book already returned!")
} else {
books[i].borrowed = false
fmt.Println("Book returned successfully!")
}
return
}
}
fmt.Println("Book not found!")
}
func listBooks() {
for _, book := range books {
fmt.Printf("Book id: %d, name: %s, author: %s, borrowed: %t\n", book.id, book.name, book.author, book.borrowed)
}
}
这个程序实现了基本的增加、删除、查询、借阅、归还等功能,可以作为一个简单的图书借阅管理系统来使用。其中,borrowed字段表示该书是否被借阅,如果被借阅,则为true,否则为false。
至于检索算法的实现,这个需要根据具体的需求来决定,比如可以使用二分查找、哈希表、Trie树等算法来实现不同的检索功能
原文地址: https://www.cveoy.top/t/topic/hqYD 著作权归作者所有。请勿转载和采集!