Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <vector>
- #include <iostream>
- #include <string>
- #include <fstream>
- #include <Windows.h>
- using namespace std;
- //Задание 6
- void odd_even(vector<int> &a,const int &lenght) {
- int left = 0;
- for (int i = 0; i < lenght; i++)
- {
- if (a[i] % 2 == 0)
- {
- swap(a[i], a[left]);
- left++;
- }
- }
- }
- //Задание 7
- int max_elem(const vector<int> &a, const int &lenght)
- {
- int max_elem = INT_MIN;
- for (int i = 0; i < lenght; i++)
- {
- if(a[i]> max_elem)
- max_elem = a[i];
- }
- return max_elem;
- }
- //Задание 8
- bool rep(const vector< int> & a, const int &lenght)
- {
- bool rep = 0;
- for (int i = 0; i < lenght; i++)
- {
- int count = 0;
- for (int j = i; j < lenght; j++)
- {
- if (a[i] == a[j])
- {
- count++;
- if (count == 2)
- rep = 1;
- }
- }
- }
- return rep;
- }
- //Задание 3-5
- void Qsort(vector<int> &a, int first, int last)
- {
- int l, r, vr, x;
- if (first < last)
- {
- x = a[(last + first) / 2];
- l = first; r = last;
- while (l <= r)
- {
- while (a[l] < x)l++;
- while (a[r] > x)r--;
- if (l <= r)
- {
- vr = a[l];
- a[l] = a[r];
- a[r] = vr;
- l++; r--;
- }
- }
- Qsort(a, first, r);
- Qsort(a, l, last);
- }
- }
- int main()
- {
- SetConsoleCP(1251);
- SetConsoleOutputCP(1251);
- //Задание 1 и 2
- //
- ////Заменить во всем тексте «короче» на «в общем»
- //string str_old = "короче";
- //string str_rep = "в общем";
- //
- ////Заменить во всем тексте «в общем» на «короче»
- ////string str_old = "в общем";
- ////string str_rep = "короче";
- //string str;
- //ifstream fi("input12.txt");
- //ofstream fo("output12.txt");
- /*if (!fi.is_open() and !fo.is_open())
- {
- cout << "Ошибка открытия файлов" << endl;
- }
- else
- {*/
- //int pos = 0;
- //while(getline(fi,str))
- //{
- // while (-1 != (pos = str.find(str_old, pos)))
- // str.replace(pos, str_old.length(), str_rep);
- // fo << str << endl;
- // pos = 0;
- //}
- //
- //fi.close(); fo.close();
- //}
- //Задание 3
- string str;
- string vr;
- vector<int> array;
- ifstream fi("input3.txt");
- ofstream fo("output3.dat", ios::binary);
- if (!fi.is_open() and !fo.is_open())
- {
- cout << "Ошибка открытия файлов" << endl;
- }
- else
- {
- getline(fi, str);
- for (int i = 0; i < str.length(); i++)
- {
- if (str[i] == ' ')
- {
- if (i > 0)
- if (str[i - 1] != ' ')
- {
- array.push_back(stoi(vr));
- vr = "";
- }
- }
- else
- {
- vr.push_back(str[i]);
- }
- }
- Qsort(array, 0, array.size() - 1);
- for (int i = 0; i < array.size(); i++)
- {
- fo.write((char*)&array[i], sizeof(int));
- }
- }
- fi.close(); fo.close();
- ifstream fi2("output3.dat", ios::binary);
- if (!fi2.is_open())
- {
- cout << "Ошибка открытия файлов" << endl;
- }
- else
- {
- int x = 0;
- for (int i = 0; i < array.size(); i++) {
- fi2.read((char*)&x, sizeof(int));
- cout << x << " ";
- }
- }
- fi2.close();
- //Задание 4
- /*
- string str;
- string vr;
- vector<int> array;
- ifstream fi1("input4_1.txt"); ifstream fi2("input4_2.txt");
- ofstream fo("output4.dat", ios::binary);
- if (!fi1.is_open() or !fo.is_open() or !fi2.is_open())
- {
- cout << "Ошибка открытия файлов" << endl;
- }
- else
- {
- getline(fi1, str);
- for (int i = 0; i < str.length(); i++)
- {
- if (str[i] == ' ')
- {
- if (i > 0)
- if (str[i - 1] != ' ')
- {
- array.push_back(stoi(vr));
- vr = "";
- }
- }
- else
- {
- vr.push_back(str[i]);
- }
- }
- getline(fi2, str);
- for (int i = 0; i < str.length(); i++)
- {
- if (str[i] == ' ')
- {
- if (i > 0)
- if (str[i - 1] != ' ')
- {
- array.push_back(stoi(vr));
- vr = "";
- }
- }
- else
- {
- vr.push_back(str[i]);
- }
- }
- Qsort(array, 0, array.size() - 1);
- for (int i = 0; i < array.size(); i++)
- {
- fo.write((char*)&array[i], sizeof(int));
- }
- }
- fi1.close(); fi2.close(); fo.close();
- ifstream fi3("output4.dat", ios::binary);
- if (!fi3.is_open())
- {
- cout << "Ошибка открытия файлов" << endl;
- }
- else
- {
- int x = 0;
- for (int i = 0; i < array.size(); i++) {
- fi3.read((char*)&x, sizeof(int));
- cout << x << " ";
- }
- }
- fi3.close();
- */
- //vector<int> array = { 2,6,0, 9,66,785,632,569,56,654,2,0 };
- ////Задание 6
- //odd_even(array, array.size());
- //cout << " Задание 6" << endl;
- //for (int i = 0; i < array.size(); i++)
- //{
- // cout << array[i] << " ";
- //}
- //cout << endl;
- //// Задание 7
- //cout << "Задание 7"<<max_elem(array, array.size())<< endl;
- ////Задание 8
- //cout <<"Задание 8: "<< rep(array,array.size()) << endl;
- }
Add Comment
Please, Sign In to add comment