hurmawe

lab7

May 12th, 2022 (edited)
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.75 KB | None | 0 0
  1. #include <vector>
  2. #include <iostream>
  3. #include <string>
  4. #include <fstream>
  5. #include <Windows.h>
  6.  
  7. using namespace std;
  8. //Задание 6
  9. void odd_even(vector<int> &a,const int &lenght) {
  10.     int left = 0;
  11.     for (int i = 0; i < lenght; i++)
  12.     {
  13.         if (a[i] % 2 == 0)
  14.         {
  15.             swap(a[i], a[left]);
  16.             left++;
  17.         }  
  18.     }
  19. }
  20. //Задание 7
  21. int max_elem(const vector<int> &a, const int &lenght)
  22. {
  23.     int max_elem = INT_MIN;
  24.     for (int i = 0; i < lenght; i++)
  25.     {
  26.         if(a[i]> max_elem)
  27.             max_elem = a[i];
  28.     }
  29.     return max_elem;
  30. }
  31. //Задание 8
  32. bool rep(const vector< int> & a, const int &lenght)
  33. {
  34.     bool rep = 0;
  35.     for (int i = 0; i < lenght; i++)
  36.     {
  37.         int count = 0;
  38.         for (int j = i; j < lenght; j++)
  39.         {
  40.             if (a[i] == a[j])
  41.             {
  42.                 count++;
  43.                 if (count == 2)
  44.                     rep = 1;
  45.             }
  46.         }
  47.     }
  48.     return rep;
  49. }
  50.  
  51. //Задание 3-5
  52. void Qsort(vector<int> &a, int first, int last)
  53. {
  54.     int l, r, vr, x;
  55.     if (first < last)
  56.     {
  57.         x = a[(last + first) / 2];
  58.         l = first; r = last;
  59.         while (l <= r)
  60.         {
  61.             while (a[l] < x)l++;
  62.             while (a[r] > x)r--;
  63.             if (l <= r)
  64.             {
  65.                 vr = a[l];
  66.                 a[l] = a[r];
  67.                 a[r] = vr;
  68.                 l++; r--;
  69.             }
  70.         }
  71.         Qsort(a, first, r);
  72.         Qsort(a, l, last);
  73.     }
  74. }
  75.  
  76. int main()
  77. {
  78.     SetConsoleCP(1251);
  79.     SetConsoleOutputCP(1251);
  80.     //Задание 1 и 2
  81.     //
  82.     ////Заменить во всем тексте «короче» на «в общем»
  83.     //string str_old = "короче";
  84.     //string str_rep = "в общем";
  85.     //
  86.     ////Заменить во всем тексте «в общем» на «короче»
  87.     ////string str_old = "в общем";
  88.     ////string str_rep = "короче";
  89.  
  90.  
  91.     //string str;
  92.  
  93.     //ifstream fi("input12.txt");
  94.     //ofstream fo("output12.txt");
  95.     /*if (!fi.is_open() and !fo.is_open())
  96.     {
  97.         cout << "Ошибка открытия файлов" << endl;
  98.     }
  99.     else
  100.     {*/
  101.     //int pos = 0;
  102.     //while(getline(fi,str))
  103.     //{
  104.     //  while (-1 != (pos = str.find(str_old, pos)))
  105.     //      str.replace(pos, str_old.length(), str_rep);
  106.     //  fo << str << endl;
  107.     //  pos = 0;
  108.     //}
  109.     //
  110.     //fi.close(); fo.close();
  111. //}
  112. //Задание 3
  113.  
  114. string str;
  115. string vr;
  116. vector<int> array;
  117.  
  118. ifstream fi("input3.txt");
  119. ofstream fo("output3.dat", ios::binary);
  120.  
  121. if (!fi.is_open() and !fo.is_open())
  122. {
  123.     cout << "Ошибка открытия файлов" << endl;
  124. }
  125. else
  126. {
  127.  
  128. getline(fi, str);
  129. for (int i = 0; i < str.length(); i++)
  130. {
  131.     if (str[i] == ' ')
  132.     {
  133.         if (i > 0)
  134.             if (str[i - 1] != ' ')
  135.             {
  136.                 array.push_back(stoi(vr));
  137.                 vr = "";
  138.             }
  139.     }
  140.     else
  141.     {
  142.         vr.push_back(str[i]);
  143.     }
  144. }
  145.  
  146. Qsort(array, 0, array.size() - 1);
  147.  
  148. for (int i = 0; i < array.size(); i++)
  149. {
  150.  
  151.     fo.write((char*)&array[i], sizeof(int));
  152. }
  153. }
  154. fi.close(); fo.close();
  155.     ifstream fi2("output3.dat", ios::binary);
  156.     if (!fi2.is_open())
  157.     {
  158.         cout << "Ошибка открытия файлов" << endl;
  159.     }
  160.     else
  161.     {
  162.         int x = 0;
  163.         for (int i = 0; i < array.size(); i++) {
  164.             fi2.read((char*)&x, sizeof(int));
  165.             cout << x << " ";
  166.         }
  167.     }
  168.  
  169.     fi2.close();
  170.  
  171.  
  172.  
  173. //Задание 4
  174. /*
  175.     string str;
  176.     string vr;
  177.     vector<int> array;
  178.  
  179.     ifstream fi1("input4_1.txt"); ifstream fi2("input4_2.txt");
  180.     ofstream fo("output4.dat", ios::binary);
  181.     if (!fi1.is_open() or !fo.is_open() or !fi2.is_open())
  182.     {
  183.         cout << "Ошибка открытия файлов" << endl;
  184.     }
  185.     else
  186.     {
  187.         getline(fi1, str);
  188.         for (int i = 0; i < str.length(); i++)
  189.         {
  190.             if (str[i] == ' ')
  191.             {
  192.                 if (i > 0)
  193.                     if (str[i - 1] != ' ')
  194.                     {
  195.                         array.push_back(stoi(vr));
  196.                         vr = "";
  197.                     }
  198.             }
  199.             else
  200.             {
  201.                 vr.push_back(str[i]);
  202.             }
  203.         }
  204.         getline(fi2, str);
  205.         for (int i = 0; i < str.length(); i++)
  206.         {
  207.             if (str[i] == ' ')
  208.             {
  209.                 if (i > 0)
  210.                     if (str[i - 1] != ' ')
  211.                     {
  212.                         array.push_back(stoi(vr));
  213.                         vr = "";
  214.                     }
  215.             }
  216.             else
  217.             {
  218.                 vr.push_back(str[i]);
  219.             }
  220.         }
  221.         Qsort(array, 0, array.size() - 1);
  222.  
  223.         for (int i = 0; i < array.size(); i++)
  224.         {
  225.  
  226.             fo.write((char*)&array[i], sizeof(int));
  227.         }
  228.     }
  229.     fi1.close(); fi2.close(); fo.close();
  230.  
  231.     ifstream fi3("output4.dat", ios::binary);
  232.     if (!fi3.is_open())
  233.     {
  234.         cout << "Ошибка открытия файлов" << endl;
  235.     }
  236.     else
  237.     {
  238.         int x = 0;
  239.         for (int i = 0; i < array.size(); i++) {
  240.             fi3.read((char*)&x, sizeof(int));
  241.             cout << x << " ";
  242.         }
  243.     }
  244.  
  245.     fi3.close();
  246. */
  247.     //vector<int> array = { 2,6,0, 9,66,785,632,569,56,654,2,0 };
  248.     ////Задание 6
  249.     //odd_even(array, array.size());
  250.     //cout << " Задание 6" << endl;
  251.     //for (int i = 0; i < array.size(); i++)
  252.     //{
  253.     //  cout << array[i] << " ";
  254.     //}
  255.     //cout << endl;
  256.     //// Задание 7
  257.     //cout << "Задание 7"<<max_elem(array, array.size())<< endl;
  258.     ////Задание 8
  259.     //cout <<"Задание 8: "<< rep(array,array.size()) << endl;
  260. }
  261.  
  262.  
Add Comment
Please, Sign In to add comment