Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.07 KB | None | 0 0
  1. // Gorbenko_lab_6.cpp : Этот файл содержит функцию "main". Здесь начинается и заканчивается выполнение программы.
  2. //
  3.  
  4. // MultipathMerge.cpp : Этот файл содержит функцию "main". Здесь начинается и заканчивается выполнение программы.
  5. //
  6.  
  7. #include "pch.h"
  8. #include <iostream>
  9. #include <iostream>
  10. #include <string>
  11. #include <fstream>
  12.  
  13. using namespace std;
  14.  
  15. // путь к файлам
  16. const int n = 6,    // кол-во всех файлов
  17. nh = n / 2; // кол-во файлов на ввод или на вывод
  18. int leng, random;
  19. bool eot;
  20. fstream src;    // основной файл
  21. fstream f[n];
  22.  
  23. void rewrite(fstream & f, int i) {
  24.     f.close();
  25.     f.open("f" + to_string(i) + ".txt", ios::out);
  26. }
  27.  
  28. void reset(fstream & f, int i) {
  29.     f.close();
  30.     f.open("f" + to_string(i) + ".txt", ios::in);
  31. }
  32.  
  33. void write(fstream & f, int & key) {
  34.     f << key;
  35. }
  36.  
  37. void read(fstream & f, int & key) {
  38.     f >> key;
  39. }
  40.  
  41. void close(fstream &f) {
  42.     f.close();
  43. }
  44. // правильно ставит пробелы
  45. void space(fstream &f) {
  46.     if ((int)f.tellg() != 0)
  47.         f << " ";
  48. }
  49. // получаем число и возвращаем курсом на позицию, перед взятием числа
  50. int getWithSeekg(fstream &f) {
  51.     int cur = f.tellg();
  52.     int buf = INT_MIN;
  53.     read(f, buf);
  54.     f.seekg(cur);
  55.     return buf;
  56. }
  57. // тупо пажылой рандом для заполнения файла
  58. void randFile() {
  59.     leng = 10;
  60.     random = 7789;
  61.     rewrite(src, -1);
  62.     do {
  63.         random = (131071 * random) % 2147483647;
  64.         int buf = random / 2147484;
  65.         space(src);
  66.         write(src, buf);
  67.         leng = leng - 1;
  68.     } while (leng != 0);
  69.     src.close();
  70. }
  71. // разбиваем на nh файлов
  72. void distribute(int &l) {
  73.     for (int i = 0; i < nh; ++i)
  74.         rewrite(f[i], i);
  75.     int j = nh - 1;
  76.     l = 0;
  77.     do {
  78.         if (j < nh - 1)
  79.             j++;
  80.         else
  81.             j = 0;
  82.         l++;
  83.         do {
  84.             int buf1, buf2;
  85.             read(src, buf1);
  86.             space(f[j]);
  87.             write(f[j], buf1);
  88.  
  89.             int cur = src.tellg();
  90.             read(src, buf2);
  91.             src.seekg(cur);
  92.             eot = (buf1 > buf2);
  93.         } while (!eot && (int)src.tellg() != -1);
  94.     } while ((int)src.tellg() != -1);
  95.  
  96.     for (int i = 0; i < nh; ++i)
  97.         close(f[i]);
  98. }
  99.  
  100. void tapeMergeSort() {
  101.     int i, j, mx, tx;
  102.     int k1, k2, l;
  103.     int x, min;
  104.     int t[n], ta[n];
  105.  
  106.     distribute(l);
  107.  
  108.     for (i = 0; i < n; ++i)
  109.         t[i] = i;
  110.  
  111.  
  112.     do {
  113.         if (l < nh)
  114.             k1 = l;
  115.         else
  116.             k1 = nh;
  117.         for (i = 0; i < k1; ++i) {
  118.             reset(f[t[i]], t[i]);
  119.             ta[i] = t[i];
  120.         }
  121.         for (i = k1; i < n; ++i)
  122.             rewrite(f[t[i]], t[i]);
  123.         l = 0;
  124.         j = nh;
  125.         do {
  126.             k2 = k1;
  127.             l++;
  128.             do {
  129.                 i = 0;
  130.                 mx = 0;
  131.                 min = getWithSeekg(f[ta[0]]);
  132.  
  133.                 while (i < k2) {
  134.                     x = getWithSeekg(f[ta[i]]);
  135.                     if (x < min) {
  136.                         min = x;
  137.                         mx = i;
  138.                     }
  139.                     i++;
  140.                 }
  141.                 int buf;
  142.                 read(f[ta[mx]], buf);
  143.                 eot = (int)f[ta[mx]].tellg() == -1;
  144.                 space(f[t[j]]);
  145.                 write(f[t[j]], buf);
  146.                 if (eot) {
  147.                     k1--;
  148.                     k2--;
  149.                     rewrite(f[ta[mx]], ta[mx]);
  150.                     ta[mx] = ta[k2];
  151.                     ta[k2] = ta[k1];
  152.  
  153.                 }
  154.                 else if (buf > getWithSeekg(f[ta[mx]])) {
  155.                     k2--;
  156.                     tx = ta[mx];
  157.                     ta[mx] = ta[k2];
  158.                     ta[k2] = tx;
  159.  
  160.                 }
  161.             } while (k2 > 0);
  162.  
  163.             if (j < n - 1)
  164.                 j++;
  165.             else
  166.                 j = nh;
  167.         } while (k1 != 0);
  168.  
  169.         for (i = 0; i < nh; ++i) {
  170.             tx = t[i];
  171.             t[i] = t[i + nh];
  172.             t[i + nh] = tx;
  173.         }
  174.     } while (l != 1);
  175. }
  176.  
  177. int getMarkNum(string mark)
  178. {
  179.     if (mark == "нет")
  180.         return 0;
  181.     if (mark == "удовл")
  182.         return 1;
  183.     if (mark == "хор")
  184.         return 2;
  185.     if (mark == "отл")
  186.         return 3;
  187.     return NULL;
  188. }
  189.  
  190. int getYearsInd(double years)
  191. {
  192.     if (years < 1.5)
  193.         return 0;
  194.     if (years < 6)
  195.         return 1;
  196.     else return 2;
  197. }
  198.  
  199. void makeSequence(string from)
  200. {
  201.     ifstream in(from+".txt");
  202.     ofstream f("f-1.txt");
  203.    
  204.     while (!in.eof()) {
  205.         string poroda;
  206.         double years;
  207.         string kl;
  208.         string fam, imya, otch, mark;
  209.  
  210.         in >> poroda >> years >> kl >> fam >> imya >> otch >> mark;
  211.  
  212.         if (!in.eof())
  213.             f << getMarkNum(mark) << " ";
  214.         else
  215.             if (mark != "")
  216.             f << getMarkNum(mark);
  217.     }
  218. }
  219.  
  220. void Divide()
  221. {
  222.     ifstream input("input.txt");
  223.     ofstream a[3];
  224.     a[0].open("a0.txt");
  225.     a[1].open("a1.txt");
  226.     a[2].open("a2.txt");
  227.  
  228.     while (!input.eof()) {
  229.         string poroda;
  230.         double years;
  231.         string kl;
  232.         string fam, imya, otch, mark;
  233.  
  234.         input >> poroda >> years >> kl >> fam >> imya >> otch >> mark;
  235.  
  236.         if (mark != "")
  237.         a[getYearsInd(years)] << poroda << " " << years << " " << kl << " " << fam << " " << imya << " " << otch << " " << mark << "\n";
  238.     }
  239.     input.close();
  240.     a[0].close();
  241.     a[1].close();
  242.     a[2].close();
  243. }
  244.  
  245.  
  246. void Divide_2(string name)
  247. {
  248.     ifstream input(name+".txt");
  249.     ofstream a[4];
  250.     a[0].open(name + "_0.txt");
  251.     a[1].open(name + "_1.txt");
  252.     a[2].open(name + "_2.txt");
  253.     a[3].open(name + "_3.txt");
  254.    
  255.  
  256.     while (!input.eof()) {
  257.         string poroda;
  258.         double years;
  259.         string kl;
  260.         string fam, imya, otch, mark;
  261.  
  262.         input >> poroda >> years >> kl >> fam >> imya >> otch >> mark;
  263.  
  264.         if (mark != "")
  265.         a[getMarkNum(mark)] << poroda << " " << years << " " << kl << " " << fam << " " << imya << " " << otch << " " << mark << "\n";
  266.  
  267.     }
  268.     input.close();
  269.     a[0].close();
  270.     a[1].close();
  271.     a[2].close();
  272.     a[3].close();
  273. }
  274.  
  275. void Combine()
  276. {
  277.     ofstream output("output.txt");
  278.  
  279.     for (char i = '0'; i < '3'; i++)
  280.     {
  281.         for (char j = '0'; j < '4'; j++)
  282.         {
  283.             ifstream f;
  284.             string k = "_";
  285.             string fname = "a";
  286.             fname += i;
  287.             fname += k + j + ".txt";
  288.             f.open(fname);
  289.  
  290.             while (!f.eof()) {
  291.                 string poroda;
  292.                 double years;
  293.                 string kl;
  294.                 string fam, imya, otch, mark;
  295.  
  296.                 f >> poroda >> years >> kl >> fam >> imya >> otch >> mark;
  297.                
  298.                 if (mark != "")
  299.                 output << poroda << " " << years << " " << kl << " " << fam << " " << imya << " " << otch << " " << mark << "\n";
  300.            
  301.             }
  302.  
  303.         }
  304.     }
  305.  
  306.     output.close();
  307.  
  308. }
  309.  
  310. int main()
  311. {
  312.  
  313.     //randFile();
  314.     reset(src, -1);
  315.     tapeMergeSort();
  316.  
  317.     Divide();
  318.     Divide_2("a0");
  319.     Divide_2("a1");
  320.     Divide_2("a2");
  321.     Combine();
  322.  
  323.     return 0;
  324. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement