Advertisement
QwarkDev

не стыдная версия

Sep 9th, 2020
1,986
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.81 KB | None | 0 0
  1.  
  2. #include <iostream>
  3. #include <fstream>
  4. #include <ctime>
  5.  
  6. void SetBit(int *arr, std::size_t length, int number)
  7. {
  8.     std::size_t bytePos = number / 32;
  9.     std::size_t bitPos  = number % 32;
  10.  
  11.     arr[bytePos] |= (1 << bitPos);
  12. }
  13.  
  14. std::size_t ReadMax(std::string path)
  15. {
  16.     std::ifstream input(path);
  17.  
  18.     std::size_t max    = 0;
  19.     std::size_t buffer = 0;
  20.  
  21.     if (!input.is_open()) { std::cerr << "ReadMax(..): File not found!" << std::endl; return 0; }
  22.  
  23.     while (!input.eof())
  24.     {
  25.         input >> buffer;
  26.  
  27.         if (buffer > max) max = buffer;
  28.     }
  29.  
  30.     input.close();
  31.  
  32.     return max;
  33. }
  34.  
  35. void ReadUnsorted(std::string path, int *arr, std::size_t length)
  36. {
  37.     std::ifstream input(path);
  38.  
  39.     if (!input.is_open()) { std::cerr << "ReadUnsorted(..): File not found!" << std::endl; return; }
  40.  
  41.     int buffer = 0;
  42.  
  43.     while (!input.eof())
  44.     {
  45.         input >> buffer;
  46.         SetBit(arr, length, buffer);
  47.     }
  48.  
  49.     input.close();
  50. }
  51.  
  52. void WriteSorted(std::string path, int *arr, std::size_t length)
  53. {
  54.     std::ofstream output(path);
  55.    
  56.     if (!output.is_open()) { std::cerr << "WriteSorted(..): File not created!" << std::endl; return; }
  57.  
  58.     for (size_t i = 0; i < length; i++)
  59.     {
  60.         for (size_t j = 0;  j < 32;  j++)
  61.         {
  62.             if (arr[i] & (1 << j)) output << 32 * i + j << " ";
  63.         }
  64.     }
  65.  
  66.     output.close();
  67. }
  68.  
  69. void SortFile(std::string path)
  70. {
  71.     std::cout << "Чтение файла..." << std::endl;
  72.  
  73.     std::size_t maxInt = ReadMax(path);
  74.     std::size_t length = maxInt / 32 + 1;
  75.  
  76.     int* arr = new int[length]();
  77.     ReadUnsorted(path, arr, length);
  78.  
  79.     std::cout << "Сортировка и запись файла..." << std::endl;
  80.     WriteSorted(path, arr, length);
  81. }
  82.  
  83. int main()
  84. {
  85.     setlocale(LC_ALL, "Russian");
  86.     std::string filename;
  87.  
  88.     std::cout << "Введите название файла: ";
  89.     std::cin >> filename;
  90.  
  91.     SortFile(filename);
  92. }
  93.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement