Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <list>
- using namespace std;
- class BookFair {
- public:
- string NameBook;
- string Author;
- double Price;
- BookFair() {
- NameBook = Author = Price = 0;
- }
- BookFair(string NameBook, string Author, double Price) {
- this->NameBook = NameBook;
- this->Author = Author;
- this->Price = Price;
- }
- void Print() {
- cout << "NameBook: " << NameBook << "\tAuthor: " << Author << "\tPrice: " << Price << endl;
- }
- };
- static void SortByNameBook(list<BookFair> &list) {
- list.sort([](BookFair& l1, BookFair& l2) { return l1.NameBook < l2.NameBook; });
- }
- static void SortByAuthor(list<BookFair> &list) {
- list.sort([](BookFair& l1, BookFair& l2) { return l1.Author < l2.Author; });
- }
- static void SortByPrice(list<BookFair> &list) {
- list.sort([](BookFair& l1, BookFair& l2) { return l1.Price < l2.Price; });
- }
- int main() {
- string path = "text.txt";
- BookFair bf("a", "3", 3);
- // bf.Print();
- /* ofstream fout;
- fout.open(path, ofstream::app);
- if (!fout.is_open())
- {
- cout << "Error open!" << endl;
- }
- else {
- fout.write((char*)&bf, sizeof(BookFair));
- }
- fout.close();*/
- list<BookFair> l;
- ifstream fin;
- fin.open(path);
- if (!fin.is_open())
- {
- cout << "Error open!" << endl;
- }
- else {
- BookFair b;
- while (fin.read((char*)&b, sizeof(BookFair)))
- {
- // b.Print();
- l.push_back(b);
- }
- for (auto i : l)
- cout << i.NameBook << "\t" << i.Author << "\t" << i.Price << "\t" << endl;
- SortByNameBook(l);
- cout << endl;
- for (auto i : l)
- cout << i.NameBook << "\t" << i.Author << "\t" << i.Price << "\t" << endl;
- }
- fin.close();
- }
Advertisement
Add Comment
Please, Sign In to add comment