Advertisement
ilyasizov

Untitled

Jun 20th, 2021
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.05 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <iomanip>
  4.  
  5. class File {
  6. public:
  7. std::string catalog;
  8. std::string name;
  9. std::string extension;
  10. std::string create_date;
  11. std::string create_time;
  12. bool is_del;
  13. int sec_cnt;
  14.  
  15. bool operator==(const File &other) {
  16. return (this->catalog == other.catalog && this->name == other.name
  17. && this->extension == other.extension && this->create_date == other.create_date &&
  18. this->create_time == other.create_time && this->is_del == other.is_del && this->sec_cnt == other.sec_cnt);
  19. }
  20.  
  21. friend std::ostream& operator<< (std::ostream &out, const File &f);
  22. friend std::istream& operator>> (std::istream &in, File &f);
  23. };
  24.  
  25. std::ostream& operator<< (std::ostream &out, const File &f) {
  26. out << f.catalog << "\n";
  27. out << f.name << " " << f.extension << " " << f.create_date << " " << f.create_time << "\n";
  28. out << f.is_del << " " << f.sec_cnt << "\n";
  29. return out;
  30. }
  31.  
  32. std::istream& operator>> (std::istream &in, File &f) {
  33. in >> f.catalog;
  34. in >> f.name >> f.extension >> f.create_date >> f.create_time;
  35. in >> f.is_del >> f.sec_cnt;
  36. return in;
  37. }
  38.  
  39.  
  40.  
  41. class Container {
  42. private:
  43. std::vector<File> data;
  44. public:
  45. void insert(const File &elem) {
  46. data.push_back(elem);
  47. }
  48.  
  49. void change(const File &elem, const File &new_elem) {
  50. for (auto &i : data) {
  51. if (i == elem) {
  52. i = new_elem;
  53. }
  54. }
  55. }
  56.  
  57. void del(const File &elem) {
  58. int pos = 0;
  59. for (auto &i : data) {
  60. if (i == elem) {
  61. break;
  62. }
  63. ++pos;
  64. }
  65. data.erase(data.begin() + pos);
  66. }
  67.  
  68. bool find_catalog(std::string &cur_catalog) {
  69. for (auto &i : data) {
  70. if (cur_catalog == i.catalog) {
  71. return 1;
  72. }
  73. }
  74. return 0;
  75. }
  76.  
  77. Container find_set_catalog(std::string &cur_catalog) {
  78. Container ans;
  79. for (auto &i : data) {
  80. if (cur_catalog == i.catalog) {
  81. ans.data.push_back(i);
  82. }
  83. }
  84. return ans;
  85. }
  86.  
  87. bool find_catalog_binary(std::string &cur_catalog) {
  88. int l = 0;
  89. int r = data.size() - 1;
  90. if (r - l > 1) {
  91. while (r - l > 1) {
  92. int m = (r - l) / 2;
  93. if (data[m].catalog <= cur_catalog) {
  94. l = m;
  95. } else {
  96. r = m;
  97. }
  98. }
  99. return data[l].catalog == cur_catalog;
  100. } else {
  101. return cur_catalog == data[0].catalog;
  102. }
  103. return 0;
  104. }
  105.  
  106. bool find_name(std::string &cur_name) {
  107. for (auto &i : data) {
  108. if (cur_name == i.name) {
  109. return 1;
  110. }
  111. }
  112. return 0;
  113. }
  114.  
  115. Container find_set_name(std::string &cur_name) {
  116. Container ans;
  117. for (auto &i : data) {
  118. if (cur_name == i.name) {
  119. ans.data.push_back(i);
  120. }
  121. }
  122. return ans;
  123. }
  124.  
  125. bool find_name_binary(std::string &cur_name) {
  126. int l = 0;
  127. int r = data.size() - 1;
  128. if (r - l > 1) {
  129. while (r - l > 1) {
  130. int m = (r - l) / 2;
  131. if (data[m].name <= cur_name) {
  132. l = m;
  133. } else {
  134. r = m;
  135. }
  136. }
  137. return data[l].name == cur_name;
  138. } else {
  139. return cur_name == data[0].name;
  140. }
  141. return 0;
  142. }
  143.  
  144. bool find_date(std::string &cur_date) {
  145. for (auto &i : data) {
  146. if (cur_date == i.create_date) {
  147. return 1;
  148. }
  149. }
  150. return 0;
  151. }
  152.  
  153. Container find_set_date(std::string &cur_date) {
  154. Container ans;
  155. for (auto &i : data) {
  156. if (cur_date == i.create_date) {
  157. ans.data.push_back(i);
  158. }
  159. }
  160. return ans;
  161. }
  162.  
  163. bool find_date_binary(std::string &cur_date) {
  164. int l = 0;
  165. int r = data.size() - 1;
  166. if (r - l > 1) {
  167. while (r - l > 1) {
  168. int m = (r - l) / 2;
  169. if (data[m].create_date <= cur_date) {
  170. l = m;
  171. } else {
  172. r = m;
  173. }
  174. }
  175. return data[l].create_date == cur_date;
  176. } else {
  177. return cur_date == data[0].create_date;
  178. }
  179. return 0;
  180. }
  181.  
  182. bool find_del(bool cur_del) {
  183. for (auto &i : data) {
  184. if (cur_del == i.is_del) {
  185. return 1;
  186. }
  187. }
  188. return 0;
  189. }
  190.  
  191. Container find_set_del(bool cur_del) {
  192. Container ans;
  193. for (auto &i : data) {
  194. if (cur_del == i.is_del) {
  195. ans.data.push_back(i);
  196. }
  197. }
  198. return ans;
  199. }
  200.  
  201. bool find_del_binary(bool cur_del) {
  202. int l = 0;
  203. int r = data.size() - 1;
  204. if (r - l > 1) {
  205. while (r - l > 1) {
  206. int m = (r - l) / 2;
  207. if (data[m].is_del <= cur_del) {
  208. l = m;
  209. } else {
  210. r = m;
  211. }
  212. }
  213. return data[l].is_del == cur_del;
  214. } else {
  215. return cur_del == data[0].is_del;
  216. }
  217. return 0;
  218. }
  219.  
  220. friend std::ostream& operator<< (std::ostream &out, const Container &cont);
  221.  
  222.  
  223. };
  224.  
  225. std::ostream& operator<< (std::ostream &out, const Container &cont) {
  226. for (auto i : cont.data) {
  227. out << i.catalog << "\n";
  228. out << i.name << " " << i.extension << " " << i.create_date << " " << i.create_time << "\n";
  229. out << i.is_del << " " << i.sec_cnt << "\n";
  230. }
  231. return out;
  232. }
  233.  
  234. /*Container ReadFromFile(std::fstream& file) {
  235. file.seekg(0, ios::beg);
  236. Container result;
  237. while (file.peek() != EOF) {
  238. File f;
  239. file >> f.catalog << "\n";
  240. file >> f.name >> f.extension >> f.create_date >> f.create_time;
  241. file >> f.is_del >> f.sec_cnt;
  242. result.push_back(f);
  243. }
  244. file.close();
  245. return result;
  246. }*/
  247.  
  248. int main() {
  249. int N;
  250. std::cin >> N;
  251. Container a;
  252. for (int i = 0; i < N; ++i) {
  253. int command;
  254. std::cin >> command;
  255. switch (command) {
  256. case 1: { // добавление
  257. File f;
  258. std::cin >> f;
  259. a.insert(f);
  260. }
  261. case 2: { // замена f на f2
  262. File f, f2;
  263. std::cin >> f >> f2;
  264. a.change(f, f2);
  265. }
  266. case 3: { // удаление f, если такой имеется
  267. File f;
  268. std::cin >> f;
  269. a.del(f);
  270. }
  271. case 4: { // вывод контейнера
  272. std::cout << a;
  273. std::cout << "\n";
  274. }
  275. case 5: { // поиск и выборка по каталогу
  276. std::string cur_catalog;
  277. std::cin >> cur_catalog;
  278. std::cout << a.find_catalog(cur_catalog) << "\n";
  279. std::cout << a.find_set_catalog(cur_catalog) << "\n";
  280. }
  281. case 6: { // поиск и выборка по имени
  282. std::string cur_name;
  283. std::cin >> cur_name;
  284. std::cout << a.find_catalog(cur_name) << "\n";
  285. std::cout << a.find_set_catalog(cur_name) << "\n";
  286. }
  287. case 7: { // поиск и выборка по дате
  288. std::string cur_date;
  289. std::cin >> cur_date;
  290. std::cout << a.find_date(cur_date) << "\n";
  291. std::cout << a.find_set_date(cur_date) << "\n";
  292. }
  293. case 8: { // поиск и выборка по признаку удаления
  294. std::string cur_del;
  295. std::cin >> cur_del;
  296. std::cout << a.find_date(cur_del) << "\n";
  297. std::cout << a.find_set_date(cur_del) << "\n";
  298. }
  299. }
  300. }
  301. return 0;
  302. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement