一个简单的图书管理系统,可以应付实验作业

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
#include <iomanip>
#include <limits>

// 声明一个结构体,用来保持图书信息
struct Book {
    std::string id;  // 书籍编号
    std::string title;  // 书籍名称
    std::string author;  // 作者姓名
    std::string category;  // 图书分类
    std::string publisher;  // 出版单位
    std::string publishDate;  // 出版时间
    double price;  // 书籍价格
};

// 函数声明
void clearScreen();     // 清屏函数
bool login();           // 登录函数
void Welcome();      // 登录欢迎页面函数
void printMenu();         //菜单函数        
void addBook(std::vector<Book>& books);             // 图书信息录入函数
void listBooks(const std::vector<Book>& books);    // 图书信息浏览函数
void searchBooks(const std::vector<Book>& books); // 图书信息查找函数
void deleteBook(std::vector<Book>& books);       // 删除图书信息
void deleteSingleBook(std::vector<Book>& books);// 删除单一数据
void deleteAllBooks(std::vector<Book>& books);// 删除全部数据
void modifyBook(std::vector<Book>& books);  // 图书信息修改函数

void clearScreen() {
#ifdef _WIN32
    system("cls");
#else
    system("clear");
#endif
}

bool login() {
    std::string username, password;
    std::cout << "================================================" << std::endl;
    std::cout << "======== 艺何浪图书信息管理系统 ========" << std::endl;
    std::cout << "================================================\n" << std::endl;
    std::cout << "================== 登录页面 ==================\n" << std::endl;

    for (int i = 0; i < 3; ++i) {
        std::cout << "请输入账号: ";
        std::cin >> username;
        std::cout << "请输入密码: ";
        std::cin >> password;
        // 登录逻辑验证
        if (username == "22" && password == "11") {
            return true;
            Welcome();


        }
        else {
            if (i < 2) {
                std::cout << "账号或密码错误,请重新输入。\n";
            }
            else {
                std::cout << "账号或密码错误超过三次,程序即将退出。\n";
                exit(0); // 退出程序
            }
        }
    }
    std::cin.get(); // 等待用户输入// 登录成功
    system("pause");
    return false; // 用户输入错误三次,登录失败
}

// 欢迎界面
void Welcome() {
    std::cout << "\t欢迎使用艺何浪图书信息管理系统!!!" << std::endl;
}
//主菜单函数
void printMenu() {
    std::cout << "================================================" << std::endl;
    std::cout << "======== 艺何浪图书信息管理系统 ========" << std::endl;
    std::cout << "================================================" << std::endl;
    std::cout << "\n\t1. 图 书 信 息 录 入" << std::endl;
    std::cout << "\t2. 图 书 信 息 浏 览" << std::endl;
    std::cout << "\t3. 图 书 信 息 查 询" << std::endl;
    std::cout << "\t4. 图 书 信 息 删 除" << std::endl;
    std::cout << "\t5. 图 书 信 息 修 改" << std::endl;
    std::cout << "\t6. 退 出 系 统 \n" << std::endl;
    std::cout << "\t请输入选择: ";
}

//书籍录入函数
void addBook(std::vector<Book>& books) {
    clearScreen();
    std::cout << "================================================" << std::endl;
    std::cout << "======== 艺何浪图书信息管理系统 ========" << std::endl;
    std::cout << "================================================\n" << std::endl;
    std::cout << "\t请选择录入方式:" << std::endl;
    std::cout << "\t1. 单个录入" << std::endl;
    std::cout << "\t2. 多个录入" << std::endl;
    std::cout << "\t3. 返回上一级\n" << std::endl;
    std::cout << "请输入选择: ";

    int choice;
    std::cin >> choice;

    // 清除输入缓冲区中的换行符
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

    switch (choice) {
    case 1:  // 单个录入
    {
        clearScreen();
        Book newBook;
        std::cout << "================================================" << std::endl;
        std::cout << "======== 艺何浪图书信息管理系统 ========" << std::endl;
        std::cout << "================================================\n" << std::endl;
        std::cout << "请输入书籍编号: ";
        std::cin >> newBook.id;
        std::cout << "请输入书籍名称: ";
        std::cin >> newBook.title;
        std::cout << "请输入作者姓名: ";
        std::cin >> newBook.author;
        std::cout << "请输入图书分类: ";
        std::cin >> newBook.category;
        std::cout << "请输入出版单位: ";
        std::cin >> newBook.publisher;

        // 获取合法的出版时间
        bool validPublishDate = false;
        while (!validPublishDate) {
            std::cout << "请输入出版时间: ";
            std::string input;
            std::cin >> input;
            if (input.find_first_not_of("0123456789") != std::string::npos) {
                std::cout << "输入有误,请重新输入" << std::endl;
            }
            else {
                newBook.publishDate = input;
                validPublishDate = true;
            }
        }
        // 获取合法的书籍价格
        bool validPrice = false;
        while (!validPrice) {
            std::cout << "请输入书籍价格: ";
            std::string input;
            std::cin >> input;
            if (input.find_first_not_of("0123456789.") != std::string::npos) {
                std::cout << "输入有误,请重新输入" << std::endl;
            }
            else {
                newBook.price = std::stod(input);
                validPrice = true;
            }
        }

        books.push_back(newBook);

        // 将书籍信息保存到文件
        std::ofstream outFile("books.txt", std::ios::app);
        if (outFile.is_open()) {
            outFile << newBook.id << '\n' << newBook.title << '\n'
                << newBook.author << '\n' << newBook.category << '\n'
                << newBook.publisher << '\n' << newBook.publishDate << '\n'
                << newBook.price << std::endl;
            outFile.close();
        }
        else {
            std::cout << "无法打开文件 books.txt" << std::endl;
        }
        std::cout << "\n书籍信息录入成功" << std::endl; // 添加提示信息
        system("pause");
        break;
    }
    case 2:  // 多个录入
    {
        clearScreen();
        int num;
        std::cout << "================================================" << std::endl;
        std::cout << "======== 艺何浪图书信息管理系统 ========" << std::endl;
        std::cout << "================================================\n" << std::endl;
        std::cout << "请输入你想要录入的书籍数量: ";
        std::cin >> num;
        for (int i = 0; i < num; ++i) {
            clearScreen();
            Book newBook;
            std::cout << "================================================" << std::endl;
            std::cout << "======== 艺何浪图书信息管理系统 ========" << std::endl;
            std::cout << "================================================\n" << std::endl;
            std::cout << "请输入书籍编号: ";
            std::cin >> newBook.id;
            std::cout << "请输入书籍名称: ";
            std::cin >> newBook.title;
            std::cout << "请输入作者姓名: ";
            std::cin >> newBook.author;
            std::cout << "请输入图书分类: ";
            std::cin >> newBook.category;
            std::cout << "请输入出版单位: ";
            std::cin >> newBook.publisher;

            // 获取合法的出版时间
            bool validPublishDate = false;
            while (!validPublishDate) {
                std::cout << "请输入出版时间( ): ";
                std::string input;
                std::cin >> input;
                if (input.find_first_not_of("0123456789") != std::string::npos) {
                    std::cout << "输入有误,请重新输入" << std::endl;
                }
                else {
                    newBook.publishDate = input;
                    validPublishDate = true;
                }
            }

            // 获取合法的书籍价格
            bool validPrice = false;
            while (!validPrice) {
                std::cout << "请输入书籍价格( ): ";
                std::string input;
                std::cin >> input;
                if (input.find_first_not_of("0123456789.") != std::string::npos) {
                    std::cout << "输入有误,请重新输入" << std::endl;
                }
                else {
                    newBook.price = std::stod(input);
                    validPrice = true;
                }
            }

            books.push_back(newBook);

            // 将书籍信息保存到文件
            std::ofstream outFile("books.txt", std::ios::app);
            if (outFile.is_open()) {
                outFile << newBook.id << '\n' << newBook.title << '\n'
                    << newBook.author << '\n' << newBook.category << '\n'
                    << newBook.publisher << '\n' << newBook.publishDate << '\n'
                    << newBook.price << std::endl;
                outFile.close();
            }
            else {
                std::cout << "无法打开文件 books.txt" << std::endl;
            }
        }
        std::cout << "\n书籍信息录入成功" << std::endl; // 添加提示信息
        system("pause");
        break;
    }
    case 3:  // 返回上一级菜单
        break;
    default:
        std::cout << "选择无效" << std::endl;
        break;
    }
}
//书籍浏览函数
void listBooks(const std::vector<Book>& books) {
    clearScreen();

    if (books.empty()) {
        std::cout << "================================================" << std::endl;
        std::cout << "======== 艺何浪图书信息管理系统 ========" << std::endl;
        std::cout << "================================================\n" << std::endl;
        std::cout << "数据库内还没有录入数据,请先进行录入!\n" << std::endl;
        system("pause");
        std::cin.get(); // 等待用户输入
    }
    else {


        std::cout << "================================================" << std::endl;
        std::cout << "======== 艺何浪图书信息管理系统 ========" << std::endl;
        std::cout << "================================================\n" << std::endl;
        std::cout << "\t请选择浏览方式:" << std::endl;
        std::cout << "\t1. 直接显示所有数据" << std::endl;
        std::cout << "\t2. 逐个浏览" << std::endl;
        std::cout << "\t3. 返回上一级\n" << std::endl;
        std::cout << "请输入选择: ";

        int choice;
        std::cin >> choice;

        // 清除输入缓冲区中的换行符
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

        switch (choice) {
        case 1:
            clearScreen();
            std::cout << "================================================" << std::endl;
            std::cout << "======== 艺何浪图书信息管理系统 ========" << std::endl;
            std::cout << "================================================\n" << std::endl;
            for (const auto& book : books) {
                std::cout << "\t书籍编号: " << book.id << std::endl;
                std::cout << "\t书籍名称: " << book.title << std::endl;
                std::cout << "\t作者姓名: " << book.author << std::endl;
                std::cout << "\t图书分类: " << book.category << std::endl;
                std::cout << "\t出版单位: " << book.publisher << std::endl;
                std::cout << "\t出版时间: " << book.publishDate << std::endl;
                std::cout << "\t书籍价格: " << book.price << std::endl;
                std::cout << std::endl;
            }
            std::cout << "按回车键继续...";
            std::cin.get();
            break;
        case 2:
            clearScreen();
            for (const auto& book : books) {
                std::cout << "================================================" << std::endl;
                std::cout << "======== 艺何浪图书信息管理系统 ========" << std::endl;
                std::cout << "================================================\n" << std::endl;
                std::cout << "\t书籍编号: " << book.id << std::endl;
                std::cout << "\t书籍名称: " << book.title << std::endl;
                std::cout << "\t作者姓名: " << book.author << std::endl;
                std::cout << "\t图书分类: " << book.category << std::endl;
                std::cout << "\t出版单位: " << book.publisher << std::endl;
                std::cout << "\t出版时间: " << book.publishDate << std::endl;
                std::cout << "\t书籍价格: " << book.price << std::endl;
                std::cout << std::endl;

                std::cout << "\n按回车键继续浏览下一本书籍...";
                if (std::cin.get() == '\n') {
                    clearScreen();
                }
            }
            break;
        case 3:  // 返回上一级选项 
            break;
        default:
            std::cout << "选择无效,请重新选择。" << std::endl;
            break;
        }
    }
}

//书籍查询函数
void searchBooks(const std::vector<Book>& books) {
    while (true) {
        clearScreen();
        int choice;
        std::string keyword;
        int priceMin, priceMax;
        bool found;
        std::cout << "================================================" << std::endl;
        std::cout << "======== 艺何浪图书信息管理系统 ========" << std::endl;
        std::cout << "================================================\n" << std::endl;
        std::cout << "\t请选择查询方式:" << std::endl;
        std::cout << "\t1. 书籍编号" << std::endl;
        std::cout << "\t2. 书籍名称" << std::endl;
        std::cout << "\t3. 作者姓名" << std::endl;
        std::cout << "\t4. 图书分类" << std::endl;
        std::cout << "\t5. 出版单位" << std::endl;
        std::cout << "\t6. 出版时间" << std::endl;
        std::cout << "\t7. 书籍价格区间" << std::endl;
        std::cout << "\t8. 返回上一级\n" << std::endl;
        std::cout << "\t请选择查询方式: ";
        std::cin >> choice;

        found = false; // 每次查询前重置found标志

        switch (choice) {
        case 1:
            clearScreen();
            std::cout << "================================================" << std::endl;
            std::cout << "======== 艺何浪图书信息管理系统 ========" << std::endl;
            std::cout << "================================================\n" << std::endl;
            std::cout << "\t请输入要查询的书籍编号: ";
            std::cin >> keyword;
            clearScreen();
            std::cout << "================================================" << std::endl;
            std::cout << "======== 艺何浪图书信息管理系统 ========" << std::endl;
            std::cout << "================================================\n" << std::endl;
            for (const auto& book : books) {
                if (book.id == keyword) {
                    found = true; // 标记为找到了匹配的书籍
                    std::cout << "\t书籍编号: " << book.id << std::endl;
                    std::cout << "\t书籍名称: " << book.title << std::endl;
                    std::cout << "\t作者姓名: " << book.author << std::endl;
                    std::cout << "\t图书分类: " << book.category << std::endl;
                    std::cout << "\t出版单位: " << book.publisher << std::endl;
                    std::cout << "\t出版时间: " << book.publishDate << std::endl;
                    std::cout << "\t书籍价格: " << book.price << std::endl;
                }
            }
            if (!found) {
                std::cout << "未能查询到您想查询的信息" << std::endl;
            }
            break;
        case 2:
            clearScreen();
            std::cout << "================================================" << std::endl;
            std::cout << "======== 艺何浪图书信息管理系统 ========" << std::endl;
            std::cout << "================================================\n" << std::endl;
            std::cout << "\t请输入要查询的书籍名称: ";
            std::cin >> keyword;
            clearScreen();
            std::cout << "================================================" << std::endl;
            std::cout << "======== 艺何浪图书信息管理系统 ========" << std::endl;
            std::cout << "================================================\n" << std::endl;
            for (const auto& book : books) {
                if (book.title == keyword) {
                    found = true; // 标记为找到了匹配的书籍
                    std::cout << "\t书籍编号: " << book.id << std::endl;
                    std::cout << "\t书籍名称: " << book.title << std::endl;
                    std::cout << "\t作者姓名: " << book.author << std::endl;
                    std::cout << "\t图书分类: " << book.category << std::endl;
                    std::cout << "\t出版单位: " << book.publisher << std::endl;
                    std::cout << "\t出版时间: " << book.publishDate << std::endl;
                    std::cout << "\t书籍价格: " << book.price << std::endl;
                }
            }
            if (!found) {
                std::cout << "未能查询到您想查询的信息!" << std::endl;
            }
            break;
        case 3:
            clearScreen();
            std::cout << "================================================" << std::endl;
            std::cout << "======== 艺何浪图书信息管理系统 ========" << std::endl;
            std::cout << "================================================\n" << std::endl;
            std::cout << "\t请输入要查询的作者姓名: ";
            std::cin >> keyword;
            clearScreen();
            std::cout << "================================================" << std::endl;
            std::cout << "======== 艺何浪图书信息管理系统 ========" << std::endl;
            std::cout << "================================================\n" << std::endl;
            for (const auto& book : books) {
                if (book.author == keyword) {
                    found = true; // 标记为找到了匹配的书籍
                    std::cout << "\t书籍编号: " << book.id << std::endl;
                    std::cout << "\t书籍名称: " << book.title << std::endl;
                    std::cout << "\t作者姓名: " << book.author << std::endl;
                    std::cout << "\t图书分类: " << book.category << std::endl;
                    std::cout << "\t出版单位: " << book.publisher << std::endl;
                    std::cout << "\t出版时间: " << book.publishDate << std::endl;
                    std::cout << "\t书籍价格: " << book.price << std::endl;
                }
            }
            if (!found) {
                std::cout << "未能查询到您想查询的信息!" << std::endl;
            }
            break;
        case 4:
            clearScreen();
            std::cout << "================================================" << std::endl;
            std::cout << "======== 艺何浪图书信息管理系统 ========" << std::endl;
            std::cout << "================================================\n" << std::endl;
            std::cout << "\t请输入要查询的图书分类: ";
            std::cin >> keyword;
            clearScreen();
            std::cout << "================================================" << std::endl;
            std::cout << "======== 艺何浪图书信息管理系统 ========" << std::endl;
            std::cout << "================================================\n" << std::endl;
            for (const auto& book : books) {
                if (book.category == keyword) {
                    found = true; // 标记为找到了匹配的书籍
                    std::cout << "\t书籍编号: " << book.id << std::endl;
                    std::cout << "\t书籍名称: " << book.title << std::endl;
                    std::cout << "\t作者姓名: " << book.author << std::endl;
                    std::cout << "\t图书分类: " << book.category << std::endl;
                    std::cout << "\t出版单位: " << book.publisher << std::endl;
                    std::cout << "\t出版时间: " << book.publishDate << std::endl;
                    std::cout << "\t书籍价格: " << book.price << std::endl;
                }
            }
            if (!found) {
                std::cout << "未能查询到您想查询的信息!" << std::endl;
            }
            break;
        case 5:
            clearScreen();
            std::cout << "================================================" << std::endl;
            std::cout << "======== 艺何浪图书信息管理系统 ========" << std::endl;
            std::cout << "================================================\n" << std::endl;
            std::cout << "\t请输入要查询的出版单位: ";
            std::cin >> keyword;
            clearScreen();
            std::cout << "================================================" << std::endl;
            std::cout << "======== 艺何浪图书信息管理系统 ========" << std::endl;
            std::cout << "================================================\n" << std::endl;
            for (const auto& book : books) {
                if (book.publisher == keyword) {
                    found = true; // 标记为找到了匹配的书籍
                    std::cout << "\t书籍编号: " << book.id << std::endl;
                    std::cout << "\t书籍名称: " << book.title << std::endl;
                    std::cout << "\t作者姓名: " << book.author << std::endl;
                    std::cout << "\t图书分类: " << book.category << std::endl;
                    std::cout << "\t出版单位: " << book.publisher << std::endl;
                    std::cout << "\t出版时间: " << book.publishDate << std::endl;
                    std::cout << "\t书籍价格: " << book.price << std::endl;
                }
            }
            if (!found) {
                std::cout << "未能查询到您想查询的信息!" << std::endl;
            }
            break;
        case 6:
            clearScreen();
            std::cout << "================================================" << std::endl;
            std::cout << "======== 艺何浪图书信息管理系统 ========" << std::endl;
            std::cout << "================================================\n" << std::endl;
            std::cout << "\t请输入要查询的出版时间: ";
            std::cin >> keyword;
            clearScreen();
            std::cout << "================================================" << std::endl;
            std::cout << "======== 艺何浪图书信息管理系统 ========" << std::endl;
            std::cout << "================================================\n" << std::endl;
            for (const auto& book : books) {
                if (book.publishDate == keyword) {
                    found = true; // 标记为找到了匹配的书籍
                    std::cout << "\t书籍编号: " << book.id << std::endl;
                    std::cout << "\t书籍名称: " << book.title << std::endl;
                    std::cout << "\t作者姓名: " << book.author << std::endl;
                    std::cout << "\t图书分类: " << book.category << std::endl;
                    std::cout << "\t出版单位: " << book.publisher << std::endl;
                    std::cout << "\t出版时间: " << book.publishDate << std::endl;
                    std::cout << "\t书籍价格: " << book.price << std::endl;
                }
            }
            if (!found) {
                std::cout << "未能查询到您想查询的信息!" << std::endl;
            }
            break;
        case 7:
            clearScreen();
            std::cout << "================================================" << std::endl;
            std::cout << "======== 艺何浪图书信息管理系统 ========" << std::endl;
            std::cout << "================================================\n" << std::endl;
            std::cout << "\t请输入要查询的书籍价格区间(最小价格 最大价格): ";
            std::cin >> priceMin >> priceMax;
            clearScreen();
            std::cout << "================================================" << std::endl;
            std::cout << "======== 艺何浪图书信息管理系统 ========" << std::endl;
            std::cout << "================================================\n" << std::endl;
            for (const auto& book : books) {
                if (book.price >= priceMin && book.price <= priceMax) {
                    found = true; // 标记为找到了匹配的书籍
                    std::cout << "\t书籍编号: " << book.id << std::endl;
                    std::cout << "\t书籍名称: " << book.title << std::endl;
                    std::cout << "\t作者姓名: " << book.author << std::endl;
                    std::cout << "\t图书分类: " << book.category << std::endl;
                    std::cout << "\t出版单位: " << book.publisher << std::endl;
                    std::cout << "\t出版时间: " << book.publishDate << std::endl;
                    std::cout << "\t书籍价格: " << book.price << std::endl;
                }
            }
            if (!found) {
                std::cout << "未能查询到您想查询的信息!" << std::endl;
            }
            break;
        case 8:
            return;
        default:
            std::cout << "选择无效" << std::endl;
            break;
        }
        int operationChoice;
        while (true) {
            std::cout << "\n\t请选择操作:" << std::endl;
            std::cout << "\t1. 继续查询" << std::endl;
            std::cout << "\t2. 结束查询" << std::endl;
            std::cout << "\n请选择操作: ";
            std::cin >> operationChoice;

            if (std::cin.fail() || (operationChoice != 1 && operationChoice != 2)) {
                std::cin.clear(); // 清除输入流的错误标志
                std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // 忽略错误输入之后的字符直到遇到换行符
                std::cout << "无效输入,请重新选择" << std::endl;
            }
            else {
                break; // 如果输入是有效的(1或2), 退出循环
            }
        }
        if (operationChoice == 2) {
            break;
        }
    }
    std::cout << "\n按回车键继续...";
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    std::cin.get();
}


// 删除图书信息
void deleteBook(std::vector<Book>& books) {
    clearScreen();
    std::cout << "================================================" << std::endl;
    std::cout << "======== 艺何浪图书信息管理系统 ========" << std::endl;
    std::cout << "================================================\n" << std::endl;
    std::cout << "\t请选择你要进行的操作:" << std::endl;
    std::cout << "\t1. 删除单一数据" << std::endl;
    std::cout << "\t2. 删除全部数据" << std::endl;
    std::cout << "\t3. 回到上一级" << std::endl;

    int choice;
    std::cin >> choice;

    if (choice == 1) {
        deleteSingleBook(books);
    }
    else if (choice == 2) {
        deleteAllBooks(books);
    }
    else {
        // 回到上一级或其他处理
    }
    // 将书籍信息保存到文件
    std::ofstream outFile("books.txt", std::ios::trunc);
    if (outFile.is_open()) {
        for (const auto& book : books) {
            outFile << book.id << '\n' << book.title << '\n'
                << book.author << '\n' << book.category << '\n'
                << book.publisher << '\n' << book.publishDate << '\n'
                << book.price << std::endl;
        }
        outFile.close();
    }
    else {
        std::cout << "\n\t无法打开文件 books.txt" << std::endl;
    }
}
// 删除单一数据
void deleteSingleBook(std::vector<Book>& books) {
    while (true) {
        std::string input;
        clearScreen();
        std::cout << "================================================" << std::endl;
        std::cout << "======== 艺何浪图书信息管理系统 ========" << std::endl;
        std::cout << "================================================\n" << std::endl;
        std::cout << "请选择你要删除的数据(输入书籍编号或书籍名称): ";
        std::cin >> input;

        auto it = std::remove_if(books.begin(), books.end(), [&input](const Book& book) {
            return book.id == input || book.title == input;
            });

        if (it != books.end()) {
            books.erase(it, books.end());  // 删除被标记为删除的图书记录
            std::cout << "\n\t数据删除成功\n" << std::endl;
        }
        else {
            std::cout << "\n\t不存在该书籍" << std::endl;
        }

        std::cout << "请选择:" << std::endl;
        std::cout << "\t1. 继续删除" << std::endl;
        std::cout << "\t2. 返回上一级" << std::endl;

        int subChoice;
        std::cin >> subChoice;
        if (subChoice != 1) {
            break;
        }
    }
}

// 删除全部数据
void deleteAllBooks(std::vector<Book>& books) {
    char confirmation;
    std::cout << "\n\t此操作会完全删除所储存的图书信息,是否执行此项?(y/n): ";
    std::cin >> confirmation;

    if (confirmation == 'y' || confirmation == 'Y') {
        books.clear();
        std::ofstream outFile("books.txt", std::ios::trunc);
        if (outFile.is_open()) {
            outFile.close();
            std::cout << "\n\t图书信息已全部删除" << std::endl;
            std::cout << "\n\t数据已全部清空\n" << std::endl;
            system("pause");
        }
        else {
            std::cout << "无法打开文件 books.txt" << std::endl;
        }
    }
    else {
        std::cout << "\n\t已取消删除操作\n" << std::endl;
        system("pause");
    }
}

void modifyBook(std::vector<Book>& books) {
    clearScreen();
    std::cout << "================================================" << std::endl;
    std::cout << "======== 艺何浪图书信息管理系统 ========" << std::endl;
    std::cout << "================================================\n" << std::endl;
    std::string input;
    std::cout << "\t请输入要修改的图书编号或书籍名称: ";
    std::cin >> input;

    auto it = std::find_if(books.begin(), books.end(), [&input](const Book& book) {
        return book.id == input || book.title == input;
        });

    if (it != books.end()) {
        Book& selectedBook = *it;
        clearScreen();
        std::cout << "================================================" << std::endl;
        std::cout << "======== 艺何浪图书信息管理系统 ========" << std::endl;
        std::cout << "================================================\n" << std::endl;

        std::cout << "请选择修改选项:" << std::endl;
        std::cout << "\t1. 单项修改" << std::endl;
        std::cout << "\t2. 全部修改" << std::endl;
        int modChoice;
        std::cin >> modChoice;
        bool success = false;

        if (modChoice == 1) {
            clearScreen();
            std::cout << "================================================" << std::endl;
            std::cout << "======== 艺何浪图书信息管理系统 ========" << std::endl;
            std::cout << "================================================\n" << std::endl;
            std::cout << "\t1. 书籍编号" << std::endl;
            std::cout << "\t2. 书籍名称" << std::endl;
            std::cout << "\t3. 作者姓名" << std::endl;
            std::cout << "\t4. 图书分类" << std::endl;
            std::cout << "\t5. 出版单位" << std::endl;
            std::cout << "\t6. 出版时间" << std::endl;
            std::cout << "\t7. 书籍价格\n" << std::endl;
            std::cout << "请选择您要修改的内容:" << std::endl;

            int choice;
            std::cin >> choice;

            clearScreen();
            std::cout << "================================================" << std::endl;
            std::cout << "======== 艺何浪图书信息管理系统 ========" << std::endl;
            std::cout << "================================================\n" << std::endl;
            switch (choice) {
            case 1:
                std::cout << "请输入新的书籍编号: ";
                std::cin >> selectedBook.id;
                success = true;
                break;
            case 2:
                std::cout << "请输入新的书籍名称: ";
                std::cin >> selectedBook.title;
                success = true;
                break;
            case 3:
                std::cout << "请输入新的作者姓名: ";
                std::cin >> selectedBook.author;
                success = true;
                break;
            case 4:
                std::cout << "请输入新的图书分类: ";
                std::cin >> selectedBook.category;
                success = true;
                break;
            case 5:
                std::cout << "请输入新的出版单位: ";
                std::cin >> selectedBook.publisher;
                success = true;
                break;
            case 6:
                std::cout << "请输入新的出版时间: ";
                std::cin >> selectedBook.publishDate;
                success = true;
                break;
            case 7:
                std::cout << "请输入新的书籍价格: ";
                std::cin >> selectedBook.price;
                success = true;
                break;
            default:
                std::cout << "无效选择" << std::endl;
                break;
            }
        }
        else if (modChoice == 2) {
            clearScreen();
            std::cout << "================================================" << std::endl;
            std::cout << "======== 艺何浪图书信息管理系统 ========" << std::endl;
            std::cout << "================================================\n" << std::endl;
            std::cout << "\t请输入新的书籍编号: ";
            std::cin >> selectedBook.id;
            std::cout << "\t请输入新的书籍名称: ";
            std::cin >> selectedBook.title;
            std::cout << "\t请输入新的作者姓名: ";
            std::cin >> selectedBook.author;
            std::cout << "\t请输入新的图书分类: ";
            std::cin >> selectedBook.category;
            std::cout << "\t请输入新的出版单位: ";
            std::cin >> selectedBook.publisher;
            std::cout << "\t请输入新的出版时间: ";
            std::cin >> selectedBook.publishDate;
            std::cout << "\t请输入新的书籍价格: ";
            std::cin >> selectedBook.price;
            success = true;
        }
        else {
            std::cout << "无效选择" << std::endl;
        }

        if (success) {
            std::cout << "\n\t修改成功\n" << std::endl;
            system("pause");
        }

    }
    else {
        std::cout << "\n\t未找到对应图书信息\n" << std::endl;
        system("pause");
    }

    // 将书籍信息保存到文件
    std::ofstream outFile("books.txt", std::ios::trunc);
    if (outFile.is_open()) {
        for (const auto& book : books) {
            outFile << book.id << '\n' << book.title << '\n'
                << book.author << '\n' << book.category << '\n'
                << book.publisher << '\n' << book.publishDate << '\n'
                << book.price << std::endl;
        }
        outFile.close();
    }
    else {
        std::cout << "无法打开文件 books.txt" << std::endl;
    }
}

int main() {
    if (!login()) {
        std::cout << "登录失败!" << std::endl;
        return 1;
    }
    Welcome();

    std::vector<Book> books;

    std::ifstream inFile("books.txt");
    if (inFile.is_open()) {
        Book tempBook;
        while (inFile >> tempBook.id >> tempBook.title >> tempBook.author >> tempBook.category >> tempBook.publisher >> tempBook.publishDate >> tempBook.price) {
            books.push_back(tempBook);
        }
        inFile.close();
    }
    else {
        std::cout << "首次运行系统或无法打开文件books.txt,将开始新的记录。" << std::endl;
    }

    int choice = 0;
    do {
        clearScreen();
        printMenu();
        std::cin >> choice;
        switch (choice) {
        case 1:
            addBook(books);
            break;
        case 2:
            listBooks(books);
            break;
        case 3:
            searchBooks(books);
            break;
        case 4:
            deleteBook(books);
            break;
        case 5:
            modifyBook(books);
            break;
        case 6:
            std::cout << "退出系统。" << std::endl;
            break;
        default:
            std::cout << "未知选项,请重新输入。" << std::endl;
        }
    } while (choice != 6);

    return 0;
}

  • 12
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值