Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class File {
- private:
- string fileName;
- string fileType;
- string fileOwner;
- int fileSize;
- int fileVersion;
- public:
- static int versionCounter;
- File() {
- this->fileName = "";
- this->fileType = "";
- this->fileOwner = "";
- this->fileSize = 0;
- this->fileVersion = 0;
- }
- File(string fileName, string fileType, string fileOwner, int fileSize) {
- if (fileName.length() < 0) {
- return;
- }
- this->fileName = fileName;
- this->fileType = fileType;
- this->fileOwner = fileOwner;
- this->fileSize = fileSize;
- }
- string getFileName() {
- return this->fileName;
- }
- string getFileType() {
- return this->fileType;
- }
- string getFileOwner() {
- return this->fileOwner;
- }
- int getFileSize() {
- return this->fileSize;
- }
- int getFileVersion() {
- return this->fileVersion;
- }
- void setFileSize(int fileSize) {
- this->fileSize = fileSize;
- }
- void setFileVersion(int fileVersion) {
- this->fileVersion = fileVersion;
- }
- bool operator<(File& obj) {
- return this->fileName < obj.fileName;
- }
- bool operator==(File& obj) {
- return this->fileName == obj.fileName && this->fileVersion == obj.fileVersion;
- }
- friend istream& operator>>(istream& input, File& obj) {
- input >> obj.fileName >> obj.fileOwner >> obj.fileType >> obj.fileSize >> obj.fileVersion;
- return input;
- }
- friend ostream& operator<<(ostream& output, File& obj) {
- output << obj.fileName << obj.fileOwner << obj.fileType << obj.fileSize << obj.fileVersion;
- return output;
- }
- };
- class Folder {
- private:
- string folderName;
- vector<File> items;
- public:
- Folder(string fileName) {
- fstream file(fileName);
- file >> this->folderName;
- while (!file.eof()) {
- File item;
- file >> item;
- this->items.push_back(item);
- }
- }
- void removeDuplicates() {
- sort(this->items.begin(), this->items.end());
- this->items.erase(unique(this->items.begin(), this->items.end()), this->items.end());
- }
- /*!
- * 1. Сортираме вектора, за да използваме set_difference
- * 2. files променливата от тип set ще запази елементите сортирани автоматично и ще махне дублираните
- * 3. result ще вземе списък от тези отпаднали елементи чрез set_differences в range this->items - files
- */
- vector<File> getDuplicates() {
- sort(this->items.begin(), this->items.end());
- set<File> files(this->items.begin(), this->items.end());
- vector<File> result;
- set_difference(this->items.begin(), this->items.end(), files.begin(), files.end(), back_inserter(result));
- return result;
- }
- File getMinimumSize() {
- return *min(this->items.begin(), this->items.end(), [](File& file1, File& file2) {
- return file1.getFileSize() < file2.getFileSize();
- });
- }
- int getSumFileSize() {
- int sum = 0;
- for (auto item : this->items) {
- sum += item.getFileSize();
- }
- //return sum;
- return accumulate(this->items.begin(), this->items.end(), 0, [](File& obj) {
- return obj.getFileSize();
- });
- }
- friend ostream& operator<<(ostream& output, Folder& obj) {
- output << obj.folderName;
- for (auto item : obj.items) {
- output << item;
- }
- return output;
- }
- };
- int main()
- {
- try {
- Folder folder("test.txt");
- cout << folder.getSumFileSize();
- File file = folder.getMinimumSize();
- cout << file;
- }
- catch (exception err) {
- cout << err.what();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement