Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include <iostream>
- #include <string>
- #include <fstream>
- using namespace std;
- class book
- {
- string name;
- string author;
- int year;
- public:
- book ( ) : name(""), author(""), year(0) {}
- book (string n, string a, int y) : name(n), author(a), year(y) {}
- bool operator < (const book& b)
- {
- return author < b.author;
- }
- string get_author ( ) const
- {
- return author;
- }
- friend istream & operator>> (istream & in, book & b)
- {
- in >> b.name >> b.author >> b.year;
- return in;
- }
- friend ostream & operator<< (ostream & out, const book & b)
- {
- out << b.name << " " << b.author << " " << b.year;
- return out;
- }
- };
- struct node
- {
- book data;
- node* next;
- node* prev;
- node(): next(NULL), prev(NULL) {}
- };
- class book_list
- {
- node * start;
- node * last;
- int count;
- public:
- book_list ( ) : start(NULL), last(NULL), count(0) {}
- ~book_list ( )
- {
- node* current = start;
- while ( current )
- {
- node* tmp = current;
- current = current->next;
- delete tmp;
- }
- }
- void add_begin (const book& C)
- {
- node* tmp = new node;
- tmp->data = C;
- if ( !start )
- start = last = tmp;
- else
- {
- start->prev = tmp;
- tmp->next = start;
- start = tmp;
- }
- count++;
- }
- void add(const book & b)
- {
- node* tmp = new node;
- tmp->data = b;
- if ( !last )
- start = last = tmp;
- else
- {
- tmp->prev = last;
- last->next = tmp;
- last = tmp;
- }
- count++;
- }
- void read (istream& in)
- {
- }
- void print (ostream& out)
- {
- }
- friend istream & operator>>(istream & in, book_list & b)
- {
- book el;
- while(!in.eof())
- {
- in >> el;
- b.add(el);
- }
- return in;
- }
- friend ostream & operator << (ostream & out, book_list & b)
- {
- out <<"Numbers of book: "<< b.count << endl;
- node* current = b.start;
- while ( current )
- {
- out << current->data << endl;
- current = current->next;
- }
- return out;
- }
- class iterator
- {
- node* curr;
- public:
- iterator() : curr(NULL) {}
- iterator (node * n) : curr(n) {}
- iterator (const iterator& i) : curr(i.curr) {}
- bool operator != (const iterator & i) const
- {
- return curr != i.curr;
- }
- iterator & operator++()
- {
- if (curr)
- curr = curr->next;
- return *this;
- }
- iterator & operator--()
- {
- if (curr)
- curr = curr->prev;
- return *this;
- }
- book & operator*()const
- {
- return curr->data;
- }
- };
- iterator begin()const
- {
- return iterator(start);
- }
- iterator end()const
- {
- return iterator(last);
- }
- int size()const
- {
- return count;
- }
- void sort ( )
- {
- iterator end;
- for (iterator i = begin(); i != end ; ++i)
- for(iterator j = i; j != end ; ++j)
- if (*j < *i)
- {
- swap(*j,*i);
- }
- }
- };
- using namespace std;
- int main()
- {
- book_list a;
- ifstream in("data.txt");
- in >> a;
- in.close();
- a.sort();
- ofstream out("result.txt");
- book_list b;
- out<<"------------Sorted by author list----------------"<<endl;
- out << a;
- string author;
- cout<<"Input author to select his books"<<endl;
- cin >> author;
- for (book_list::iterator it = a.begin(); it != a.end(); ++it)
- {
- if ((*it).get_author() == author) b.add(*it);
- }
- out<<"----------------List books of author "<<author<<" ----------------------"<<endl;
- try
- {
- if ( !b.size() ) throw "There is no books with this author!";
- out << b;
- }
- catch(char* str)
- {
- out << str << endl;
- }
- out.close();
- cout<<"Information has been processed and saved to file"<<endl;
- system("pause");
- return 0;
- }
Add Comment
Please, Sign In to add comment