Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <ctime>
- void SetBit(int *arr, std::size_t length, int number)
- {
- std::size_t bytePos = number / 32;
- std::size_t bitPos = number % 32;
- arr[bytePos] |= (1 << bitPos);
- }
- std::size_t ReadMax(std::string path)
- {
- std::ifstream input(path);
- std::size_t max = 0;
- std::size_t buffer = 0;
- if (!input.is_open()) { std::cerr << "ReadMax(..): File not found!" << std::endl; return 0; }
- while (!input.eof())
- {
- input >> buffer;
- if (buffer > max) max = buffer;
- }
- input.close();
- return max;
- }
- void ReadUnsorted(std::string path, int *arr, std::size_t length)
- {
- std::ifstream input(path);
- if (!input.is_open()) { std::cerr << "ReadUnsorted(..): File not found!" << std::endl; return; }
- int buffer = 0;
- while (!input.eof())
- {
- input >> buffer;
- SetBit(arr, length, buffer);
- }
- input.close();
- }
- void WriteSorted(std::string path, int *arr, std::size_t length)
- {
- std::ofstream output(path);
- if (!output.is_open()) { std::cerr << "WriteSorted(..): File not created!" << std::endl; return; }
- for (size_t i = 0; i < length; i++)
- {
- for (size_t j = 0; j < 32; j++)
- {
- if (arr[i] & (1 << j)) output << 32 * i + j << " ";
- }
- }
- output.close();
- }
- void SortFile(std::string path)
- {
- std::cout << "Чтение файла..." << std::endl;
- std::size_t maxInt = ReadMax(path);
- std::size_t length = maxInt / 32 + 1;
- int* arr = new int[length]();
- ReadUnsorted(path, arr, length);
- std::cout << "Сортировка и запись файла..." << std::endl;
- WriteSorted(path, arr, length);
- }
- int main()
- {
- setlocale(LC_ALL, "Russian");
- std::string filename;
- std::cout << "Введите название файла: ";
- std::cin >> filename;
- SortFile(filename);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement