Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Gorbenko_lab_6.cpp : Этот файл содержит функцию "main". Здесь начинается и заканчивается выполнение программы.
- //
- // MultipathMerge.cpp : Этот файл содержит функцию "main". Здесь начинается и заканчивается выполнение программы.
- //
- #include "pch.h"
- #include <iostream>
- #include <iostream>
- #include <string>
- #include <fstream>
- using namespace std;
- // путь к файлам
- const int n = 6, // кол-во всех файлов
- nh = n / 2; // кол-во файлов на ввод или на вывод
- int leng, random;
- bool eot;
- fstream src; // основной файл
- fstream f[n];
- void rewrite(fstream & f, int i) {
- f.close();
- f.open("f" + to_string(i) + ".txt", ios::out);
- }
- void reset(fstream & f, int i) {
- f.close();
- f.open("f" + to_string(i) + ".txt", ios::in);
- }
- void write(fstream & f, int & key) {
- f << key;
- }
- void read(fstream & f, int & key) {
- f >> key;
- }
- void close(fstream &f) {
- f.close();
- }
- // правильно ставит пробелы
- void space(fstream &f) {
- if ((int)f.tellg() != 0)
- f << " ";
- }
- // получаем число и возвращаем курсом на позицию, перед взятием числа
- int getWithSeekg(fstream &f) {
- int cur = f.tellg();
- int buf = INT_MIN;
- read(f, buf);
- f.seekg(cur);
- return buf;
- }
- // тупо пажылой рандом для заполнения файла
- void randFile() {
- leng = 10;
- random = 7789;
- rewrite(src, -1);
- do {
- random = (131071 * random) % 2147483647;
- int buf = random / 2147484;
- space(src);
- write(src, buf);
- leng = leng - 1;
- } while (leng != 0);
- src.close();
- }
- // разбиваем на nh файлов
- void distribute(int &l) {
- for (int i = 0; i < nh; ++i)
- rewrite(f[i], i);
- int j = nh - 1;
- l = 0;
- do {
- if (j < nh - 1)
- j++;
- else
- j = 0;
- l++;
- do {
- int buf1, buf2;
- read(src, buf1);
- space(f[j]);
- write(f[j], buf1);
- int cur = src.tellg();
- read(src, buf2);
- src.seekg(cur);
- eot = (buf1 > buf2);
- } while (!eot && (int)src.tellg() != -1);
- } while ((int)src.tellg() != -1);
- for (int i = 0; i < nh; ++i)
- close(f[i]);
- }
- void tapeMergeSort() {
- int i, j, mx, tx;
- int k1, k2, l;
- int x, min;
- int t[n], ta[n];
- distribute(l);
- for (i = 0; i < n; ++i)
- t[i] = i;
- do {
- if (l < nh)
- k1 = l;
- else
- k1 = nh;
- for (i = 0; i < k1; ++i) {
- reset(f[t[i]], t[i]);
- ta[i] = t[i];
- }
- for (i = k1; i < n; ++i)
- rewrite(f[t[i]], t[i]);
- l = 0;
- j = nh;
- do {
- k2 = k1;
- l++;
- do {
- i = 0;
- mx = 0;
- min = getWithSeekg(f[ta[0]]);
- while (i < k2) {
- x = getWithSeekg(f[ta[i]]);
- if (x < min) {
- min = x;
- mx = i;
- }
- i++;
- }
- int buf;
- read(f[ta[mx]], buf);
- eot = (int)f[ta[mx]].tellg() == -1;
- space(f[t[j]]);
- write(f[t[j]], buf);
- if (eot) {
- k1--;
- k2--;
- rewrite(f[ta[mx]], ta[mx]);
- ta[mx] = ta[k2];
- ta[k2] = ta[k1];
- }
- else if (buf > getWithSeekg(f[ta[mx]])) {
- k2--;
- tx = ta[mx];
- ta[mx] = ta[k2];
- ta[k2] = tx;
- }
- } while (k2 > 0);
- if (j < n - 1)
- j++;
- else
- j = nh;
- } while (k1 != 0);
- for (i = 0; i < nh; ++i) {
- tx = t[i];
- t[i] = t[i + nh];
- t[i + nh] = tx;
- }
- } while (l != 1);
- }
- int getMarkNum(string mark)
- {
- if (mark == "нет")
- return 0;
- if (mark == "удовл")
- return 1;
- if (mark == "хор")
- return 2;
- if (mark == "отл")
- return 3;
- return NULL;
- }
- int getYearsInd(double years)
- {
- if (years < 1.5)
- return 0;
- if (years < 6)
- return 1;
- else return 2;
- }
- void makeSequence(string from)
- {
- ifstream in(from+".txt");
- ofstream f("f-1.txt");
- while (!in.eof()) {
- string poroda;
- double years;
- string kl;
- string fam, imya, otch, mark;
- in >> poroda >> years >> kl >> fam >> imya >> otch >> mark;
- if (!in.eof())
- f << getMarkNum(mark) << " ";
- else
- if (mark != "")
- f << getMarkNum(mark);
- }
- }
- void Divide()
- {
- ifstream input("input.txt");
- ofstream a[3];
- a[0].open("a0.txt");
- a[1].open("a1.txt");
- a[2].open("a2.txt");
- while (!input.eof()) {
- string poroda;
- double years;
- string kl;
- string fam, imya, otch, mark;
- input >> poroda >> years >> kl >> fam >> imya >> otch >> mark;
- if (mark != "")
- a[getYearsInd(years)] << poroda << " " << years << " " << kl << " " << fam << " " << imya << " " << otch << " " << mark << "\n";
- }
- input.close();
- a[0].close();
- a[1].close();
- a[2].close();
- }
- void Divide_2(string name)
- {
- ifstream input(name+".txt");
- ofstream a[4];
- a[0].open(name + "_0.txt");
- a[1].open(name + "_1.txt");
- a[2].open(name + "_2.txt");
- a[3].open(name + "_3.txt");
- while (!input.eof()) {
- string poroda;
- double years;
- string kl;
- string fam, imya, otch, mark;
- input >> poroda >> years >> kl >> fam >> imya >> otch >> mark;
- if (mark != "")
- a[getMarkNum(mark)] << poroda << " " << years << " " << kl << " " << fam << " " << imya << " " << otch << " " << mark << "\n";
- }
- input.close();
- a[0].close();
- a[1].close();
- a[2].close();
- a[3].close();
- }
- void Combine()
- {
- ofstream output("output.txt");
- for (char i = '0'; i < '3'; i++)
- {
- for (char j = '0'; j < '4'; j++)
- {
- ifstream f;
- string k = "_";
- string fname = "a";
- fname += i;
- fname += k + j + ".txt";
- f.open(fname);
- while (!f.eof()) {
- string poroda;
- double years;
- string kl;
- string fam, imya, otch, mark;
- f >> poroda >> years >> kl >> fam >> imya >> otch >> mark;
- if (mark != "")
- output << poroda << " " << years << " " << kl << " " << fam << " " << imya << " " << otch << " " << mark << "\n";
- }
- }
- }
- output.close();
- }
- int main()
- {
- //randFile();
- reset(src, -1);
- tapeMergeSort();
- Divide();
- Divide_2("a0");
- Divide_2("a1");
- Divide_2("a2");
- Combine();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement