ડꫀꫀ ꪗꪮꪊ. 2022-12-28 20:15 采纳率: 88.6%
浏览 71
已结题

C++ list容器与文件的使用 读不出来

img


如图所示 我的文件里已经有存入对应的数据 但是读出来的时候 显示不全 我用的是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;//作者
};
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.num < b2.num;
    }




}
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();
        }
    }

    
        
    
    //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 20:48
    关注

    img

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 1月5日
  • 已采纳回答 12月28日
  • 创建了问题 12月28日

悬赏问题

  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来