Need a unique gift idea?
A Pastebin account makes a great Christmas gift
SHARE
TWEET

Untitled

a guest Jul 21st, 2018 62 Never
Upgrade to PRO!
ENDING IN00days00hours00mins00secs
 
  1. #pragma once
  2. #include <iostream>
  3. #include <string>
  4. #include <fstream>
  5.  
  6. using namespace std;
  7.  
  8. class book
  9. {
  10.     string name;
  11.     string author;
  12.     int year;
  13. public:
  14.     book ( ) : name(""), author(""), year(0) {}
  15.     book (string n, string a, int y) : name(n), author(a), year(y) {}
  16.    
  17.     bool operator < (const book& b)
  18.     {
  19.         return author < b.author;
  20.     }
  21.     string get_author ( ) const
  22.     {
  23.         return author;
  24.     }
  25.     friend istream & operator>> (istream & in, book & b)
  26.     {
  27.         in >> b.name >> b.author >> b.year;
  28.         return in;
  29.     }
  30.     friend ostream & operator<< (ostream & out, const book & b)
  31.     {
  32.         out << b.name << " " << b.author << " " << b.year;
  33.         return out;
  34.     }
  35.  
  36. };
  37.  
  38.  
  39. struct node
  40. {
  41.     book data;
  42.     node* next;
  43.     node* prev;
  44.     node(): next(NULL), prev(NULL) {}
  45. };
  46.  
  47. class book_list
  48. {
  49.  
  50.     node * start;
  51.     node * last;
  52.     int count;
  53. public:
  54.     book_list ( ) : start(NULL), last(NULL), count(0) {}
  55.     ~book_list ( )
  56.     {
  57.         node* current = start;
  58.         while ( current )
  59.         {
  60.             node* tmp = current;
  61.             current = current->next;
  62.             delete tmp;
  63.         }
  64.     }
  65.  
  66.     void add_begin (const book& C)
  67.     {
  68.         node* tmp = new node;
  69.         tmp->data = C;
  70.         if ( !start )
  71.             start = last = tmp;
  72.         else
  73.         {
  74.             start->prev = tmp;
  75.             tmp->next = start;
  76.             start = tmp;
  77.         }
  78.         count++;
  79.     }
  80.  
  81.     void add(const book & b)
  82.     {
  83.         node* tmp = new node;
  84.         tmp->data = b;
  85.         if ( !last )
  86.             start = last = tmp;
  87.         else
  88.         {
  89.             tmp->prev = last;
  90.             last->next = tmp;
  91.             last = tmp;
  92.         }
  93.         count++;
  94.     }
  95.     void read (istream& in)
  96.     {
  97.        
  98.     }
  99.     void print (ostream& out)
  100.     {
  101.        
  102.     }
  103.     friend istream & operator>>(istream & in, book_list & b)
  104.     {
  105.         book el;
  106.         while(!in.eof())
  107.         {
  108.             in >> el;
  109.             b.add(el);
  110.         }
  111.         return in;
  112.     }
  113.     friend ostream & operator << (ostream & out, book_list & b)
  114.     {
  115.         out <<"Numbers of book: "<< b.count << endl;
  116.         node* current = b.start;
  117.         while ( current )
  118.         {
  119.             out << current->data << endl;
  120.             current = current->next;
  121.         }
  122.         return out;
  123.     }
  124.  
  125.     class iterator
  126.     {
  127.         node* curr;
  128.     public:
  129.         iterator() : curr(NULL) {}
  130.         iterator (node * n) : curr(n) {}
  131.         iterator (const iterator& i) : curr(i.curr) {}
  132.         bool operator != (const iterator & i) const
  133.         {
  134.             return curr != i.curr;
  135.         }
  136.         iterator & operator++()
  137.         {
  138.             if (curr)
  139.                 curr = curr->next;
  140.             return *this;
  141.         }
  142.         iterator & operator--()
  143.         {
  144.             if (curr)
  145.                 curr = curr->prev;
  146.             return *this;
  147.         }
  148.         book & operator*()const
  149.         {
  150.             return curr->data;
  151.         }
  152.     };
  153.     iterator begin()const
  154.     {
  155.         return iterator(start);
  156.     }
  157.     iterator end()const
  158.     {
  159.         return iterator(last);
  160.     }
  161.     int size()const
  162.     {
  163.         return count;
  164.     }
  165.  
  166.     void sort ( )
  167.     {
  168.         iterator end;
  169.         for (iterator i = begin(); i != end ; ++i)
  170.             for(iterator j = i; j != end ; ++j)
  171.             if (*j < *i)
  172.             {
  173.                 swap(*j,*i);
  174.             }
  175.     }
  176.  
  177. };
  178.  
  179.  
  180. using namespace std;
  181.  
  182. int main()
  183. {
  184.     book_list a;
  185.     ifstream in("data.txt");
  186.     in >> a;
  187.     in.close();
  188.  
  189.  
  190.     a.sort();
  191.    
  192.  
  193.     ofstream out("result.txt");
  194.     book_list b;
  195.  
  196.  
  197.     out<<"------------Sorted by author list----------------"<<endl;
  198.     out << a;
  199.    
  200.  
  201.     string author;
  202.     cout<<"Input author to select his books"<<endl;
  203.     cin >> author;
  204.    
  205.    
  206.    
  207.     for (book_list::iterator it = a.begin(); it != a.end(); ++it)
  208.     {
  209.         if ((*it).get_author() == author) b.add(*it);
  210.     }
  211.  
  212.     out<<"----------------List books of author "<<author<<" ----------------------"<<endl;
  213.     try
  214.     {
  215.         if ( !b.size() ) throw "There is no books with this author!";
  216.         out << b;
  217.     }
  218.     catch(char* str)
  219.     {
  220.         out << str << endl;
  221.     }
  222.  
  223.     out.close();
  224.  
  225.     cout<<"Information has been processed and saved to file"<<endl;
  226.  
  227.     system("pause");
  228.     return 0;
  229. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top