https://pastebin.com/TCkx0dnE
У меня есть вот такое начало. Тут реализация поразрядной сортировки, она читает элементы из файла, отсортирует по возрастанию и сохраняет в другой файл. Надо добавить операцию, сортировки по убыванию и выбор между возрастающей и убывающей.
Другие языки программирования и технологии
Как поправить программу? C++
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <iterator>
#include <conio.h>
template<typename Type>
void radixsort(std::vector<Type>& box) {
const auto size = box.size();
if (!size) return;
auto max_number = *std::max_element(box.cbegin(), box.cend());
Type exp = 1;
while (max_number / exp > 0) {
std::vector<Type> tmp_buffer(size, Type());
Type decimal_bucket[10] = { 0 };
for (auto i = 0U; i < size; ++i)
++decimal_bucket[box[i] / exp % 10];
for (auto i = 1; i < 10; ++i)
decimal_bucket[i] += decimal_bucket[i - 1];
for (int i = size - 1; i >= 0; --i)
tmp_buffer[--decimal_bucket[box[i] / exp % 10]] = box[i];
box = std::move(tmp_buffer);
exp *= 10;
std::cout << "\nPASS : ";
std::copy(box.cbegin(), box.cend(), std::ostream_iterator<Type>{ std::cout, " " });
}
}
int main() {
// В C++ каждому оператору new должен соответствовать свой delete.
// Заввязывайте с понтами или используйте умные указатели!
const auto input = new std::ifstream{ "1.txt" };
std::vector<int> box{ std::istream_iterator<int>{ *input },
std::istream_iterator<int>{}
};
input->close();
delete input;
radixsort(box);
char choice;
do {
std::cout << "\nTo sort in ascending order, press 1\nTo sort in descending order, press 2 ";
choice = _getch();
if (choice == '1')
std::sort(box.begin(), box.end(), std::less<>());
if (choice == '2')
std::sort(box.begin(), box.end(), std::greater<>());
_getch();
} while (choice != '1' && choice != '2');
std::ofstream output{ "2.txt", std::ios::trunc };
std::copy(box.cbegin(), box.cend(), std::ostream_iterator<int>{ output, " " });
output.close();
}
#include <fstream>
#include <algorithm>
#include <vector>
#include <iterator>
#include <conio.h>
template<typename Type>
void radixsort(std::vector<Type>& box) {
const auto size = box.size();
if (!size) return;
auto max_number = *std::max_element(box.cbegin(), box.cend());
Type exp = 1;
while (max_number / exp > 0) {
std::vector<Type> tmp_buffer(size, Type());
Type decimal_bucket[10] = { 0 };
for (auto i = 0U; i < size; ++i)
++decimal_bucket[box[i] / exp % 10];
for (auto i = 1; i < 10; ++i)
decimal_bucket[i] += decimal_bucket[i - 1];
for (int i = size - 1; i >= 0; --i)
tmp_buffer[--decimal_bucket[box[i] / exp % 10]] = box[i];
box = std::move(tmp_buffer);
exp *= 10;
std::cout << "\nPASS : ";
std::copy(box.cbegin(), box.cend(), std::ostream_iterator<Type>{ std::cout, " " });
}
}
int main() {
// В C++ каждому оператору new должен соответствовать свой delete.
// Заввязывайте с понтами или используйте умные указатели!
const auto input = new std::ifstream{ "1.txt" };
std::vector<int> box{ std::istream_iterator<int>{ *input },
std::istream_iterator<int>{}
};
input->close();
delete input;
radixsort(box);
char choice;
do {
std::cout << "\nTo sort in ascending order, press 1\nTo sort in descending order, press 2 ";
choice = _getch();
if (choice == '1')
std::sort(box.begin(), box.end(), std::less<>());
if (choice == '2')
std::sort(box.begin(), box.end(), std::greater<>());
_getch();
} while (choice != '1' && choice != '2');
std::ofstream output{ "2.txt", std::ios::trunc };
std::copy(box.cbegin(), box.cend(), std::ostream_iterator<int>{ output, " " });
output.close();
}
Рома Кирнисов
Ну вы даёте, Николай. Спасибо!
300 рублей будет стоить ответ
Рома Кирнисов
"Не имей 100 рублей, а имей 100 друзей"
Похожие вопросы
- Помогите исправить ошибку в программе (c++).
- написать программу C++
- Программа C++ Напишите программу которая переводит из десятичной в двоичную систему счисления (C++)
- Что не так с программой? C++
- Помогите объяснить программу C++
- Свои иконки на свои программы c++ или ассемблер
- На каком языке программирования писать гостиничную программу? C++, C#, PHP, Java?
- помогите сделать в программе c++
- Помогите с написанием программы C++
- Необходим код программы, C++