我做了一个书店管理系统的C++程序,目前存在以下错误:
1、在VS code中调试程序,中文部分全部为乱码,更改cpp文件编码格式(UTF-8和ANSI)都没有用
2、保存图书信息时会覆盖掉过去的记录
3、图书信息查询时,不能正常查询
(包括:不能从读取的数据中获取信息,例如:我在data.txt中录入了图书A的信息,程序成功读取该文件,但不能查找到这本书;当此录入书籍后不能正常查询)
请帮我检查一下是哪里出了问题?
#include <iostream>
#include <fstream>
#include <vector>
#include <map>
#include <set>
#include <ctime>
#include <iomanip>
#include <windows.h>
using namespace std;
class Book {
public:
string name;
string author;
string publisher;
int pages;
time_t latest_time;
int stock;
double price;
Book(string n, string a, string p, int pg, int s, double pr) {
name = n;
author = a;
publisher = p;
pages = pg;
latest_time = time(NULL);
stock = s;
price = pr;
}
friend ostream& operator<<(ostream& os, const Book& book) {
os << "图书书名:" << book.name << endl;
os << "图书作者:" << book.author << endl;
os << "图书出版社:" << book.publisher << endl;
os << "图书页数:" << book.pages << endl;
os << "最新入库时间:" << put_time(localtime(&book.latest_time), "%Y-%m-%d %H:%M:%S") << endl;
os << "图书库存量:" << book.stock << endl;
os << "图书价格:" << book.price << endl;
return os;
}
};
class PurchaseRecord {
public:
string name;
string author;
string publisher;
int pages;
time_t time1;
int quantity;
double price;
PurchaseRecord(string n, string a, string p, int pg, int q, double pr) {
name = n;
author = a;
publisher = p;
pages = pg;
time1 = time(NULL);
quantity = q;
price = pr;
}
friend ostream& operator<<(ostream& os, const PurchaseRecord& record) {
os << "图书书名:" << record.name << endl;
os << "图书作者:" << record.author << endl;
os << "图书出版社:" << record.publisher << endl;
os << "图书页数:" << record.pages << endl;
os << "图书入库时间:" << put_time(localtime(&record.time1), "%Y-%m-%d %H:%M:%S") << endl;
os << "图书进货数量:" << record.quantity << endl;
os << "图书进货价格:" << record.price << endl;
return os;
}
};
class SalesRecord {
public:
string name;
string author;
string publisher;
int pages;
time_t time2;
int quantity;
double price;
SalesRecord (string n, string a, string p, int pg, int q, double pr) {
name = n;
author = a;
publisher = p;
pages = pg;
time2 = time(NULL);
quantity = q;
price = pr;
}
friend ostream& operator<<(ostream& os, const SalesRecord& record) {
os << "图书书名:" << record.name << endl;
os << "图书作者:" << record.author << endl;
os << "图书出版社:" << record.publisher << endl;
os << "图书页数:" << record.pages << endl;
os << "图书销售时间:" << put_time(localtime(&record.time2), "%Y-%m-%d %H:%M:%S") << endl;
os << "图书销售数量:" << record.quantity << endl;
os << "图书销售价格:" << record.price << endl;
return os;
}
};
class Bookstore
{
private:
vector<Book> books;
vector<PurchaseRecord> purchase_records;
vector<SalesRecord> sales_records;
set<string> warning_books;
public:
void add_book(string n, string a, string p, int pg, int s, double pr)
{
Book book(n, a, p, pg, s, pr); books.push_back(book);
}
void purchase(string n, string a, string p, int pg, int q, double pr) {
PurchaseRecord record(n, a, p, pg, q, pr);
purchase_records.push_back(record);
for (auto& book : books) {
if (book.name == n && book.author == a && book.publisher == p && book.pages == pg) {
book.stock += q;
book.latest_time = time(NULL);
if (book.stock <= 1) {
warning_books.insert(n);
}
return;
}
}
add_book(n, a, p, pg, q, pr);
warning_books.insert(n);
}
void sell(string n, string a, string p, int pg, int q, double pr) {
SalesRecord record(n, a, p, pg, q, pr);
sales_records.push_back(record);
for (auto& book : books) {
if (book.name == n && book.author == a && book.publisher == p && book.pages == pg) {
book.stock -= q;
book.latest_time = time(NULL);
if (book.stock <= 1) {
warning_books.insert(n);
}
return;
}
}
}
void query_book(string n, string a)
{
for (auto& book : books) {
if (book.name == n && book.author == a)
{
cout << book << endl;
return;
}
}
cout << "Error Message : Warning! The book information was not found! Please enter the correct book information!" << endl;
}
void warning()
{
if (warning_books.empty())
{
cout << "Message : There are currently no books that require warning!" << endl;
return;
}
cout << "Message : The inventory of the following books is 1. Please purchase them in a timely manner:" << endl;
for (auto& book : warning_books)
{
cout << book << endl;
}
}
void save_data()
{
ofstream fout("data.txt");
if (!fout)
{
cout << "Error Message : File opening failed!"<< endl;
return;
}
fout << "图书信息:" << endl;
for (auto& book : books) {
fout << book << endl;
}
fout << "图书进货记录:" << endl;
for (auto& record : purchase_records) {
fout << record << endl;
}
fout << "图书销售记录:" << endl;
for (auto& record : sales_records) {
fout << record << endl;
}
fout.close();
cout << "Message : Data saved successfully!" << endl;
}
void load_data()
{
ifstream fin("data.txt");
if (!fin) {
cout << "Error Message : File opening failed!" << endl;
return;
}
string line;
while (getline(fin, line)) {
if (line == "图书信息:") {
while (getline(fin, line) && line != "进货记录:") {
string name, author, publisher;
int pages, stock;
double price;
time_t latest_time;
sscanf(line.c_str(), "书名:%s 作者:%s 出版社:%s 页数:%d 最新入库时间:%ld 库存量:%d 价格:%lf",
&name, &author, &publisher, &pages, &latest_time, &stock, &price);
Book book(name, author, publisher, pages, stock, price);
book.latest_time = latest_time;
books.push_back(book);
if (stock == 1) {
warning_books.insert(name);
}
}
}
else if (line == "进货记录:") {
while (getline(fin, line) && line != "销售记录:") {
string name, author, publisher;
int pages, quantity;
double price;
time_t time;
sscanf(line.c_str(), "书名:%s 作者:%s 出版社:%s 页数:%d 入库时间:%ld 进货数量:%d 进货价格:%lf",
&name, &author, &publisher, &pages, &time, &quantity, &price);
PurchaseRecord record(name, author, publisher, pages, quantity, price);
record.time1 = time;
purchase_records.push_back(record);
}
}
else if (line == "销售记录:") {
while (getline(fin, line)) {
string name, author, publisher;
int pages, quantity;
double price;
time_t time;
sscanf(line.c_str(), "书名:%s 作者:%s 出版社:%s 页数:%d 销售时间:%ld 销售数量:%d 销售价格:%lf",
&name, &author, &publisher, &pages, &time, &quantity, &price);
SalesRecord record(name, author, publisher, pages, quantity, price);
record.time2 = time;
sales_records.push_back(record);
}
}
}
fin.close();
cout << "Message : Data read successful!"<< endl;
}
void show_menu() {
cout << "欢迎使用书店管理系统!" << endl;
cout << "1. 添加图书" << endl;
cout << "2. 进货入库" << endl;
cout << "3. 销售出货" << endl;
cout << "4. 查询图书信息" << endl;
cout << "5. 自动预警提示" << endl;
cout << "6. 保存数据到文件" << endl;
cout << "7. 从文件中读取数据" << endl;
cout << "0. 退出系统" << endl;
cout << "请输入操作编号:";
}
void run() {
int choice;
while (true) {
show_menu();
cin >> choice;
switch (choice) {
case 1: {
string name, author, publisher;
int pages, stock; double price;
cout << "请输入书名:"; cin >> name;
cout << "请输入作者:"; cin >> author;
cout << "请输入出版社:"; cin >> publisher;
cout << "请输入页数:"; cin >> pages;
cout << "请输入库存量:"; cin >> stock;
cout << "请输入价格:"; cin >> price;
add_book(name, author, publisher, pages, stock, price);
cout << "Message : Book added successfully!"<< endl;
break;
}
case 2: {
string name, author, publisher;
int pages, quantity;
double price;
cout << "请输入书名:";
cin >> name;
cout << "请输入作者:";
cin >> author;
cout << "请输入出版社:";
cin >> publisher;
cout << "请输入页数:";
cin >> pages;
cout << "请输入进货数量:";
cin >> quantity;
cout << "请输入进货价格:";
cin >> price;
purchase(name, author, publisher, pages, quantity, price);
cout << "Message : Succeeded in purchasing and warehousing!" << endl;
break;
}
case 3: {
string name, author, publisher;
int pages, quantity;
double price;
cout << "请输入书名:";
cin >> name;
cout << "请输入作者:";
cin >> author;
cout << "请输入出版社:";
cin >> publisher;
cout << "请输入页数:";
cin >> pages;
cout << "请输入销售数量:";
cin >> quantity;
cout << "请输入销售价格:";
cin >> price;
sell(name, author, publisher, pages, quantity, price);
cout << "Message : Sales shipment successful!" << endl;
break;
}
case 4: {
string name, author;
cout << "请输入书名:";
cin >> name;
cout << "请输入作者:";
cin >> author;
query_book(name, author);
break;
}
case 5: {
warning();
break;
}
case 6: {
save_data();
break;
}
case 7: {
load_data();
break;
}
case 0: {
cout << "感谢使用书店管理系统,再见!" << endl;
return;
}
default: {
cout << "Error Message : Warning! No valid input information detected! Please re-enter!" << endl;
break;
}
}
}
}
};
int main()
{
Bookstore bookstore;
bookstore.run();
return 0;
}