用c++代码实现下面的要求 需要程序能够完整运行 设计一个计算机管理系统完成图书管理基本业务 基本要求: 1) 每种书的登记内容包括书号、书名、著作者、现存量和库存量 2) 对书号建立索引表(线性表)以提高查找效率(索引表采用树表) 3) 系统主要功能如下: *采编入库:新购一种书,确定书号后,登记到图书账目表中,如果表中已有,则只将库存量增加; *借阅:如果一种书的现存量大于0,则借出一本,登记借阅这的书证号和归还日期,改变现存量 *归还:注销对借阅者的登记,改变该书的现存量。
时间: 2024-04-07 19:30:02 浏览: 83
用c++实现图书管理系统
以下是使用C++实现的计算机管理系统的代码:
```cpp
#include <iostream>
#include <string>
#include <vector>
using namespace std;
// 图书类
class Book {
public:
string bookNumber;
string bookName;
string author;
int stockNum;
int inventoryNum;
Book(string number, string name, string writer, int stock, int inventory) {
bookNumber = number;
bookName = name;
author = writer;
stockNum = stock;
inventoryNum = inventory;
}
};
// 索引表节点类
class IndexNode {
public:
string bookNumber;
Book* bookPtr;
IndexNode* leftChild;
IndexNode* rightChild;
IndexNode(string number, Book* book) {
bookNumber = number;
bookPtr = book;
leftChild = nullptr;
rightChild = nullptr;
}
};
// 索引表类
class IndexTable {
public:
IndexNode* root;
IndexTable() {
root = nullptr;
}
void insertNode(IndexNode* node) {
if (root == nullptr) {
root = node;
return;
}
IndexNode* current = root;
while (true) {
if (node->bookNumber < current->bookNumber) {
if (current->leftChild == nullptr) {
current->leftChild = node;
break;
}
else {
current = current->leftChild;
}
}
else if (node->bookNumber > current->bookNumber) {
if (current->rightChild == nullptr) {
current->rightChild = node;
break;
}
else {
current = current->rightChild;
}
}
else {
// 书号已存在,只增加库存量
current->bookPtr->stockNum += node->bookPtr->stockNum;
delete node;
break;
}
}
}
Book* searchBook(string bookNumber) {
IndexNode* current = root;
while (current != nullptr) {
if (bookNumber < current->bookNumber) {
current = current->leftChild;
}
else if (bookNumber > current->bookNumber) {
current = current->rightChild;
}
else {
return current->bookPtr;
}
}
return nullptr;
}
};
// 图书管理系统类
class BookManagementSystem {
public:
IndexTable indexTable;
vector<Book*> bookList;
void addBook(string bookNumber, string bookName, string author, int stock, int inventory) {
Book* book = new Book(bookNumber, bookName, author, stock, inventory);
IndexNode* indexNode = new IndexNode(bookNumber, book);
indexTable.insertNode(indexNode);
bookList.push_back(book);
}
void borrowBook(string bookNumber) {
Book* book = indexTable.searchBook(bookNumber);
if (book != nullptr && book->stockNum > 0) {
book->stockNum -= 1;
cout << "借阅成功!" << endl;
}
else {
cout << "该书不存在或库存不足!" << endl;
}
}
void returnBook(string bookNumber) {
Book* book = indexTable.searchBook(bookNumber);
if (book != nullptr) {
book->stockNum += 1;
cout << "归还成功!" << endl;
}
else {
cout << "该书不存在!" << endl;
}
}
};
int main() {
BookManagementSystem system;
// 添加图书
system.addBook("001", "C++ Primer", "Stanley B. Lippman", 10, 10);
system.addBook("002", "Effective Modern C++", "Scott Meyers", 5, 5);
system.addBook("003", "Clean Code", "Robert C. Martin", 8, 8);
// 借阅图书
system.borrowBook("001");
system.borrowBook("002");
system.borrowBook("003");
system.borrowBook("004"); // 不存在的书号
// 归还图书
system.returnBook("001");
system.returnBook("002");
system.returnBook("003");
system.returnBook("004"); // 不存在的书号
return 0;
}
```
这段代码实现了一个简单的计算机管理系统,包括添加图书、借阅图书和归还图书的功能。你可以根据需求进行修改和扩展。
阅读全文