图书管理系统增量开发

此为软件开发与创新设计的作业,要求如下:


找一个已有的软件项目(同学、高年级同学、网络下载),软件项目不必过大,可以实现某一部分应用功能即可,不能只是一段代码。阅读分析,找出软件尚存的缺陷, 改进其软件做二次开发,并将这个过程整理成一份博客,于2023年3月9日下午2点45分之前上传到自己的博客园地址。 


 

一、源程序运行及功能

 

因为本身编程就很菜,所以我找了一份简单的C++图书购买系统代码,准备对此进行增量开发、修改一些bug。下为源代码:

#include<iostream>
#include<string>
#include<fstream>
using namespace std;
class user; class common; class vip; class honored;
class book
{
protected:
    string book_name;
    string author;
    string publisher;
    double price;
    book* next;
public:
    book() :next(NULL) {};
    void addbook(book*); //添加书籍
    void showbook(book*); //展示书籍
    void deletbook(book*); //删除书籍
    void loadbook(book*); 
    void savebook(book*);  
    friend user;
};
void book::addbook(book* p)
{
    book* p1 = p;
    while (p->next != NULL)
    {
        p = p->next;
    }
    book* p2 = new book;
    cout << "请输入书名:" << endl;
    cin >> p2->book_name;
  cout << "请输入作者:" << endl;
    cin >> p2->author;
    cout << "请输入出版社:" << endl;
    cin >> p2->publisher;
    cout << "请输入价格:" << endl;
    cin >> p2->price;
    if (cin.fail()) {
        cin.clear();
        cin.ignore(1000, '\n');
        system("cls");
        cout << "无效输入!" << endl;
        return;
    }
    p->next = p2;
    system("cls");
    cout << "添加成功!" << endl;
}
void book::showbook(book* p)
{
    if (p->next == NULL)
    {
        cout << "目前无书籍信息!\t\t" << endl;
        return;
    }
    p = p->next;
    int n = 1;
    while (1)
    {
        cout << "NO." << n << ":" << '\t' << endl;
        cout << "书名:" << "" << p->book_name << "" << '\t';
        cout << "作者:" << p->author << '\t';
        cout << "出版社:" << p->publisher << '\t';
        cout << "价格:" << p->price << '\n';
        if (p->next == NULL)break;
        p = p->next;
        n++;
    }
}
void book::deletbook(book* p)
{
    if (p->next == NULL)
    {
        cout << "目前无书籍信息!\t\t" << endl;
        return;
    }
    string na; cout << "请输入要删除的书名:" << endl; cin >> na;
    while (1)
    {
        if (p->next->book_name != na)
        {
            p = p->next;
            if (p->next == NULL)
            {
                system("cls");
                cout << "没有这本书的信息!" << endl << endl;
                break;
            }
        }
        else
        {
            cout << "找到了!" << endl;
            cout << "—————————————————————————————————————————————————————————\n";
            cout << "书名:" << "" << p->next->book_name << "" << '\t';
            cout << "作者:" << p->next->author << '\t';
            cout << "出版社:" << p->next->publisher << '\t';
            cout << "价格:" << p->next->price << '\n';
            cout << "—————————————————————————————————————————————————————————\n\n";
            cout << "是否删除该书籍信息?" << endl;
            cout << "1) 是\t0)否" << endl;
            int a; cin >> a;
            switch (a)
            {
            case 0:
            {    break; }
            case 1:
            {
                p->next = p->next->next;
                system("cls");
                cout << "删除成功!" << endl;
                break;
            }
            default:
                cout << "ERROR!" << endl;
            }
            break;
        }
    }
}
void book::loadbook(book* p)
{
    ifstream fin;
    fin.open("book.txt", ios::in);
    if (!fin)
    {
        cout << "文件打开失败!" << endl;
        exit(1);
    }
    string name;
    while (fin >> name)
    {
        book* newnode = new book;
        newnode->book_name = name;
        fin >> newnode->author;
        fin >> newnode->publisher;
        fin >> newnode->price;
        p->next = newnode;
        p = p->next;
    }
    fin.close();
}
void book::savebook(book* p)
{
    ofstream fout;
    fout.open("book.txt", ios::out);
    if (!fout)
    {
        cout << "文件打开失败!" << endl;
        exit(1);
    }
    while (p->next != NULL)
    {
        fout << p->next->book_name << "\t";
        fout << p->next->author << "\t";
        fout << p->next->publisher << "\t";
        fout << p->next->price << "\n";
        p = p->next;
    }
    fout.close();
}
class user
{
protected:
    string ID;          //用户ID
    string key;              //用户密码
    string adress;             //用户地址
    double pay;
    string status;           //用户身份
    book* list;                //购物车
public:
    user(string, string, string);
    string getID() { return ID; }  //取用户ID
    string getadress() { return adress; }//取用户地址
    string getkey() { return key; }  //取用户密码
    void main_user();       //用户界面
    void main_shop_list(); //购物车管理界面
    void showbook();         //显示书籍信息
    void addbook(book*);        //向购物车中添加书籍
    void deletebook(book*);       //从购物车中删除书籍
    void load(book*);       //从文件读入购物车信息
    void save(book*);         //向文件输出购物车信息
    int signin(string i, string k)              //用户登录函数
    {
        if (i == ID && k == key) return 1;
        else return 0;
    }
    void showbuyer();  //显示用户信息
    void sum(book*);  //购物车结算函数
};
user::user(string I, string k, string ad)
{
    ID = I; key = k; adress = ad; status = "user";
    list = new book();
}
void user::addbook(book* p)
{
    book* p1 = new book;
    p1->loadbook(p1);
    p1->showbook(p1);
    if (p1->next == NULL)
    {
        cout << "购物车当前为空!" << endl;
        return;
    }
    cout << endl << "请输入要添加的书籍名称:" << endl;
    string name; cin >> name;
    while (p1->next != NULL)
    {
        if (p1->next->book_name == name)
        {
            cout << "找到了!" << endl;
            cout << "—————————————————————————————————————————————————————————\n";
            cout << "书名:" << "" << p1->next->book_name << "" << '\t';
            cout << "作者:" << p1->next->author << '\t';
            cout << "出版社:" << p1->next->publisher << '\t';
            cout << "价格:" << p1->next->price << '\n';
            cout << "—————————————————————————————————————————————————————————\n\n";
            cout << "是否将此书加入购物车?" << endl;
            cout << "1) 是\t0)否" << endl;
            int o; cin >> o;
            if (o == 1)
            {
                while (p->next != NULL)
                    p = p->next;
                p->next = p1->next;
                p1->next->next = NULL;
                system("cls");
                cout << "添加成功!" << endl;
                break;
            }
            else if (o == 0)
            {
                break;
            }
            else
            {
                cout << "请输入正确指令!" << endl;
                break;
            }
        }
        p1 = p1->next;
    }
    if (p1->next == NULL)
    {
        cout << "未找到符合条件的书籍!" << endl;
    }
}
void user::deletebook(book* p)
{
    if (p->next == NULL)
    {
        cout << "购物车当前为空!" << endl;
        return;
    }
    cout << endl << "请输入要删除的书籍名称:" << endl;
    string name; cin >> name;
    int i = 0;
    while (p->next != NULL)
    {
        if (p->next->book_name == name)
        {
            i++;
            cout << "找到了!" << endl;
            cout << "—————————————————————————————————————————————————————————\n";
            cout << "书名:" << "" << p->next->book_name << "" << '\t';
            cout << "作者:" << p->next->author << '\t';
            cout << "出版社:" << p->next->publisher << '\t';
            cout << "价格:" << p->next->price << '\n';
            cout << "—————————————————————————————————————————————————————————\n\n";
            cout << "是否将此书从购物车中移除?" << endl;
            cout << "1) 是\t0)否" << endl;
            int o; cin >> o;
            if (o == 1)
            {
                p->next = p->next->next;
                system("cls");
                cout << "删除成功!" << endl;
                break;
            }
            else if (o == 0)
            {
                break;
            }
            else
            {
                cout << "请输入正确指令!" << endl;
                break;
            }
        }
        p = p->next;
    }
    if (i == 0)
    {
        cout << "未找到符合条件的书籍!" << endl;
    }
}
void user::load(book* p)
{
    if (p->next != NULL)p->next = NULL;
    string fname = this->ID + ".txt";
    ifstream fin;
    fin.open(fname, ios::in);
    if (!fin)
    {
        cout << "文件打开失败!" << endl;
        exit(1);
    }
    string name;
    book* p1 = new book;
    book* p2 = p1;
    list->loadbook(p1);
    while (fin >> name)
    {
        book* newnode = new book;
        newnode->book_name = name;
        fin >> newnode->author;
        fin >> newnode->publisher;
        fin >> newnode->price;
        while (p1->next != NULL)
        {
            if (p1->next->book_name == newnode->book_name)
            {
                newnode->author = p1->next->author;
                newnode->publisher = p1->next->publisher;
                newnode->price = p1->next->price;
                break;
            }
            else    p1 = p1->next;
        }
        if (p1->next != NULL)
        {
            p->next = newnode;
            p = p->next;
            p1 = p2;
        }
    }
    fin.close();
}
void user::save(book* p)
{
    string fname = this->ID + ".txt";
    ofstream fout;
    fout.open(fname, ios::out);
    if (!fout)
    {
        cout << "文件打开失败!" << endl;
        exit(1);
    }
    while (p->next != NULL)
    {
        fout << p->next->book_name << "\t";
        fout << p->next->author << "\t";
        fout << p->next->publisher << "\t";
        fout << p->next->price << "\n";
        p = p->next;
    }
    fout.close();
}
void user::main_user()
{
    int o;
    while (1)
    {
        system("cls");
        printf("\n\n\n");
        printf("\t\t\t\t\t+------------------------------+\n");
        printf("\t\t\t\t\t|--------------用户-----------|\n");
        printf("\t\t\t\t\t+-------------------------------+\n");
        printf("\t\t\t\t\t\t1 查看书籍\n");
        printf("\t\t\t\t\t\t2 管理购物车\n");
        printf("\t\t\t\t\t\t3 查看个人信息\n");
        printf("\t\t\t\t\t\t4 登出\n");
        printf("\n\n\n");
        printf("\t\t\t\t\t请输入你想实现的功能前的代号:\n");
        printf("\t\t\t\t");
        cin >> o;
        switch (o)
        {
        case 1:
        {
            system("cls");
            showbook();
            break;
        }
        case 2:
        {
            main_shop_list();
            break;
        }
        case 3:
        {
            showbuyer();
            break;
        }
        case 4:
        {return;    }
        default:
        {cout << "请输入正确指令!" << endl << endl; }
        }
        system("pause");
    }
}
void user::main_shop_list()
{
    this->load(list);
    while (1)
    {
        system("cls");
        cout << this->getID() << "的购物车:" << endl << endl;
        list->showbook(list);
        printf("\n\n\n");
        printf("\t\t\t\t\t+-------------DoKiDoKi----------+\n");
        printf("\t\t\t\t\t|--------------购物车-----------|\n");
        printf("\t\t\t\t\t+-------------------------------+\n");
        printf("\t\t\t\t\t\t1 向购物车中添加书籍\n");
        printf("\t\t\t\t\t\t2 从购物车中删除书籍\n");
        printf("\t\t\t\t\t\t3 结算\n");
        printf("\t\t\t\t\t\t4 退出购物车\n");
        printf("\n\n\n");
        printf("\t\t\t\t\t请输入你想实现的功能前的代号:\n");
        printf("\t\t\t\t");
        int o;
        cin >> o;
        switch (o)
        {
        case 1:
        {
            addbook(list);
            break;
        }
        case 2:
        {
            deletebook(list);
            break;
        }
        case 3:
        {
            sum(list);
            break;
        }
        case 4:
        {
            save(list);
            return;
        }
        default:
        {cout << "指令错误!" << endl; }
        }
        system("pause");
    }
}
void user::showbook()
{
    ifstream fin;
    fin.open("book.txt", ios::in);
    if (!fin)
    {
        cout << "文件打开失败!" << endl;
        exit(1);
    }
    int i = 0;
    while (fin >> list->book_name)
    {
        cout << "NO." << ++i << endl;
        fin >> list->author;
        fin >> list->publisher;
        fin >> list->price;
        cout << "书名:" << "" << list->book_name << "" << '\t';
        cout << "作者:" << list->author << '\t';
        cout << "出版社:" << list->publisher << '\t';
        cout << "价格:" << list->price << endl;
    }
    fin.close();
}
void user::showbuyer()
{
    cout << "用户名:" << getID() << endl;
    cout << "用户地址:" << getadress() << endl;
    cout << "用户身份:" << status << endl;
}
void user::sum(book* p)
{
    double pay = 0;
    while (p->next != NULL)
    {
        pay += p->next->price;
        p = p->next;
    }
    cout << "用户 " << this->getID() << " 应付" << pay << "" << endl;
}
class admin    //管理员类
{
private:
    string ID;
    string key;
    string status;
    book* list;
public:
    string getID() { return ID; }  //取管理员ID
    string getkey() { return key; }  //取管理员密码
    admin(string i, string k)
    {
        ID = i; key = k; status = "ad"; list = new book;
    }
    int signin()
    {
        string i, k;
        cout << "请输入账号:";
        cin >> i;
        cout << "请输入密码:";
        cin >> k;
        if (i == ID && k == key)    return 1;
        else    return 0;
    }
    void main_ad();
};
void admin::main_ad()
{

    list->loadbook(list);
    while (1)
    {
        system("cls");
        printf("\n\n\n");
        printf("\t\t\t\t\t+------------------------------+\n");
        printf("\t\t\t\t\t|--------------管理员-----------|\n");
        printf("\t\t\t\t\t+-------------------------------+\n");
        printf("\t\t\t\t\t\t1 查看书籍\n");
        printf("\t\t\t\t\t\t2 添加书籍\n");
        printf("\t\t\t\t\t\t3 删除书籍\n");
        printf("\t\t\t\t\t\t4 保存登出\n");
        printf("\n\n\n");
        printf("\t\t\t\t\t请输入你想实现的功能前的代号:\n");
        printf("\t\t\t\t");
        int o;
        cin >> o;
        switch (o)
        {
        case 1:
        {system("cls");
        list->showbook(list);
        break;
        }
        case 2:
        {
            list->addbook(list);
            break;
        }
        case 3:
        {
            list->deletbook(list);
            break;
        }
  case 4:
        {
            list->savebook(list);
            return;
        }
        default:
        {    cout << "请输入正确指令!" << endl << endl; }
        }
        system("pause");
    }
}
int main()
{
    admin A("admin", "160922");
    user B("me", "19170", "中国某省某市某区");
    while (1)
    {
        system("cls");
        printf("\n\n\n");
        printf("\t\t\t\t\t+-------------------------------+\n");
        printf("\t\t\t\t\t|-----------图书购买系统--------|\n");
        printf("\t\t\t\t\t+-------------------------------+\n");
        printf("\t\t\t\t\t\t1 管理员登录\n");
        printf("\t\t\t\t\t\t2 用户登录\n");
        printf("\t\t\t\t\t\t3 退出\n");
        printf("\n\n\n");
        printf("\t\t\t\t\t请输入你想实现的功能前的代号:\n");
        printf("\t\t\t\t");
        int o;
        cin >> o;
        switch (o)
        {
        case 1:
        {
            if (A.signin() == 1)
            {
                A.main_ad();
            }
            else    cout << "账号或密码错误" << endl;
            break;
        }
        case 2:
        {
            string id, k;
            cout << "请输入账号:";  cin >> id;
            cout << "请输入密码:";  cin >> k;
            if (id == "me" && k == "19170")
            {
                B.main_user();
                break;
            }
            else
                cout << "账号或密码错误" << endl;
            break;
            break;
        }
        case 3:
        {    return 0; }
        default:
        {    cout << "请输入正确指令!" << endl << endl;   }
        }
        system("pause");
    }
}
原版

 

这个程序一共有三个类,分别是book类、admin管理员类和user用户类,并且在主函数中分别实体化了一个管理员和用户对象,

初始化参数分别是("admin", "160922"),("me", "19170", "中国某省某市某区"),admin为管理员,密码是1

可以看到提示文件打开失败,经过我一番调试和分析后发现这个程序是靠txt文本文件读取数据的,一共涉及到两个txt文件,分别是保存书籍数据的book.txt和 ''用户名''.txt,而程序运行并不会自动创建这两个文件,所以会提示文件打不开,解决方法就是手动在文件目录下创建出这两个txt文件,就能够成功运行程序了。

 该程序是一个简单的图书购买系统,用户分为管理员和普通用户,管理员可向书库中添加和删除书籍,书籍信息将保存到book.txt文件中,用户可读取书库文件,将书库中的书加入到自己的购物车中并进行结算,购物车文件名格式为:用户名.txt。

 

管理员界面:

 

用户界面:

用户购物车界面:

 对原程序进行总结,主要功能如下:

  • 不同权限用户登录
  • 添加、删除、查看书籍
  • 将书籍加入购物车/从购物车中删除
  • 购物车结算

 


 

二、增量开发

1.优化代码结构

首先要做的事就是重新整理一下代码结构,把代码份文件编写。原版把代码都写进一个文件里,阅读和修改起来极其麻烦,看得眼花,于是我将其三个类抽取了出来写到单独的文件里。

book类:

book.h:

#pragma once
#include<iostream>
#include<string>
#include<fstream>
#include<stdlib.h>
using namespace std;
class book
{
protected:
    string book_name;
    string author;
    string publisher;
    double price;
    book* next;
public:
    friend class user;
    book() :next(NULL) {};              
    void addbook(book*); //添加书籍
    void showbook(book*); //展示书籍
    void deletbook(book*); //删除书籍
    void loadbook(book*); //加载文件
    void savebook(book*);  //保存数据
    
};

 

#include "book.h"
void book::addbook(book* p)
{
    book* p1 = p;
    while (p->next != NULL)
    {
        p = p->next;
    }
    book* p2 = new book;
    p->next = p2;
    cout << "请输入书名:" << endl;
    cin >> p2->book_name;
cout << "请输入作者:" << endl;
    cin >> p2->author;
    cout << "请输入出版社:" << endl;
    cin >> p2->publisher;
    cout << "请输入价格:" << endl;
    cin >> p2->price;
    system("cls");
    cout << "添加成功!" << endl;
}
void book::showbook(book* p)
{
    if (p->next == NULL)
    {
        cout << "目前无书籍信息!\t\t" << endl;
        return;
    }
    p = p->next;
    int n = 1;
    while (1)
    {
        cout << "NO." << n << ":" << '\t' << endl;
        cout << "书名:" << "" << p->book_name << "" << '\t';
        cout << "作者:" << p->author << '\t';
        cout << "出版社:" << p->publisher << '\t';
        cout << "价格:" << p->price << '\n';
        if (p->next == NULL)break;
        p = p->next;
        n++;
    }
}
void book::deletbook(book* p)
{
    if (p->next == NULL)
    {
        cout << "目前无书籍信息!\t\t" << endl;
        return;
    }
    string na; cout << "请输入要删除的书名:" << endl; cin >> na;
    while (1)
    {
        if (p->next->book_name != na)
        {
            p = p->next;
            if (p->next == NULL)
            {
                system("cls");
                cout << "没有这本书的信息!" << endl << endl;
                break;
            }
        }
        else
        {
            cout << "找到了!" << endl;
            cout << "—————————————————————————————————————————————————————————\n";
            cout << "书名:" << "" << p->next->book_name << "" << '\t';
            cout << "作者:" << p->next->author << '\t';
            cout << "出版社:" << p->next->publisher << '\t';
            cout << "价格:" << p->next->price << '\n';
            cout << "—————————————————————————————————————————————————————————\n\n";
            cout << "是否删除该书籍信息?" << endl;
            cout << "1) 是\t0)否" << endl;
            int a; cin >> a;
            switch (a)
            {
            case 0:
            {    break; }
            case 1:
            {
                p->next = p->next->next;
                system("cls");
                cout << "删除成功!" << endl;
                break;
            }
            default:
                cout << "ERROR!" << endl;
            }
            break;
        }
    }
}
void book::loadbook(book* p)
{
    ifstream fin;
    fin.open("book.txt", ios::in);
    if (!fin)
    {
        cout << "文件打开失败!" << endl;
        exit(1);
    }
    string name;
    while (fin >> name)
    {
        book* newnode = new book;
        newnode->book_name = name;
        fin >> newnode->author;
        fin >> newnode->publisher;
        fin >> newnode->price;
        p->next = newnode;
        p = p->next;
    }
    fin.close();
}
void book::savebook(book* p)
{
    ofstream fout;
    fout.open("book.txt", ios::out);
    if (!fout)
    {
        cout << "文件打开失败!" << endl;
        exit(1);
    }
    while (p->next != NULL)
    {
        fout << p->next->book_name << "\t";
        fout << p->next->author << "\t";
        fout << p->next->publisher << "\t";
        fout << p->next->price << "\n";
        p = p->next;
    }
    fout.close();
}
book.cpp

user类:

user.h:

#pragma once
#include "book.h"
using namespace std;
class user
{
protected:
    string user_name;          //用户名
    string key;              //用户密码
    string adress;             //用户地址
    double pay;
    string status;          //用户身份
    book * list;                //购物车
public:
    user(string, string, string);
    string getID(); 
    string getadress();
    string getkey();
    void main_user();       //用户界面
    void main_shop_list(); //购物车管理界面
    void showbook();         //显示书籍信息
    void addbook(book*);        //向购物车中添加书籍
    void deletebook(book*);       //从购物车中删除书籍
    void load(book*);       //从文件读入购物车信息
    void save(book*);   //保存购物车信息
    void showuser();  //显示用户信息
    void sum(book*);  //购物车结算函数
};

 

#include "user.h"
string user::getID() { return user_name; }  //取用户名
string user::getadress() { return adress; }//取用户地址
string user::getkey() { return key; }  //取用户密码
user::user(string I, string k, string ad)
{
    user_name = I; key = k; adress = ad; status = "user";
    list = new book();
}
void user::addbook(book* p)
{
    book* p1 = new book;
    p1->loadbook(p1);
    p1->showbook(p1);
    if (p1->next == NULL)
    {
        cout << "购物车当前为空!" << endl;
        return;
    }
    cout << endl << "请输入要添加的书籍名称:" << endl;
    string name; cin >> name;
    while (p1->next != NULL)
    {
        if (p1->next->book_name == name)
        {
            cout << "找到了!" << endl;
            cout << "—————————————————————————————————————————————————————————\n";
            cout << "书名:" << "" << p1->next->book_name << "" << '\t';
            cout << "作者:" << p1->next->author << '\t';
            cout << "出版社:" << p1->next->publisher << '\t';
            cout << "价格:" << p1->next->price << '\n';
            cout << "—————————————————————————————————————————————————————————\n\n";
            cout << "是否将此书加入购物车?" << endl;
            cout << "1) 是\t0)否" << endl;
            int o; cin >> o;
            if (o == 1)
            {
                while (p->next != NULL)
                    p = p->next;
                p->next = p1->next;
                p1->next->next = NULL;
                system("cls");
                cout << "添加成功!" << endl;
                break;
            }
            else if (o == 0)
            {
                break;
            }
            else
            {
                cout << "请输入正确指令!" << endl;
                break;
            }
        }
        p1 = p1->next;
    }
    if (p1->next == NULL)
    {
        cout << "未找到符合条件的书籍!" << endl;
    }
}
void user::deletebook(book* p)
{
    if (p->next == NULL)
    {
        cout << "购物车当前为空!" << endl;
        return;
    }
    cout << endl << "请输入要删除的书籍名称:" << endl;
    string name; cin >> name;
    int i = 0;
    while (p->next != NULL)
    {
        if (p->next->book_name == name)
        {
            i++;
            cout << "找到了!" << endl;
            cout << "—————————————————————————————————————————————————————————\n";
            cout << "书名:" << "" << p->next->book_name << "" << '\t';
            cout << "作者:" << p->next->author << '\t';
            cout << "出版社:" << p->next->publisher << '\t';
            cout << "价格:" << p->next->price << '\n';
            cout << "—————————————————————————————————————————————————————————\n\n";
            cout << "是否将此书从购物车中移除?" << endl;
            cout << "1) 是\t0)否" << endl;
            int o; cin >> o;
            if (o == 1)
            {
                p->next = p->next->next;
                system("cls");
                cout << "删除成功!" << endl;
                break;
            }
            else if (o == 0)
            {
                break;
            }
            else
            {
                cout << "请输入正确指令!" << endl;
                break;
            }
        }
        p = p->next;
    }
    if (i == 0)
    {
        cout << "未找到符合条件的书籍!" << endl;
    }
}
void user::load(book* p)
{
    if (p->next != NULL)p->next = NULL;
    string fname = this->user_name + ".txt";
    ifstream fin;
    fin.open(fname, ios::in);
    if (!fin)
    {
        cout << "文件打开失败!" << endl;
        exit(1);
    }
    string name;
    book* p1 = new book;
    book* p2 = p1;
    list->loadbook(p1);
    while (fin >> name)
    {
        book* newnode = new book;
        newnode->book_name = name;
        fin >> newnode->author;
        fin >> newnode->publisher;
        fin >> newnode->price;
        while (p1->next != NULL)
        {
            if (p1->next->book_name == newnode->book_name)
            {
                newnode->author = p1->next->author;
                newnode->publisher = p1->next->publisher;
                newnode->price = p1->next->price;
                break;
            }
            else    p1 = p1->next;
        }
        if (p1->next != NULL)
        {
            p->next = newnode;
            p = p->next;
            p1 = p2;
        }
    }
    fin.close();
}
void user::save(book* p)
{
    string fname = this->user_name + ".txt";
    ofstream fout;
    fout.open(fname, ios::out);
    if (!fout)
    {
        cout << "文件打开失败!" << endl;
        exit(1);
    }
    while (p->next != NULL)
    {
        fout << p->next->book_name << "\t";
        fout << p->next->author << "\t";
        fout << p->next->publisher << "\t";
        fout << p->next->price << "\n";
        p = p->next;
    }
    fout.close();
}
void user::main_user()
{
    int o;
    while (1)
    {
        system("cls");
        printf("\n\n\n");
        printf("\t\t\t\t\t+-------------DoKiDoKi----------+\n");
        printf("\t\t\t\t\t|--------------用户-----------|\n");
        printf("\t\t\t\t\t+-------------------------------+\n");
        printf("\t\t\t\t\t\t1 查看书籍\n");
        printf("\t\t\t\t\t\t2 管理购物车\n");
        printf("\t\t\t\t\t\t3 查看个人信息\n");
        printf("\t\t\t\t\t\t4 登出\n");
        printf("\n\n\n");
        printf("\t\t\t\t\t请输入你想实现的功能前的代号:\n");
        printf("\t\t\t\t");
        cin >> o;
        switch (o)
        {
        case 1:
        {
            system("cls");
            showbook();
            break;
        }
        case 2:
        {
            main_shop_list();
            break;
        }
        case 3:
        {
            showuser();
            break;
        }
        case 4:
        {return;    }
        default:
        {cout << "请输入正确指令!" << endl << endl; }
        }
        system("pause");
    }
}
void user::main_shop_list()
{
    this->load(list);
    while (1)
    {
        system("cls");
        cout << this->getID() << "的购物车:" << endl << endl;
        list->showbook(list);
        printf("\n\n\n");
        printf("\t\t\t\t\t+-------------DoKiDoKi----------+\n");
        printf("\t\t\t\t\t|--------------购物车-----------|\n");
        printf("\t\t\t\t\t+-------------------------------+\n");
        printf("\t\t\t\t\t\t1 向购物车中添加书籍\n");
        printf("\t\t\t\t\t\t2 从购物车中删除书籍\n");
        printf("\t\t\t\t\t\t3 结算\n");
        printf("\t\t\t\t\t\t4 退出购物车\n");
        printf("\n\n\n");
        printf("\t\t\t\t\t请输入你想实现的功能前的代号:\n");
        printf("\t\t\t\t");
        int o;
        cin >> o;
        switch (o)
        {
        case 1:
        {
            addbook(list);
            break;
        }
        case 2:
        {
            deletebook(list);
            break;
        }
        case 3:
        {
            sum(list);
            break;
        }
        case 4:
        {
            save(list);
            return;
        }
        default:
        {cout << "再乱输指令,头套给你薅一地,必须打你脸。" << endl; }
        }
        system("pause");
    }
}
void user::showbook()
{
    ifstream fin;
    fin.open("book.txt", ios::in);
    if (!fin)
    {
        cout << "文件打开失败!" << endl;
        exit(1);
    }
    int i = 0;
    while (fin >> list->book_name)
    {
        cout << "NO." << ++i << endl;
        fin >> list->author;
        fin >> list->publisher;
        fin >> list->price;
        cout << "书名:" << "" << list->book_name << "" << '\t';
        cout << "作者:" << list->author << '\t';
        cout << "出版社:" << list->publisher << '\t';
        cout << "价格:" << list->price << endl;
    }
    fin.close();
}
void user::showuser() {
    cout << "用户名:" << getID() << endl;
    cout << "用户地址:" << getadress() << endl;
    cout << "用户身份:" << status << endl;
}
void user::sum(book*p){
    double pay = 0;
    while (p->next != NULL)
    {
        pay += p->next->price;
        p = p->next;
    }
    cout << "用户 " << this->getID() << " 应付" << pay << "" << endl;
}
user.cpp

admin类:

admin.h:

#pragma once
#include "book.h"
using namespace std;
class admin  
{
private:
    string Ad_name;
    string key;
    string status;
    book* list;
public:
    string getID(); 
    string getkey(); 
    admin(string i, string k);
    int signin();
    void main_ad();
};

 

#include "admin.h"
void admin::main_ad()
{
    int o;
    list->loadbook(list);
    while (1)
    {
        system("cls");
        printf("\n\n\n");
        printf("\t\t\t\t\t+-------------DoKiDoKi----------+\n");
        printf("\t\t\t\t\t|--------------管理员-----------|\n");
        printf("\t\t\t\t\t+-------------------------------+\n");
        printf("\t\t\t\t\t\t1 查看书籍\n");
        printf("\t\t\t\t\t\t2 添加书籍\n");
        printf("\t\t\t\t\t\t3 删除书籍\n");
printf("\t\t\t\t\t\t4 保存登出\n");
        printf("\n\n\n");
        printf("\t\t\t\t\t请输入你想实现的功能前的代号:\n");
        printf("\t\t\t\t");
        cin >> o;
        switch (o)
        {
        case 1:
        {system("cls");
        list->showbook(list);
        break;
        }
        case 2:
        {
            list->addbook(list);
            break;
        }
        case 3:
        {
            list->deletbook(list);
            break;
        }
case 4:
        {
            list->savebook(list);
            return;
        }
        default:
        {    cout << "请输入正确指令!" << endl << endl; }
        }
        system("pause");
    }
}
string admin::getID() { return Ad_name; }
string admin::getkey() { return key; }
admin::admin(string i, string k)
{
    Ad_name = i; key = k; status = "ad"; list = new book;
}
int admin::signin()
{
    string i, k;
    cout << "请输入账号:";
    cin >> i;
    cout << "请输入密码:";
    cin >> k;
    if (i == Ad_name && k == key)    return 1;
    else    return 0;
}
admin.cpp

 

此外,主函数也写得太长不好看,于是我把写在主函数里的菜单界面抽取成了一个函数,在main中调用,main函数就变得很简洁了:

int main()
{
    system("color 0A");
    admin A("monika", "160922");
    user B("me", "19170", "桥洞底下盖小被儿");
    Menu(A, B);
    return 0;
}

这是完整的main.cpp:

#include"user.h"
#include"admin.h"
using namespace std;
void Menu(admin &A,user&B) {
    while (1)
    {
        system("cls");
printf("\n\n\n");
        printf("\t\t\t\t\t+-------------DoKiDoKi----------+\n");
        printf("\t\t\t\t\t|-----------图书购买系统--------|\n");
        printf("\t\t\t\t\t+-------------------------------+\n");
        printf("\t\t\t\t\t\t1 管理员登录\n");
        printf("\t\t\t\t\t\t2 用户登录\n");
        printf("\t\t\t\t\t\t3 退出\n");
        printf("\n\n\n");
        printf("\t\t\t\t\t请输入你想实现的功能前的代号:\n");
        printf("\t\t\t\t");
        int o;
        cin >> o;
        switch (o)
        {
        case 1:
        {
            if (A.signin() == 1)
            {
                A.main_ad();
            }
            else    cout << "账号或密码错误" << endl;
            break;
        }
        case 2:
        {
            string id, k;
            cout << "请输入账号:";  cin >> id;
            cout << "请输入密码:";  cin >> k;
            if (id == "me" && k == "19170")
            {
                B.main_user();
                break;
            }
            else
                cout << "账号或密码错误" << endl;
            break;
        }
        case 3:
        {    return ; }
        default:
        {    cout << "请输入正确指令!" << endl << endl;   }
        }
        system("pause");
    }
}

int main()
{
    system("color 0A");
    admin A("monika", "160922");
    user B("me", "19170", "桥洞底下盖小被儿");
    Menu(A, B);
    return 0;
}
main.cpp

这样一来整个程序的结构就比较清晰了。

2.自动创建文件

就像我开头提到的,直接把代码复制粘贴到新环境中是无法运行的,因为缺少必要的txt文件,要解决这个问题也很简单,只要在main函数里加一个判断就好了。

ifstream in;
    in.open("book.txt");//打开book.txt,不存在就创建
    if (!in)
    {
        ofstream out("book.txt", ios::out); out.close();
    } in.close();  
        string fname = B.getID() + ".txt";    //打开用户购物车文件,不存在就创建          
        in.open(fname, ios::in);                      
        if (!in) { ofstream ou(fname, ios::out); ou.close(); }      
        in.close();

这段代码在管理员和用户对象创建以后会尝试打开对应的txt文件,如果打开失败就自动创建两个空的txt文件。

这样一来即使第一次运行的时候缺少txt文件程序也会自动创建,不会再报文件不存在的错啦。

3.管理员添加书籍重复性检验

作为管理员,可以向书库中添加书籍,然而原版不会对插入的书籍进行重复性检验,这样就会出现出现同名书籍的情况,这是不合理的。

不会对添加的书籍进行重复性检验

 

 

 于是我在addbook函数里加了这么一段:

while (p1 != p)                                          {
        if (p1->next->book_name == p2->book_name)  //
        {
            cout << endl << "书名重复!" << endl << endl;//
            p->next = NULL;
            delete p2;
            return;
        }
        p1 = p1->next;
    }

这段代码的作用是在用户输入了要添加的书名后,程序会自动从头到尾扫描书库文件,如果有同名的,就提示书名重复,然后终止此次添加。如下图

书库

 

 

 添加重名书籍

 

 

 提示

 

 

 

 

4.用户输入检验

经过测试,发现此程序有一处比较影响使用的bug。在输入指令的时候,如果用户输入了不符合预期的指令,程序就会陷入死循环,无法正常运行。输入指令的时候,一般来说都会根据提示输入对应指令的数字,但总有意外的情况发生,也许是按错了,也许是用户喜欢在酒吧里点炒饭,总之他输入了一个字母进去。这怎么能行!我接收指令的变量是int型的,结果cin收到的是字母,程序就崩了,具体表现是按回车确认后陷入死循环,不断的输出提示语句。

这个问题的原因是如果在 while 循环中使用 cin 来接收数据时发生了类型错误,可能会导致循环陷入死循环。这是因为如果输入的数据类型与程序期望的不匹配,cin 会进入一个错误状态,并停止从标准输入流中读取数据。如果不清除错误状态,后续的 cin 输入会被忽略,导致循环一直等待输入,从而陷入死循环。

为了避免这种情况,我们可以在循环中检查输入是否成功。如果成功,就跳出循环;如果失败,则需要清除错误状态并跳过残留字符,以便进行下一次输入。例如:

int num;
while (true) {
    cout << "请输入一个整数:" << endl;
    if (cin >> num) {
        // 输入成功
        break;
    }
    else {
        // 输入失败,清除错误状留字符
        cin.clear();//清除cin的错误状态
        cin.ignore(numeric_limits<streamsize>::max(), '\n');//跳过残留字符

        cout << "输入无效,请重新输入" << endl;
    }
}

使用 cin.clear() 清除错误状态。这个函数可以将 cin 从错误状态中恢复。

使用 cin.ignore() 跳过残留字符。这个函数可以跳过输入缓冲区中指定数量的字符。

需要注意的是,ignore函数的第二个参数指定了要跳过的字符,通常是换行符 \n,也就是我们输入的回车。在跳过残留字符之前,

可以使用 numeric_limits<streamsize>::max() 或者一个具体的数字来指定要跳过的最大字符数,这样可以确保所有的残留字符都被跳过。

 

对于这个问题我的解决方法是写一个函数来判断是否输入有效,有效就返回输入的数字,无效返回-1

double Input_oder() {
    int o;
    cin >> o;
    if (cin.fail()) {
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
         o=-1;
    }
    return o;
}

然后把程序里所有可能出现这种错误的输入语句换成这个函数,就解决了这个问题。

5.管理员添加修改书籍功能

只能增加和删除书籍是不够的,如果管理员临时想改书的信息怎么办,所以我又给管理员添加了一个修改已有图书的功能:

void book::updatebook(book* p)
{
    if (p->next == NULL)
    {
        cout << "目前无书籍信息!\t\t" << endl;
        return;
    }
    string na; cout << "请输入要更改信息的书名:" << endl; cin >> na;
    while (1)
    {
        if (p->next->book_name != na)
        {
            p = p->next;
            if (p->next == NULL)
            {
                system("cls");
                cout << "没有这本书的信息!" << endl << endl;
                return;
            }
        }
        else break;
    }
    cout << "找到了!" << endl;
    cout << "—————————————————————————————————————————————————————————\n";
    cout << "书名:" << "" << p->next->book_name << "" << '\t';
    cout << "作者:" << p->next->author << '\t';
    cout << "出版社:" << p->next->publisher << '\t';
    cout << "价格:" << p->next->price << '\n';
    cout << "—————————————————————————————————————————————————————————\n\n";
    cout << endl << endl;
    cout << "请选择要进行的修改操作:" << endl << endl;
    cout << "1) 修改书名" << endl;
    cout << "2) 修改作者" << endl;
    cout << "3) 修改出版社" << endl;
    cout << "4) 修改价格" << endl;
    cout << "7) 退出修改" << endl << endl;
    int order = Input_oder();
    switch (order)
    {
    case 1:
    {
        cout << "请输入修改后的书名:" << endl;
        string a; cin >> a;
        p->next->book_name = a;
        system("cls"); cout << "修改成功!" << endl;
        break;
    }
    case 2:
    {
        cout << "请输入修改后的作者:" << endl;
        string a; cin >> a;
        p->next->author = a;
        system("cls"); cout << "修改成功!" << endl;
        break;
    }
    case 3:
    {
        cout << "请输入修改后的出版社:" << endl;
        string a; cin >> a;
        p->next->publisher = a;
        system("cls"); cout << "修改成功!" << endl;
        break;
    }
    case 4:
    {
        cout << "请输入修改后的价格:" << endl;
        double a=Input_oder();
        if (a == -1) { cout << "再乱输指令,头套必须给你拽掉,必须打你脸!没你好果汁吃的,你记住了嗷!" << endl << endl; return; }
        p->next->price = a;
        system("cls"); cout << "修改成功!" << endl;
        break;
    }
    case 7:
    {break; }
    default:
    {
        system("cls");
        cout << "再乱输指令,头套必须给你拽掉,必须打你脸!没你好果汁吃的,你记住了嗷!" << endl << endl;
    }
    }
}
update

6.瞎搞的小功能

光看着控制台界面多没意思,放首歌听吧!(不会调音量,音乐声很大,不想听就注释掉这两句。)

我在main函数里加了这两句

mciSendString("open \"Twilight.mp3\" type mpegvideo alias mysong", NULL, 0, NULL);
    mciSendString("play mysong", NULL, 0, NULL);

是Windows的库函数,使用需要在头文件添加这几句:

#include <windows.h>
#include <mmsystem.h>
#pragma comment(lib, "winmm.lib")

如果没有正常播放的话可能是这么几个原因:文件目录下没有该文件、文件路径不对、项目字符集没有设置成多字节字符集、该音频文件太大加载不出来等等,实在放不出来就别放了。 

 

7.其他

除了以上的更改之外,我还重命名了一些函数和变量、为函数添加了一些注释、界面做了小小的改动等等,太过琐碎,就不一 一列举了。

 


 

三、更改后的代码

#pragma once
#include<iostream>
#include<string>
#include<fstream>
#include<stdlib.h>
using namespace std;
class book
{
protected:
    string book_name;
    string author;
    string publisher;
    double price;
    book* next;
public:
    friend class user;
    book() :next(NULL) {};              
    void addbook(book*); //添加书籍
    void deletbook(book*); //删除书籍
    void updatebook(book*);   //图书信息
    void showbook(book*); //展示书籍
    void loadbook(book*); //加载文件
    void savebook(book*);  //保存数据
    
};
book.h
#include "book.h"
double Input_oder();
void book::addbook(book* p)
{
    book* p1 = p;
    while (p->next != NULL)
    {
        p = p->next;
    }
    book* p2 = new book;
    p->next = p2;
    cout << "请输入书名:" << endl;
    cin >> p2->book_name;
    while (p1 != p)                                      //
    {
        if (p1->next->book_name == p2->book_name)  //
        {
            cout << endl << "书名重复!" << endl << endl;//
            p->next = NULL;//                          检查书名是否重复
            delete p2;
            return;
        }
        p1 = p1->next;
    }
    cout << "请输入作者:" << endl;
    cin >> p2->author;
    cout << "请输入出版社:" << endl;
    cin >> p2->publisher;
    cout << "请输入价格:" << endl;
    p2->price=Input_oder();
    if (p2->price == -1)
    {
        cout << "价格输入错误!";
        p->next = NULL;
        delete p2;
            return;
    }

    system("cls");
    cout << "添加成功!" << endl;
}
void book::showbook(book* p)
{
    if (p->next == NULL)
    {
        cout << "目前无书籍信息!\t\t" << endl;
        return;
    }
    p = p->next;
    int n = 1;
    while (1)
    {
        cout << "NO." << n << ":" << '\t' << endl;
        cout << "书名:" << "" << p->book_name << "" << '\t';
        cout << "作者:" << p->author << '\t';
        cout << "出版社:" << p->publisher << '\t';
        cout << "价格:" << p->price << '\n';
        if (p->next == NULL)break;
        p = p->next;
        n++;
    }
}
void book::deletbook(book* p)
{
    if (p->next == NULL)
    {
        cout << "目前无书籍信息!\t\t" << endl;
        return;
    }
    string na; cout << "请输入要删除的书名:" << endl; cin >> na;
    while (1)
    {
        if (p->next->book_name != na)
        {
            p = p->next;
            if (p->next == NULL)
            {
                system("cls");
                cout << "没有这本书的信息!" << endl << endl;
                break;
            }
        }
        else
        {
            cout << "找到了!" << endl;
            cout << "—————————————————————————————————————————————————————————\n";
            cout << "书名:" << "" << p->next->book_name << "" << '\t';
            cout << "作者:" << p->next->author << '\t';
            cout << "出版社:" << p->next->publisher << '\t';
            cout << "价格:" << p->next->price << '\n';
            cout << "—————————————————————————————————————————————————————————\n\n";
            cout << "是否删除该书籍信息?" << endl;
            cout << "1) 是\t0)否" << endl;
            int a; cin >> a;
            switch (a)
            {
            case 0:
            {    break; }
            case 1:
            {
                p->next = p->next->next;
                system("cls");
                cout << "删除成功!" << endl;
                break;
            }
            default:
                cout << "ERROR!" << endl;
            }
            break;
        }
    }
}
void book::updatebook(book* p)
{
    if (p->next == NULL)
    {
        cout << "目前无书籍信息!\t\t" << endl;
        return;
    }
    string na; cout << "请输入要更改信息的书名:" << endl; cin >> na;
    while (1)
    {
        if (p->next->book_name != na)
        {
            p = p->next;
            if (p->next == NULL)
            {
                system("cls");
                cout << "没有这本书的信息!" << endl << endl;
                return;
            }
        }
        else break;
    }
    cout << "找到了!" << endl;
    cout << "—————————————————————————————————————————————————————————\n";
    cout << "书名:" << "" << p->next->book_name << "" << '\t';
    cout << "作者:" << p->next->author << '\t';
    cout << "出版社:" << p->next->publisher << '\t';
    cout << "价格:" << p->next->price << '\n';
    cout << "—————————————————————————————————————————————————————————\n\n";
    cout << endl << endl;
    cout << "请选择要进行的修改操作:" << endl << endl;
    cout << "1) 修改书名" << endl;
    cout << "2) 修改作者" << endl;
    cout << "3) 修改出版社" << endl;
    cout << "4) 修改价格" << endl;
    cout << "7) 退出修改" << endl << endl;
    int order = Input_oder();
    switch (order)
    {
    case 1:
    {
        cout << "请输入修改后的书名:" << endl;
        string a; cin >> a;
        p->next->book_name = a;
        system("cls"); cout << "修改成功!" << endl;
        break;
    }
    case 2:
    {
        cout << "请输入修改后的作者:" << endl;
        string a; cin >> a;
        p->next->author = a;
        system("cls"); cout << "修改成功!" << endl;
        break;
    }
    case 3:
    {
        cout << "请输入修改后的出版社:" << endl;
        string a; cin >> a;
        p->next->publisher = a;
        system("cls"); cout << "修改成功!" << endl;
        break;
    }
    case 4:
    {
        cout << "请输入修改后的价格:" << endl;
        double a=Input_oder();
        if (a == -1) { cout << "再乱输指令,头套必须给你拽掉,必须打你脸!没你好果汁吃的,你记住了嗷!" << endl << endl; return; }
        p->next->price = a;
        system("cls"); cout << "修改成功!" << endl;
        break;
    }
    case 7:
    {break; }
    default:
    {
        system("cls");
        cout << "再乱输指令,头套必须给你拽掉,必须打你脸!没你好果汁吃的,你记住了嗷!" << endl << endl;
    }
    }
}
void book::loadbook(book* p)
{
    ifstream fin;
    fin.open("book.txt", ios::in);
    if (!fin)
    {
        cout << "文件打开失败!" << endl;
        exit(1);
    }
    string name;
    while (fin >> name)
    {
        book* newnode = new book;
        newnode->book_name = name;
        fin >> newnode->author;
        fin >> newnode->publisher;
        fin >> newnode->price;
        p->next = newnode;
        p = p->next;
    }
    fin.close();
}
void book::savebook(book* p)
{
    ofstream fout;
    fout.open("book.txt", ios::out);
    if (!fout)
    {
        cout << "文件打开失败!" << endl;
        exit(1);
    }
    while (p->next != NULL)
    {
        fout << p->next->book_name << "\t";
        fout << p->next->author << "\t";
        fout << p->next->publisher << "\t";
        fout << p->next->price << "\n";
        p = p->next;
    }
    fout.close();
}
book.cpp
#pragma once
#include "book.h"
using namespace std;
class user
{
protected:
    string user_name;          //用户名
    string key;              //用户密码
    string adress;             //用户地址
    double pay;
    string status;          //用户身份
    book * list;                //购物车
public:
    user(string, string, string);
    string getID(); 
    string getadress();
    string getkey();
    void main_user();       //用户界面
    void main_shop_list(); //购物车管理界面
    void showbook();         //显示书籍信息
    void addbook(book*);        //向购物车中添加书籍
    void deletebook(book*);       //从购物车中删除书籍
    void load(book*);       //从文件读入购物车信息
    void save(book*);   //保存购物车信息
    void showuser();  //显示用户信息
    void sum(book*);  //购物车结算函数
};
user.h
#include "user.h"
string user::getID() { return user_name; }  //取用户名
string user::getadress() { return adress; }//取用户地址
string user::getkey() { return key; }  //取用户密码
user::user(string I, string k, string ad)
{
    user_name = I; key = k; adress = ad; status = "user";
    list = new book();
}
void user::addbook(book* p)
{
    book* p1 = new book;
    p1->loadbook(p1);
    p1->showbook(p1);
    if (p1->next == NULL)
    {
        cout << "购物车当前为空!" << endl;
        return;
    }
    cout << endl << "请输入要添加的书籍名称:" << endl;
    string name; cin >> name;
    while (p1->next != NULL)
    {
        if (p1->next->book_name == name)
        {
            cout << "找到了!" << endl;
            cout << "—————————————————————————————————————————————————————————\n";
            cout << "书名:" << "" << p1->next->book_name << "" << '\t';
            cout << "作者:" << p1->next->author << '\t';
            cout << "出版社:" << p1->next->publisher << '\t';
            cout << "价格:" << p1->next->price << '\n';
            cout << "—————————————————————————————————————————————————————————\n\n";
            cout << "是否将此书加入购物车?" << endl;
            cout << "1) 是\t0)否" << endl;
            int o; cin >> o;
            if (o == 1)
            {
                while (p->next != NULL)
                    p = p->next;
                p->next = p1->next;
                p1->next->next = NULL;
                system("cls");
                cout << "添加成功!" << endl;
                break;
            }
            else if (o == 0)
            {
                break;
            }
            else
            {
                cout << "请输入正确指令!" << endl;
                break;
            }
        }
        p1 = p1->next;
    }
    if (p1->next == NULL)
    {
        cout << "未找到符合条件的书籍!" << endl;
    }
}
void user::deletebook(book* p)
{
    if (p->next == NULL)
    {
        cout << "购物车当前为空!" << endl;
        return;
    }
    cout << endl << "请输入要删除的书籍名称:" << endl;
    string name; cin >> name;
    int i = 0;
    while (p->next != NULL)
    {
        if (p->next->book_name == name)
        {
            i++;
            cout << "找到了!" << endl;
            cout << "—————————————————————————————————————————————————————————\n";
            cout << "书名:" << "" << p->next->book_name << "" << '\t';
            cout << "作者:" << p->next->author << '\t';
            cout << "出版社:" << p->next->publisher << '\t';
            cout << "价格:" << p->next->price << '\n';
            cout << "—————————————————————————————————————————————————————————\n\n";
            cout << "是否将此书从购物车中移除?" << endl;
            cout << "1) 是\t0)否" << endl;
            int o; cin >> o;
            if (o == 1)
            {
                p->next = p->next->next;
                system("cls");
                cout << "删除成功!" << endl;
                break;
            }
            else if (o == 0)
            {
                break;
            }
            else
            {
                cout << "请输入正确指令!" << endl;
                break;
            }
        }
        p = p->next;
    }
    if (i == 0)
    {
        cout << "未找到符合条件的书籍!" << endl;
    }
}
void user::load(book* p)
{
    if (p->next != NULL)p->next = NULL;
    string fname = this->user_name + ".txt";
    ifstream fin;
    fin.open(fname, ios::in);
    if (!fin)
    {
        cout << "文件打开失败!" << endl;
        exit(1);
    }
    string name;
    book* p1 = new book;
    book* p2 = p1;
    list->loadbook(p1);
    while (fin >> name)
    {
        book* newnode = new book;
        newnode->book_name = name;
        fin >> newnode->author;
        fin >> newnode->publisher;
        fin >> newnode->price;
        while (p1->next != NULL)
        {
            if (p1->next->book_name == newnode->book_name)
            {
                newnode->author = p1->next->author;
                newnode->publisher = p1->next->publisher;
                newnode->price = p1->next->price;
                break;
            }
            else    p1 = p1->next;
        }
        if (p1->next != NULL)
        {
            p->next = newnode;
            p = p->next;
            p1 = p2;
        }
    }
    fin.close();
}
void user::save(book* p)
{
    string fname = this->user_name + ".txt";
    ofstream fout;
    fout.open(fname, ios::out);
    if (!fout)
    {
        cout << "文件打开失败!" << endl;
        exit(1);
    }
    while (p->next != NULL)
    {
        fout << p->next->book_name << "\t";
        fout << p->next->author << "\t";
        fout << p->next->publisher << "\t";
        fout << p->next->price << "\n";
        p = p->next;
    }
    fout.close();
}
void user::main_user()
{
    int o;
    while (1)
    {
        system("cls");
        printf("\n\n\n");
        printf("\t\t\t\t\t+-------------DoKiDoKi----------+\n");
        printf("\t\t\t\t\t|--------------用户-----------|\n");
        printf("\t\t\t\t\t+-------------------------------+\n");
        printf("\t\t\t\t\t\t1 查看书籍\n");
        printf("\t\t\t\t\t\t2 管理购物车\n");
        printf("\t\t\t\t\t\t3 查看个人信息\n");
        printf("\t\t\t\t\t\t4 登出\n");
        printf("\n\n\n");
        printf("\t\t\t\t\t请输入你想实现的功能前的代号:\n");
        printf("\t\t\t\t");
        cin >> o;
        switch (o)
        {
        case 1:
        {
            system("cls");
            showbook();
            break;
        }
        case 2:
        {
            main_shop_list();
            break;
        }
        case 3:
        {
            showuser();
            break;
        }
        case 4:
        {return;    }
        default:
        {cout << "请输入正确指令!" << endl << endl; }
        }
        system("pause");
    }
}
void user::main_shop_list()
{
    this->load(list);
    while (1)
    {
        system("cls");
        cout << this->getID() << "的购物车:" << endl << endl;
        list->showbook(list);
        printf("\n\n\n");
        printf("\t\t\t\t\t+-------------DoKiDoKi----------+\n");
        printf("\t\t\t\t\t|--------------购物车-----------|\n");
        printf("\t\t\t\t\t+-------------------------------+\n");
        printf("\t\t\t\t\t\t1 向购物车中添加书籍\n");
        printf("\t\t\t\t\t\t2 从购物车中删除书籍\n");
        printf("\t\t\t\t\t\t3 结算\n");
        printf("\t\t\t\t\t\t4 退出购物车\n");
        printf("\n\n\n");
        printf("\t\t\t\t\t请输入你想实现的功能前的代号:\n");
        printf("\t\t\t\t");
        int o;
        cin >> o;
        switch (o)
        {
        case 1:
        {
            addbook(list);
            break;
        }
        case 2:
        {
            deletebook(list);
            break;
        }
        case 3:
        {
            sum(list);
            break;
        }
        case 4:
        {
            save(list);
            return;
        }
        default:
        {cout << "再乱输指令,头套给你薅一地,必须打你脸。" << endl; }
        }
        system("pause");
    }
}
void user::showbook()
{
    ifstream fin;
    fin.open("book.txt", ios::in);
    if (!fin)
    {
        cout << "文件打开失败!" << endl;
        exit(1);
    }
    int i = 0;
    while (fin >> list->book_name)
    {
        cout << "NO." << ++i << endl;
        fin >> list->author;
        fin >> list->publisher;
        fin >> list->price;
        cout << "书名:" << "" << list->book_name << "" << '\t';
        cout << "作者:" << list->author << '\t';
        cout << "出版社:" << list->publisher << '\t';
        cout << "价格:" << list->price << endl;
    }
    fin.close();
}
void user::showuser() {
    cout << "用户名:" << getID() << endl;
    cout << "用户地址:" << getadress() << endl;
    cout << "用户身份:" << status << endl;
}
void user::sum(book*p){
    double pay = 0;
    while (p->next != NULL)
    {
        pay += p->next->price;
        p = p->next;
    }
    cout << "用户 " << this->getID() << " 应付" << pay << "" << endl;
}
user.cpp
#pragma once
#include "book.h"
using namespace std;
class admin  
{
private:
    string Ad_name;
    string key;
    string status;
    book* list;
public:
    string getID(); 
    string getkey(); 
    admin(string i, string k);
    int signin();
    void main_ad();
};
admin.h
#include "admin.h"
double Input_oder();
void admin::main_ad()
{

    list->loadbook(list);
    while (1)
    {
        system("cls");
        printf("\n\n\n");
        printf("\t\t\t\t\t+-------------DoKiDoKi----------+\n");
        printf("\t\t\t\t\t|--------------管理员-----------|\n");
        printf("\t\t\t\t\t+-------------------------------+\n");
        printf("\t\t\t\t\t\t1 查看书籍\n");
        printf("\t\t\t\t\t\t2 添加书籍\n");
        printf("\t\t\t\t\t\t3 删除书籍\n");
        printf("\t\t\t\t\t\t4 修改书籍信息\n");
        printf("\t\t\t\t\t\t5 保存登出\n");
        printf("\n\n\n");
        printf("\t\t\t\t\t请输入你想实现的功能前的代号:\n");
        printf("\t\t\t\t");
        int o = Input_oder();
        switch (o)
        {
        case 1:
        {system("cls");
        list->showbook(list);
        break;
        }
        case 2:
        {
            list->addbook(list);
            break;
        }
        case 3:
        {
            list->deletbook(list);
            break;
        }
        case 4:
        {
            list->updatebook(list);
            break;
        }
        case 5:
        {
            list->savebook(list);
            return;
        }
        default:
        {    cout << "请输入正确指令!" << endl << endl; }
        }
        system("pause");
    }
}
string admin::getID() { return Ad_name; }
string admin::getkey() { return key; }
admin::admin(string i, string k)
{
    Ad_name = i; key = k; status = "ad"; list = new book;
}
int admin::signin()
{
    string i, k;
    cout << "请输入账号:";
    cin >> i;
    cout << "请输入密码:";
    cin >> k;
    if (i == Ad_name && k == key)    return 1;
    else    return 0;
}
admin.cpp
#include <windows.h>
#include <mmsystem.h>
#pragma comment(lib, "winmm.lib")
#include"user.h"
#include"admin.h"
using namespace std;
double Input_oder() {
    int o;
    cin >> o;
    if (cin.fail()) {
        cin.clear();
        cin.ignore(1000, '\n');
         o=-1;
    }
    return o;
}
void Menu(admin &A,user&B) {
    while (1)
    {
        system("cls");
        printf("\n\n\n");
        printf("\t\t\t\t\t+-------------DoKiDoKi----------+\n");
        printf("\t\t\t\t\t|-----------图书购买系统--------|\n");
        printf("\t\t\t\t\t+-------------------------------+\n");
        printf("\t\t\t\t\t\t1 管理员登录\n");
        printf("\t\t\t\t\t\t2 用户登录\n");
        printf("\t\t\t\t\t\t3 退出\n");
        printf("\n\n\n");
        printf("\t\t\t\t\t请输入你想实现的功能前的代号:\n");
        printf("\t\t\t\t");
        int o=Input_oder();
        switch (o)
        {
        case 1:
        {
            if (A.signin() == 1)
            {
                A.main_ad();
            }
            else    cout << "账号或密码错误" << endl;
            break;
        }
        case 2:
        {
            string id, k;
            cout << "请输入账号:";  cin >> id;
            cout << "请输入密码:";  cin >> k;
            if (id == "me" && k == "19170")
            {
                B.main_user();
                break;
            }
            else
                cout << "账号或密码错误" << endl;
            break;
        }
        case 3:
        {    return ; }
        default:
        {    cout << "请输入正确指令!" << endl << endl;   }
        }
        system("pause");
    }
}
int main()
{
    system("color 0A");
    mciSendString("open \"Twilight.mp3\" type mpegvideo alias mysong", NULL, 0, NULL);
    mciSendString("play mysong repeat", NULL, 0, NULL);
    admin A("monika", "160922");
    user B("me", "19170", "桥洞底下盖小被儿");
    ifstream in;
    in.open("book.txt");//打开book.txt,不存在就创建
    if (!in)
    {
        ofstream out("book.txt", ios::out); out.close();
    } in.close();  
        string fname = B.getID() + ".txt";    //打开用户购物车文件,不存在就创建          
        in.open(fname, ios::in);                      
        if (!in) { ofstream ou(fname, ios::out); ou.close(); }      
        in.close();
    Menu(A, B);
    return 0;
}
main.cpp

 好像没办法上传附件啊。。。

四、未完成的工作

其实对于原本的代码还有许多改进的空间,比如类的设计有缺陷、变量和属性起名不合理,代码冗余等,但是要全部进行修改的话实在是太麻烦了,时间上也不允许,而且我是个懒狗就先做这么多吧,以后有时间的话大概也许可能会再改改 随便说说的别当真, 

 

posted @ 2023-03-09 01:54  FataMorgana  阅读(52)  评论(0)    收藏  举报