Advertisement
SkeptaProgrammer

Untitled

Dec 18th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.77 KB | None | 0 0
  1. // ConsoleApplication7.cpp : Этот файл содержит функцию "main". Здесь начинается и заканчивается выполнение программы.
  2. // сортировка каталога файлов естествнным слиянием
  3.  
  4. /*
  5. 1. сделать файл
  6. 2. сдеть ещё 2 файла
  7. 3. сделать ещё 1 файл
  8. 4. написать функцию сравнения строк
  9. 5. спиздить оставшийся алгоритм
  10. */
  11.  
  12. #include "pch.h"
  13. #include <iostream>
  14. #include <string>
  15. #include <algorithm>
  16. #include <fstream>
  17. #include <vector>
  18. using namespace std;
  19.  
  20. const string path = "C:\\Users\\Владимир\\source\\repos\\ConsoleApplication7\\c.txt";
  21. const string path1 = "C:\\Users\\Владимир\\source\\repos\\ConsoleApplication7\\c1.txt";
  22. const string path2 = "C:\\Users\\Владимир\\source\\repos\\ConsoleApplication7\\c2.txt";
  23. string allocateStrings(string first, string second)
  24. {
  25. int size = min(first.size(), second.size());
  26. string ext1="", ext2="",name1="",name2="";
  27. for (int i = first.find('.') + 1; i < first.size(); i++)
  28. ext1 += first[i];
  29. for (int i = 0; i < first.find('.') + 1; i++)
  30. name1 += first[i];
  31. for (int i = 0; i < second.find('.') + 1; i++)
  32. name2 += second[i];
  33. for (int i = second.find('.') + 1; i < second.size(); i++)
  34. ext2 += second[i];
  35. if (ext1 > ext2) return second;
  36. else if (ext1 < ext2) return first;
  37. else
  38. {
  39. if (name1 > name2) return name2;
  40. else if (name1 < name2) return name1;
  41. else return name1;
  42. }
  43. }
  44.  
  45. void clear_file(const string& file_name)
  46. {
  47. fstream(file_name, fstream::out);
  48. }
  49.  
  50. void fillFile(fstream& f1, fstream& f2)
  51. {
  52. fstream f(path);
  53. string s="";
  54. while (!f1.eof())
  55. {
  56. f1 >> s;
  57. f << s;
  58. }
  59. while (!f1.eof())
  60. {
  61. f2 >> s;
  62. f << s;
  63. }
  64. f.close();
  65. clear_file(path1);
  66. clear_file(path2);
  67. }
  68.  
  69. vector<string> getNames()
  70. {
  71. vector<string> names;
  72. fstream f(path);
  73. string str = "";
  74. while (!f.eof())
  75. {
  76. f >> str;
  77. names.push_back(str);
  78. }
  79. f.close();
  80. return names;
  81. }
  82.  
  83. int main()
  84. {
  85. string str = "";
  86. vector<string> names, names1;
  87. names = getNames();
  88. while (true)
  89. {
  90. fstream f1(path1), f2(path2);
  91. if (names == names1) break;
  92. for (int i = 0; i < names.size() - 1; i += 2)
  93. {
  94. str = allocateStrings(names[i], names[i + 1]);
  95. f1 << str;
  96. names[i] == str ? f2 << names[i + 1] : f2 << names[i];
  97. }
  98. clear_file(path);
  99. f1.close(); f1.open(path1);
  100. f2.close(); f2.open(path2);
  101. fstream f(path);
  102. string s = "";
  103. while (!f1.eof())
  104. {
  105. f1 >> s;
  106. f << s;
  107. }
  108. while (!f1.eof())
  109. {
  110. f2 >> s;
  111. f << s;
  112. }
  113. f.close();
  114. clear_file(path1);
  115. clear_file(path2);
  116.  
  117. names1 = names;
  118. names.clear();
  119. names = getNames();
  120. }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement