Advertisement
selebry

final v quinke 1 siaod 2 part

Sep 12th, 2022
986
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.83 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <bitset>
  4. #include <vector>
  5. #include <chrono>
  6. #include <ctime>
  7. #include <random>
  8. using namespace std;
  9. class Stopwatch {
  10.     chrono::steady_clock::time_point point;
  11.     chrono::nanoseconds dim;
  12. public:
  13.     void start_countdown() {
  14.         point = chrono::steady_clock::now();
  15.     }
  16.     double get_milliseconds() {
  17.         dim = chrono::duration_cast<chrono::nanoseconds>(chrono::steady_clock::now() - point);
  18.         return (double)dim.count() / 1'000'000;
  19.     }
  20. };
  21. const int N = 10000000;
  22. void outSort(fstream& f, bitset<N>* set = nullptr, unsigned int* arr=nullptr);
  23. void sorting_by_bitset();
  24. void sorting_by_bitarray();
  25. void fill_storage_random(int n, bitset<N>* set = nullptr, unsigned int* arr = nullptr);
  26. int main() {
  27.     srand(time(NULL));
  28.     setlocale(LC_ALL, "rus");
  29.     int answer1 = 100;
  30.     while (answer1 != 0) {
  31.         system("cls");
  32.         cout << "Практическая работа №1 ИКБО-11-21 Резван М.Б. Вариант 28" << endl << endl;
  33.         cout << "Задание 1" << endl;
  34.         cout << "Меню\n";
  35.         cout << "1) Битовая сортировка с помощью bitset\n";
  36.         cout << "2) Битовая сортировка с помощью битового массива\n";
  37.         cout << "0) Выход\n";
  38.         cout << "Ваш выбор: ";
  39.         cin >> answer1;
  40.         system("cls");
  41.         cout << "Практическая работа №1 ИКБО-11-21 Резван М.Б. Вариант 28" << endl << endl;
  42.         switch (answer1)
  43.         {
  44.         case 1:
  45.             sorting_by_bitset();
  46.             system("pause");
  47.             break;
  48.         case 2:
  49.             sorting_by_bitarray();
  50.             system("pause");
  51.             break;
  52.         default:
  53.             break;
  54.         }
  55.     }
  56. }
  57.  
  58. void fill_storage_random(int n, bitset<N>* set , unsigned int* arr ) {
  59.     random_device rd;
  60.     default_random_engine rng(rd());
  61.     uniform_int_distribution<unsigned int> dist(1'000'000, 9'999'999);
  62.     if (set) {
  63.         for (int i = 0; i < n; i++)
  64.             set->set(dist(rng));
  65.  
  66.     }
  67.     else {
  68.         unsigned int t;
  69.         for (int i = 0; i < n; i++) {
  70.             t = dist(rng);
  71.             arr[t / 32] |= (1 << t % 32);
  72.         }
  73.     }
  74. }
  75.  
  76. void sorting_by_bitset() {
  77.     Stopwatch s;
  78.     bitset<N>* arr = new bitset<N>{ 0 };
  79.     fstream f;
  80.     unsigned int t;
  81.     cout << "Вид входных данных:"
  82.         << "\n1.Ввод с клавиатуры"
  83.         << "\n2.Заполнение случайными числами"
  84.         << "\n3.Чтение из файла";
  85.     cout << "\nВаш ответ: "; cin >> t;
  86.     if (t == 1) {
  87.         int n;
  88.         cout << "Введите кол-во чисел:";
  89.         cin >> n;
  90.         s.start_countdown();
  91.         for (int i = 0; i < n; i++) {
  92.             cin >> t;
  93.             arr->set(t);
  94.         }
  95.     }
  96.     else if (t == 2) {
  97.         int n;
  98.         cout << "Введите кол-во чисел:";
  99.         cin >> n;
  100.         s.start_countdown();
  101.         fill_storage_random(n, arr);
  102.     }
  103.     else if (t == 3) {
  104.         string name;
  105.         cout << "Введите название файла (вместе с расширением): "; cin >> name;
  106.         f.open(name, ios::in);
  107.         s.start_countdown();
  108.         while (f >> t) arr->set(t);
  109.         f.close();
  110.     }
  111.     else {
  112.         cout << "Такого значение нет!(";
  113.         return;
  114.     }
  115.     outSort(f, arr);
  116.     cout << "\nВремя выполнения (bitset): " << s.get_milliseconds() << "ms\n";
  117.     delete arr;
  118. }
  119. void sorting_by_bitarray() {
  120.     Stopwatch s;
  121.     fstream f;
  122.     unsigned int t = 0, * arr = new unsigned int[N / 32]{ 0 };
  123.     cout << "Вид входных данных:"
  124.         << "\n1.Ввод с клавиатуры"
  125.         << "\n2.Заполнение случайными числами"
  126.         << "\n3.Чтение из файла";
  127.     cout << "\nВаш ответ: "; cin >> t;
  128.     if (t == 1) {
  129.         int n;
  130.         cout << "Введите кол-во чисел:";
  131.         cin >> n;
  132.         s.start_countdown();
  133.         for (int i = 0; i < n; i++) {
  134.             cin >> t;
  135.             arr[t / 32] |= (1 << t % 32);
  136.         }
  137.         //cout << "fill bit: " << bitset<32>(arr[38580]);
  138.     }
  139.     else if (t == 2) {
  140.         int n;
  141.         cout << "Введите кол-во чисел:";
  142.         cin >> n;
  143.         s.start_countdown();
  144.         fill_storage_random(n, nullptr, arr);
  145.     }
  146.     else if (t == 3) {
  147.         string name;
  148.         cout << "Введите название файла (вместе с расширением): "; cin >> name;
  149.         f.open(name, ios::in);
  150.         s.start_countdown();
  151.         while (f >> t) arr[t / 32] |= (1 << t % 32);
  152.         f.close();
  153.     }
  154.     else {
  155.         cout << "Такого значение нет!(";
  156.         return;
  157.     }
  158.     outSort(f, nullptr, arr);
  159.     cout << "\nВремя выполнения (bitarray): " << s.get_milliseconds() << "ms\n";
  160.  
  161.     delete[] arr;
  162. }
  163. void outSort(fstream& f, bitset<N>* set, unsigned int* arr) {
  164.     if(set){
  165.         f.open("sortS.txt", ios::out);
  166.         for (int i = 0; i < N; i++)
  167.             if (set->test(i) == 1) {
  168.                 f << i << '\n';
  169.                 cout << i << ' ';
  170.             }
  171.         f.close();
  172.     }
  173.     else {
  174.         f.open("sortA.txt", ios::out);
  175.         for (int i = 0; i < N; i++)
  176.             if ((arr[i / 32] & (1 << i % 32)) != 0) {
  177.                 f << i << '\n';
  178.                 cout << i << ' ';
  179.             }
  180.         f.close();
  181.     }
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement