Advertisement
Garey

Test

Jan 30th, 2020
600
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.02 KB | None | 0 0
  1.  
  2. class File {
  3. private:
  4. string fileName;
  5. string fileType;
  6. string fileOwner;
  7. int fileSize;
  8. int fileVersion;
  9.  
  10. public:
  11. static int versionCounter;
  12.  
  13. File() {
  14. this->fileName = "";
  15. this->fileType = "";
  16. this->fileOwner = "";
  17. this->fileSize = 0;
  18. this->fileVersion = 0;
  19. }
  20.  
  21. File(string fileName, string fileType, string fileOwner, int fileSize) {
  22. if (fileName.length() < 0) {
  23. return;
  24. }
  25.  
  26. this->fileName = fileName;
  27. this->fileType = fileType;
  28. this->fileOwner = fileOwner;
  29. this->fileSize = fileSize;
  30. }
  31.  
  32. string getFileName() {
  33. return this->fileName;
  34. }
  35.  
  36. string getFileType() {
  37. return this->fileType;
  38. }
  39.  
  40. string getFileOwner() {
  41. return this->fileOwner;
  42. }
  43.  
  44. int getFileSize() {
  45. return this->fileSize;
  46. }
  47.  
  48. int getFileVersion() {
  49. return this->fileVersion;
  50. }
  51.  
  52. void setFileSize(int fileSize) {
  53. this->fileSize = fileSize;
  54. }
  55.  
  56. void setFileVersion(int fileVersion) {
  57. this->fileVersion = fileVersion;
  58. }
  59.  
  60. bool operator<(File& obj) {
  61. return this->fileName < obj.fileName;
  62. }
  63.  
  64. bool operator==(File& obj) {
  65. return this->fileName == obj.fileName && this->fileVersion == obj.fileVersion;
  66. }
  67.  
  68.  
  69. friend istream& operator>>(istream& input, File& obj) {
  70. input >> obj.fileName >> obj.fileOwner >> obj.fileType >> obj.fileSize >> obj.fileVersion;
  71.  
  72. return input;
  73. }
  74.  
  75. friend ostream& operator<<(ostream& output, File& obj) {
  76. output << obj.fileName << obj.fileOwner << obj.fileType << obj.fileSize << obj.fileVersion;
  77.  
  78. return output;
  79. }
  80. };
  81.  
  82. class Folder {
  83. private:
  84. string folderName;
  85. vector<File> items;
  86.  
  87. public:
  88. Folder(string fileName) {
  89. fstream file(fileName);
  90.  
  91. file >> this->folderName;
  92.  
  93. while (!file.eof()) {
  94. File item;
  95.  
  96. file >> item;
  97.  
  98. this->items.push_back(item);
  99. }
  100. }
  101.  
  102. void removeDuplicates() {
  103. sort(this->items.begin(), this->items.end());
  104. this->items.erase(unique(this->items.begin(), this->items.end()), this->items.end());
  105. }
  106.  
  107. /*!
  108. * 1. Сортираме вектора, за да използваме set_difference
  109. * 2. files променливата от тип set ще запази елементите сортирани автоматично и ще махне дублираните
  110. * 3. result ще вземе списък от тези отпаднали елементи чрез set_differences в range this->items - files
  111. */
  112. vector<File> getDuplicates() {
  113. sort(this->items.begin(), this->items.end());
  114.  
  115. set<File> files(this->items.begin(), this->items.end());
  116.  
  117. vector<File> result;
  118.  
  119. set_difference(this->items.begin(), this->items.end(), files.begin(), files.end(), back_inserter(result));
  120.  
  121. return result;
  122. }
  123.  
  124. File getMinimumSize() {
  125. return *min(this->items.begin(), this->items.end(), [](File& file1, File& file2) {
  126. return file1.getFileSize() < file2.getFileSize();
  127. });
  128. }
  129.  
  130. int getSumFileSize() {
  131. int sum = 0;
  132.  
  133. for (auto item : this->items) {
  134. sum += item.getFileSize();
  135. }
  136.  
  137. //return sum;
  138.  
  139. return accumulate(this->items.begin(), this->items.end(), 0, [](File& obj) {
  140. return obj.getFileSize();
  141. });
  142. }
  143.  
  144. friend ostream& operator<<(ostream& output, Folder& obj) {
  145. output << obj.folderName;
  146.  
  147. for (auto item : obj.items) {
  148. output << item;
  149. }
  150.  
  151. return output;
  152. }
  153. };
  154.  
  155. int main()
  156. {
  157. try {
  158. Folder folder("test.txt");
  159.  
  160. cout << folder.getSumFileSize();
  161.  
  162. File file = folder.getMinimumSize();
  163. cout << file;
  164. }
  165. catch (exception err) {
  166. cout << err.what();
  167. }
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement