图书馆管理系统书的数量打印在txt文件中 并可以读取txt文件中书名 页数
时间: 2024-05-12 09:13:04 浏览: 13
实现这个功能需要以下几个步骤:
1. 在图书馆管理系统中记录每本书的数量,并将数量保存到一个txt文件中。可以使用Python中的文件处理库(如os和io)来实现。
2. 对于读取txt文件中的书名和页数,可以使用Python中内置的文件读取函数(如open()和readline())。
以下是一个简单的示例代码,可以帮助你开始实现这个功能:
```python
# 记录每本书的数量
books = {'book1': 10, 'book2': 15, 'book3': 20}
with open('book_count.txt', 'w') as f:
for book, count in books.items():
f.write(f"{book}: {count}\n")
f.close()
# 读取txt文件中的书名和页数
with open('book_count.txt', 'r') as f:
for line in f.readlines():
book, count = line.strip().split(': ')
print(f"Book name: {book}, Page count: {count}")
f.close()
```
上面的代码将图书馆中每本书的数量保存在`book_count.txt`文件中,并将其打印出来。你可以根据自己的需要修改代码中的书籍信息和文件名。
相关问题
图书馆管理系统书的数量打印在txt文件中 并可以读取txt文件中书名 页数c++
以下是一个简单的图书馆管理系统的示例代码,可以实现添加书籍、删除书籍、查看所有书籍信息、将所有书籍数量打印到 txt 文件中,并可以读取 txt 文件中的书名和页数。
```c++
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
struct Book {
string name;
int pages;
};
vector<Book> books;
void addBook() {
Book book;
cout << "请输入书名:";
cin >> book.name;
cout << "请输入页数:";
cin >> book.pages;
books.push_back(book);
cout << "添加成功!" << endl;
}
void deleteBook() {
string name;
cout << "请输入要删除的书名:";
cin >> name;
auto it = find_if(books.begin(), books.end(), [&](const Book& book) {
return book.name == name;
});
if (it == books.end()) {
cout << "没有找到该书籍!" << endl;
} else {
books.erase(it);
cout << "删除成功!" << endl;
}
}
void showBooks() {
if (books.empty()) {
cout << "当前没有任何书籍!" << endl;
} else {
cout << "所有书籍信息如下:" << endl;
for (const auto& book : books) {
cout << book.name << ",页数:" << book.pages << endl;
}
}
}
void printCountToTxt() {
ofstream fout("count.txt");
if (!fout) {
cout << "无法打开文件!" << endl;
return;
}
fout << books.size();
fout.close();
cout << "书籍数量已经保存到 count.txt 文件中!" << endl;
}
void readNameFromTxt() {
ifstream fin("names.txt");
if (!fin) {
cout << "无法打开文件!" << endl;
return;
}
string name;
while (getline(fin, name)) {
cout << "书名:" << name << endl;
}
fin.close();
}
int main() {
while (true) {
cout << endl << "请选择操作:" << endl;
cout << "1. 添加书籍" << endl;
cout << "2. 删除书籍" << endl;
cout << "3. 查看所有书籍信息" << endl;
cout << "4. 将所有书籍数量打印到 txt 文件中" << endl;
cout << "5. 从 txt 文件中读取书名" << endl;
cout << "6. 退出" << endl;
int choice;
cin >> choice;
switch (choice) {
case 1:
addBook();
break;
case 2:
deleteBook();
break;
case 3:
showBooks();
break;
case 4:
printCountToTxt();
break;
case 5:
readNameFromTxt();
break;
case 6:
return 0;
default:
cout << "无效的选项!" << endl;
}
}
}
```
在这个示例代码中,我们通过使用 `vector` 来存储所有的书籍信息。`addBook` 函数用于添加一本新书,`deleteBook` 函数用于删除一本书,`showBooks` 函数用于显示所有书籍的信息。`printCountToTxt` 函数用于将所有书籍数量打印到一个 txt 文件中,`readNameFromTxt` 函数用于从另一个 txt 文件中读取书名。
注意,在使用文件操作时,要确保文件能够正确打开,操作完成后要关闭文件。此外,在读取文件时,要使用 `getline` 函数逐行读取文件内容。
请设计一个书店管理系统 能完成书店的日程管理工作,用c++并生成代码 包括:书店,图书,时间三个类。 叔带你 要求完成的基本功能: 1.进货入库功能,先判断书店里有无该图书,没有则输入该图书的相信信息进行入库,有则只需要在原有的基础上增添数量即可。 2.销售出货记录。 3、图书信息查询:可通过书名、作者等途径查询某本图书的详细信息(含书名、作者、出版社、页数、最新入库时间、库存量、价格等)。 4、自动预警提示(当某图书的库存量为1时自动预警提示) 5、数据存储:以文件方式存储出入库记录 对象集合的管理必须使用STL的相关容器类。
以下是C++书店管理系统的代码实现:
```c++
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <ctime>
using namespace std;
class Book {
public:
string name; // 书名
string author; // 作者
string publisher; // 出版社
int pages; // 页数
int stock; // 库存量
double price; // 价格
time_t last_in; // 最新入库时间
Book(string name, string author, string publisher, int pages, int stock, double price) {
this->name = name;
this->author = author;
this->publisher = publisher;
this->pages = pages;
this->stock = stock;
this->price = price;
time(&this->last_in); // 记录最新入库时间
}
void print_info() {
cout << "书名:" << name << endl;
cout << "作者:" << author << endl;
cout << "出版社:" << publisher << endl;
cout << "页数:" << pages << endl;
cout << "库存量:" << stock << endl;
cout << "价格:" << price << endl;
cout << "最新入库时间:" << ctime(&last_in) << endl;
}
};
class BookStore {
private:
vector<Book> books; // 图书集合
public:
void import_book() {
string name, author, publisher;
int pages, stock;
double price;
cout << "请输入书名:";
cin >> name;
// 判断是否已存在该图书,如果存在则只增加库存量
bool found = false;
for (auto& book : books) {
if (book.name == name) {
cout << "请输入增加的库存量:";
cin >> stock;
book.stock += stock;
found = true;
break;
}
}
if (!found) {
cout << "请输入作者:";
cin >> author;
cout << "请输入出版社:";
cin >> publisher;
cout << "请输入页数:";
cin >> pages;
cout << "请输入库存量:";
cin >> stock;
cout << "请输入价格:";
cin >> price;
books.emplace_back(name, author, publisher, pages, stock, price);
}
}
void sell_book() {
string name;
int amount;
cout << "请输入书名:";
cin >> name;
// 判断是否有足够的库存
bool found = false;
for (auto& book : books) {
if (book.name == name) {
cout << "请输入售出数量:";
cin >> amount;
if (amount <= book.stock) {
book.stock -= amount;
found = true;
break;
} else {
cout << "库存不足!" << endl;
}
}
}
if (!found) {
cout << "未找到该图书!" << endl;
}
}
void query_book() {
string name, author;
bool found = false;
cout << "请输入书名或作者:";
cin >> name;
for (auto& book : books) {
if (book.name == name || book.author == name) {
book.print_info();
found = true;
}
}
if (!found) {
cout << "未找到该图书!" << endl;
}
}
void warn_book() {
for (auto& book : books) {
if (book.stock == 1) {
cout << "图书《" << book.name << "》库存量为1,请及时补货!" << endl;
}
}
}
void save_data() {
ofstream ofs("bookstore.dat");
for (auto& book : books) {
ofs << book.name << " " << book.author << " " << book.publisher << " "
<< book.pages << " " << book.stock << " " << book.price << " " << book.last_in << endl;
}
ofs.close();
}
void load_data() {
ifstream ifs("bookstore.dat");
string name, author, publisher;
int pages, stock;
double price;
time_t last_in;
while (ifs >> name >> author >> publisher >> pages >> stock >> price >> last_in) {
books.emplace_back(name, author, publisher, pages, stock, price);
books.back().last_in = last_in;
}
ifs.close();
}
};
class Time {
public:
int year, month, day;
Time() {
time_t now = time(nullptr);
struct tm* t = localtime(&now);
year = t->tm_year + 1900;
month = t->tm_mon + 1;
day = t->tm_mday;
}
void print_date() {
cout << year << "-" << month << "-" << day << endl;
}
};
int main() {
BookStore store;
Time t;
int choice;
store.load_data();
do {
cout << "1.进货入库\n2.销售出货\n3.图书查询\n4.自动预警\n5.退出" << endl;
cout << "请输入操作编号:";
cin >> choice;
switch (choice) {
case 1:
store.import_book();
break;
case 2:
store.sell_book();
break;
case 3:
store.query_book();
break;
case 4:
cout << "今天是";
t.print_date();
store.warn_book();
break;
case 5:
store.save_data();
cout << "谢谢使用!" << endl;
break;
default:
cout << "无效的操作!" << endl;
break;
}
} while (choice != 5);
return 0;
}
```
在该实现中,Book 表示图书类,包括书名、作者、出版社、页数、库存量、价格和最新入库时间等属性,以及打印图书信息的方法。BookStore 表示书店类,包括图书集合,以及进货入库、销售出货、图书查询和自动预警等方法。Time 表示时间类,包括年、月、日等属性,以及打印日期的方法。在 main 函数中,使用一个循环读取用户的操作选择,并调用相应的方法。同时,读写数据采用文件方式存储,便于数据的持久化。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![none](https://img-home.csdnimg.cn/images/20210720083646.png)