Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ConsoleApplication7.cpp : Этот файл содержит функцию "main". Здесь начинается и заканчивается выполнение программы.
- // сортировка каталога файлов естествнным слиянием
- /*
- 1. сделать файл
- 2. сдеть ещё 2 файла
- 3. сделать ещё 1 файл
- 4. написать функцию сравнения строк
- 5. спиздить оставшийся алгоритм
- */
- #include "pch.h"
- #include <iostream>
- #include <string>
- #include <algorithm>
- #include <fstream>
- #include <vector>
- using namespace std;
- const string path = "C:\\Users\\Владимир\\source\\repos\\ConsoleApplication7\\c.txt";
- const string path1 = "C:\\Users\\Владимир\\source\\repos\\ConsoleApplication7\\c1.txt";
- const string path2 = "C:\\Users\\Владимир\\source\\repos\\ConsoleApplication7\\c2.txt";
- string allocateStrings(string first, string second)
- {
- int size = min(first.size(), second.size());
- string ext1="", ext2="",name1="",name2="";
- for (int i = first.find('.') + 1; i < first.size(); i++)
- ext1 += first[i];
- for (int i = 0; i < first.find('.') + 1; i++)
- name1 += first[i];
- for (int i = 0; i < second.find('.') + 1; i++)
- name2 += second[i];
- for (int i = second.find('.') + 1; i < second.size(); i++)
- ext2 += second[i];
- if (ext1 > ext2) return second;
- else if (ext1 < ext2) return first;
- else
- {
- if (name1 > name2) return name2;
- else if (name1 < name2) return name1;
- else return name1;
- }
- }
- void clear_file(const string& file_name)
- {
- fstream(file_name, fstream::out);
- }
- void fillFile(fstream& f1, fstream& f2)
- {
- fstream f(path);
- string s="";
- while (!f1.eof())
- {
- f1 >> s;
- f << s;
- }
- while (!f1.eof())
- {
- f2 >> s;
- f << s;
- }
- f.close();
- clear_file(path1);
- clear_file(path2);
- }
- vector<string> getNames()
- {
- vector<string> names;
- fstream f(path);
- string str = "";
- while (!f.eof())
- {
- f >> str;
- names.push_back(str);
- }
- f.close();
- return names;
- }
- int main()
- {
- string str = "";
- vector<string> names, names1;
- names = getNames();
- while (true)
- {
- fstream f1(path1), f2(path2);
- if (names == names1) break;
- for (int i = 0; i < names.size() - 1; i += 2)
- {
- str = allocateStrings(names[i], names[i + 1]);
- f1 << str;
- names[i] == str ? f2 << names[i + 1] : f2 << names[i];
- }
- clear_file(path);
- f1.close(); f1.open(path1);
- f2.close(); f2.open(path2);
- fstream f(path);
- string s = "";
- while (!f1.eof())
- {
- f1 >> s;
- f << s;
- }
- while (!f1.eof())
- {
- f2 >> s;
- f << s;
- }
- f.close();
- clear_file(path1);
- clear_file(path2);
- names1 = names;
- names.clear();
- names = getNames();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement