Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <vector>
- #include <fstream>
- #include <algorithm>
- using namespace std;
- class libraryBook
- {
- private:
- string name;
- int sheets;
- public:
- libraryBook()
- {
- name ="";
- sheets=0;
- }
- libraryBook (string nameArg,int sheetsArg)
- {
- name=nameArg;
- sheets=sheetsArg;
- }
- string getName()
- {
- return name;
- }
- int getNumber()
- {
- return sheets;
- }
- bool operator <(libraryBook p)
- {
- if(name.length()<p.name.length()) return true;
- }
- bool operator ==(libraryBook p)
- {
- if(p.name == name ){return true;}
- else return false;
- }
- friend ostream& operator <<(ostream& fp,libraryBook p)
- {
- fp<< " Book name : "<<p.name<< " Number of sheets : "<<p.sheets<<endl;
- return fp;
- }
- friend istream& operator >>(istream& fp,libraryBook p)
- {
- fp>>p.name>>p.sheets;
- }
- };
- class library
- {
- private:
- string libraryName;
- vector<libraryBook>books;
- public:
- library(string fileName)
- {
- fstream fp;
- string buff;
- int pos;
- int counter = 0;
- fp.open(fileName);
- if(fp.is_open())
- {
- while(getline(fp,buff))
- if(counter != 0)
- {
- pos=buff.find(" ");
- books.push_back(libraryBook(buff.substr(0,pos),stoi(buff.substr(pos))));
- }else{
- libraryName=buff;
- counter++;
- }
- }
- fp.close();
- }
- library()
- {
- libraryName="";
- books.push_back(libraryBook());
- }
- string getLibraryName()
- {
- return libraryName;
- }
- void setLibraryName(string nameArg)
- {
- libraryName=nameArg;
- }
- vector<libraryBook>getBooks()
- {
- return books;
- }
- friend ostream& operator <<(ostream& str,library cp)
- {
- vector<libraryBook>::iterator it=cp.books.begin();
- while(it != cp.books.end())
- {
- str<<endl<<"Library Name : "<<cp.libraryName<<*it;
- it++;
- }
- return str;
- }
- friend istream& operator >>(istream& str,library cp)
- {
- vector<libraryBook>::iterator it = cp.books.begin();
- str>>cp.libraryName>>*it;
- }
- vector<libraryBook>getDuplicate()
- {
- vector<libraryBook>duplicates;
- vector<libraryBook>::iterator it1 = books.begin();
- vector<libraryBook>::iterator it2 = books.begin();
- while(it1 != books.end())
- {
- it2=it1 +1;
- while(it2 != books.end())
- {
- if( (*it1).getName() == (*it2).getName() )
- {
- duplicates.push_back(*it1);
- }
- it2++;
- }
- it1++;
- }
- duplicates.erase(unique(duplicates.begin(),duplicates.end()),duplicates.end());
- return duplicates;
- }
- int countDuplicates()
- {
- vector<libraryBook>buff = getDuplicate();
- vector<libraryBook>::iterator it = buff.begin();
- int counter=0;
- while(it != buff.end())
- {
- it++;
- counter++;
- }
- return counter;
- }
- };
- int main()
- {
- vector<libraryBook>buff;
- library myTest("file.txt");
- buff = (myTest.getDuplicate());
- vector<libraryBook>::iterator it = buff.begin();
- cout<< " number of duplicates : ";
- /*while(buff.end() != it)
- {
- cout<<endl<<(*it).getName();
- it++;
- }*/
- int count2 = myTest.countDuplicates();
- cout<<count2;
- system("pause");
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement