ડꫀꫀ ꪗꪮꪊ. 2022-12-28 16:59 采纳率: 88.6%
浏览 85
已结题

C++ 容器 list 排序出错

C++容器 第7选项的排序功能 我思路:对list容器进行一个自定义排序规则进行排序后,然后将排序后的内容写入文件。现在我的排序出了问题,但思路没错,有点搞不懂哪里错了 就是我该怎么改才可以使用自定义排序?

#include<iostream>
using namespace std;
#include<fstream>
#define filename "booklist.txt"
#include<list>
#include<cstring>
#include<iomanip>

class Book
{
public:
    Book(string na = " ", int p = 0, int n = 0, string m = " ", string o = " ", int s = 0, int r = 0, string t = " ")
    {
        name = na;
        num = n;
        riqi = p;
        chubanshe = m;
        leibie = o;
        bianhao = s;
        ISBN = r;
        zuozhe = t;
    }
    void Show()
    {
        cout << "书名:" << std::left << setw(20) << name << std::right << setw(6) << "\t日期:" << riqi << "\t数量:" << num << "\t作者:"<<zuozhe<<"\t出版社:" << chubanshe << "\t类别:" << leibie << "\t编号:" << bianhao << "\tISBN:" << ISBN << endl;
    }
    void Set()
    {
        cout << "请输入书名:";
        cin >> name;
        cout << "请输入日期:";
        cin >> riqi;
        cout << "请输入数量:";
        cin >> num;
        cout << "请输入作者:";
        cin >> zuozhe;
        cout << "请输入出版社:";
        cin >> chubanshe;
        cout << "请输入类别:";
        cin >> leibie;
        cout << "请输入编号:";
        cin >> bianhao;
        cout << "请输入ISBN:";
        cin >> ISBN;
    }
    void Addnum()
    {
        int n;
        cout << "请输入归还的数量:";
        cin >> n;
        num += n;
    }
    void Borrownum()
    {
        int n;
        cout << "请输入借出的数量:";
        cin >> n;
        num -= n;
    }
public:
    string name;
    int riqi;//出版日期
    int num;//剩余数量
    string chubanshe;//出版社
    string leibie;//类别
    int bianhao;//编号
    int ISBN;//ISBN
    string zuozhe;//作者
};

void menu()
{
    cout << "--------------------------------------欢迎进入图书管理系统--------------------------------------" << endl;
    cout << endl << "0 - 退出系统;" << "1 - 显示库存;" << "2 - 查询图书;" << "3 - 借阅图书;" << "4 - 归还图书;" << "5 - 增加图书;" << "6 - 删除图书;" <<"7 -排序图书;"<< endl;
}

class Booklist//创建BookList类,数据成员有Book还有图书数量
{
public:
    void save()//新建图书的话保存数据,用app方式打开文件
    {
        ofstream fout(filename, ios::app);
        list<Book>::iterator it = BList.begin();
        for (int i = 0; i < num - 1; i++)//偏移迭代器,指向新加入的Book并写入文件
        {
            it++;
        }
        for (; it != BList.end(); it++)
        {
            fout << (*it).name << ' ' << (*it).riqi << ' ' << (*it).num << (*it).chubanshe<<'  '<<(*it).zuozhe<<'  '<<(*it).leibie << '  ' << (*it).ISBN << '  ' << (*it).bianhao << '  ' << '\n';
        }
        fout.close();
    }
    void resave()
    {
        ofstream fout(filename, ios::out);//重新写入数据,因为删除了某个元素
        if (fout.is_open())
        {
            for (list<Book>::iterator it = BList.begin(); it != BList.end(); it++)
            {
                fout << (*it).name << ' ' << (*it).riqi << ' ' << (*it).num << (*it).chubanshe << '  ' << (*it).zuozhe << '  ' << (*it).leibie << '  ' << (*it).ISBN << '  ' << (*it).bianhao << '  ' << '\n';
            }
        }
        fout.close();
    }
    void Show()
    {
        for (list<Book>::iterator it = BList.begin(); it != BList.end(); it++)
        {
            (*it).Show();
        }
    }
    void adddata()//添加数据
    {
        Book B;
        B.Set();
        BList.push_back(B);
        num++;
    }
    void start()//程序一开始读取文件里的数据
    {
        string na;
        int n;
        int p;
        string m;
        string o;
        int s;
        int r;
        string t;

        ifstream fin(filename, ios::in);
        if (fin.is_open())
        {
            while (fin >> na >> p >>  n>>  m>>  o>>  s>>  r>>  t)
            {
                Book B(na, p, n,m,o,s,r,t);
                BList.push_back(B);
                num++;
            }
        }
        fin.close();
    }
    void increase()
    {
        cout << "请输入书名:" << endl;
        string n;
        cin >> n;
        for (list<Book>::iterator it = BList.begin(); it != BList.end(); it++)
        {
            if ((*it).name == n)
                (*it).Addnum();
        }
        resave();
    }
    void decrease()
    {
        cout << "请输入书名:" << endl;
        string n;
        cin >> n;
        for (list<Book>::iterator it = BList.begin(); it != BList.end(); it++)
        {
            if ((*it).name == n)
                (*it).Borrownum();
        }
        resave();
    }
    void FindBook()
    {
        string name;
        cin >> name;
        for (list<Book>::iterator it = BList.begin(); it != BList.end(); it++)//遍历整个list,所以符合关键字的都会被找到
        {
            int index = (*it).name.find(name);//如果没找到返回值是一个很大的数
            if (index < (*it).name.length())
                (*it).Show();
        }
    }

    bool paixuguize(Book &b1, Book &b2)//排序规则
    {
        if (b1.leibie == b2.leibie)
        {
            if (b1.num == b2.num)
        
            {
                return b1.ISBN < b2.ISBN;

        }
            else
            {
                return b1.num < b2.num;
            }
            


    }
        else {
            return b1.leibie < b2.leibie;
        }




    }
    //void  show(list<Book>&b1) //排序后的显示
    //{
    //    for (list<Book>::iterator it = BList.begin(); it != BList.end(); it++)
    //    {


    //    cout<<"姓名:"<< (*it).name << ' ' <<"日期:" << (*it).riqi << ' ' <<"数量:" << (*it).num <<"出版社:"<< (*it).chubanshe << '  ' <<"作者:"<< (*it).zuozhe << '  ' <<"类别:"<< (*it).leibie << '  ' << "ISBN:"<<(*it).ISBN << '  ' << "编号:"<<(*it).bianhao << '  ' << '\n';
    //    }


    //}
    void paixu()//进行排序并打印
    {
        BList.sort(paixuguize);
        resave();
        
        


    }
    void DeleteBook()
    {
        string name;
        cout << "请输入书名:";
        cin >> name;
        int i = 0;
        for (list<Book>::iterator it = BList.begin(); it != BList.end(); it++)
        {
            if ((*it).name == name)
                break;
            ++i;
        }
        list<Book>::iterator it = BList.begin();
        advance(it, i);
        BList.erase(it);
        --num;
        resave();
    }
public:
    list<Book>BList;
    int num = 0;
};

int main()
{
    Booklist B1;
    B1.start();
    while (1)
    {
        menu();
        int key;
        cout << "请输入要进行的操作:";
        cin >> key;
        switch (key)
        {
        case 0:
            return 0;
            break;
        case 1:
            B1.Show();
            break;
        case 2:
            B1.FindBook();
            break;
        case 3:
            B1.decrease();
            break;
        case 4:
            B1.increase();
            break;
        case 5:
        {
            B1.adddata();
            B1.save();
            break;
        }
        case 6:
            B1.DeleteBook();
            break;
        case 7:
            B1.paixu();
            break;
            

        }

    }

}

  • 写回答

2条回答 默认 最新

  • 浪客 2022-12-28 18:07
    关注
    可以给Book重载<,后面用 BList.sort();排序
        bool operator<(const Book &b2)
        {
            if (leibie != b2.leibie)
                return leibie < b2.leibie;
    
            if (num != b2.num)
                return num < b2.num;
    
            return ISBN < b2.ISBN;
        }
    
    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 12月28日
  • 赞助了问题酬金15元 12月28日
  • 修改了问题 12月28日
  • 创建了问题 12月28日

悬赏问题

  • ¥15 STM32F103C8T6使用标准库stm32f10x.h驱动ws2812
  • ¥20 我是数控机床电气工程师,主要是做840DSL与one,请问如何自学
  • ¥20 显示器休眠后重新唤醒出现蓝屏,蓝屏代码为DRIVER-POWER.STATE-FAILURE
  • ¥20 alt+tab怎么恢复到以前的模式
  • ¥15 来一个会抓包app支付链接的
  • ¥15 MMdetection安装
  • ¥15 STM32U535系列stop3模式进入和RTC唤醒
  • ¥15 如何提取全民K歌没下载过但播放过很多次的音频?
  • ¥15 树莓派运行detect.py
  • ¥15 pdfplumber提起文本内容如何过滤水印内容