Advertisement
fursty

OOP1

Mar 2nd, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.90 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <fstream>
  5. #include <algorithm>
  6. using namespace std;
  7.  
  8. class libraryBook
  9. {
  10. private:
  11.     string name;
  12.     int sheets;
  13.  
  14. public:
  15.     libraryBook()
  16.     {
  17.         name ="";
  18.         sheets=0;
  19.     }
  20.  
  21.     libraryBook (string nameArg,int sheetsArg)
  22.     {
  23.         name=nameArg;
  24.         sheets=sheetsArg;
  25.     }
  26.  
  27.     string getName()
  28.     {
  29.         return name;
  30.     }
  31.  
  32.     int getNumber()
  33.     {
  34.         return sheets;
  35.     }
  36.  
  37.     bool operator <(libraryBook p)
  38.     {
  39.         if(name.length()<p.name.length()) return true;
  40.     }
  41.  
  42.     bool operator ==(libraryBook p)
  43.     {
  44.         if(p.name == name ){return true;}
  45.         else return false;
  46.     }
  47.  
  48.     friend ostream& operator <<(ostream& fp,libraryBook p)
  49.     {
  50.         fp<< " Book name : "<<p.name<< " Number of sheets : "<<p.sheets<<endl;
  51.         return fp;
  52.     }
  53.  
  54.     friend istream& operator >>(istream& fp,libraryBook p)
  55.     {
  56.         fp>>p.name>>p.sheets;
  57.     }
  58.  
  59. };
  60.  
  61. class library
  62. {
  63. private:
  64.     string libraryName;
  65.     vector<libraryBook>books;
  66.  
  67. public:
  68.     library(string fileName)
  69.     {
  70.         fstream fp;
  71.         string buff;
  72.         int pos;
  73.         int counter = 0;
  74.         fp.open(fileName);
  75.         if(fp.is_open())
  76.         {
  77.             while(getline(fp,buff))
  78.                 if(counter != 0)
  79.                 {
  80.                     pos=buff.find(" ");
  81.                     books.push_back(libraryBook(buff.substr(0,pos),stoi(buff.substr(pos))));
  82.                 }else{
  83.                     libraryName=buff;
  84.                     counter++;
  85.                 }
  86.  
  87.         }
  88.         fp.close();
  89.     }
  90.  
  91.     library()
  92.     {
  93.         libraryName="";
  94.         books.push_back(libraryBook());
  95.     }
  96.  
  97.     string getLibraryName()
  98.     {
  99.         return libraryName;
  100.     }
  101.  
  102.     void setLibraryName(string nameArg)
  103.     {
  104.         libraryName=nameArg;
  105.     }
  106.  
  107.     vector<libraryBook>getBooks()
  108.     {
  109.         return books;
  110.     }
  111.  
  112.     friend ostream& operator <<(ostream& str,library cp)
  113.     {
  114.         vector<libraryBook>::iterator it=cp.books.begin();
  115.         while(it != cp.books.end())
  116.         {
  117.             str<<endl<<"Library Name : "<<cp.libraryName<<*it;
  118.             it++;
  119.         }
  120.         return str;
  121.     }
  122.  
  123.     friend istream& operator >>(istream& str,library cp)
  124.     {
  125.         vector<libraryBook>::iterator it = cp.books.begin();
  126.         str>>cp.libraryName>>*it;
  127.     }
  128.  
  129.     vector<libraryBook>getDuplicate()
  130.     {
  131.         vector<libraryBook>duplicates;
  132.         vector<libraryBook>::iterator it1 = books.begin();
  133.         vector<libraryBook>::iterator it2 = books.begin();
  134.  
  135.         while(it1 != books.end())
  136.         {
  137.             it2=it1 +1;
  138.             while(it2 != books.end())
  139.             {
  140.                 if( (*it1).getName() == (*it2).getName() )
  141.                 {
  142.                     duplicates.push_back(*it1);        
  143.                 }          
  144.                 it2++;
  145.             }  
  146.             it1++;
  147.         }
  148.         duplicates.erase(unique(duplicates.begin(),duplicates.end()),duplicates.end());
  149.  
  150.         return duplicates;
  151.     }
  152.    
  153.     int countDuplicates()
  154.     {
  155.         vector<libraryBook>buff = getDuplicate();
  156.         vector<libraryBook>::iterator it = buff.begin();
  157.         int counter=0;
  158.         while(it != buff.end())
  159.         {
  160.             it++;
  161.             counter++;
  162.                
  163.         }
  164.         return counter;
  165.     }
  166.    
  167. };
  168.  
  169.  
  170. int main()
  171. {
  172.    
  173.     vector<libraryBook>buff;
  174.    
  175.     library myTest("file.txt");
  176.     buff = (myTest.getDuplicate());
  177.     vector<libraryBook>::iterator it = buff.begin();
  178.     cout<< " number of duplicates : ";
  179.         /*while(buff.end() != it)
  180.     {
  181.         cout<<endl<<(*it).getName();
  182.         it++;
  183.  
  184.     }*/
  185.     int count2 = myTest.countDuplicates();
  186.     cout<<count2;
  187.  
  188.    
  189.  
  190.  
  191.  
  192.     system("pause");
  193.     return 1;
  194. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement