Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- using namespace std;
- void outputTask() {
- cout << "Эта программа сортирует заданный массив чисел методом подсчета." << endl;
- }
- string inputPath() {
- string path;
- bool isNotCorrect;
- do {
- isNotCorrect = false;
- cin >> path;
- ifstream fin(path);
- if (fin.is_open() and (size(path) > 4) and ((path[size(path) - 1] == 't') and (path[size(path) - 2] == 'x') and (path[size(path) - 3] == 't') and (path[size(path) - 4] == '.'))) {
- cout << "Файл успешно открыт!" << endl;
- }
- else {
- cout << "Ошибка открытия файла!" << endl;
- isNotCorrect = true;
- }
- fin.close();
- } while (isNotCorrect);
- return path;
- }
- void outputArray(int* array, int size) {
- cout << "Введенный массив: " << endl;
- for (int i = 0; i < size; i++) {
- cout << array[i];
- cout << " ";
- }
- cout << endl;
- }
- void sortArray(int*& array, int size){
- int max_value = array[0];
- for (int i = 1; i < size; i++) {
- if (array[i] > max_value) {
- max_value = array[i];
- }
- }
- int* count = new int[max_value + 1];
- for (int i = 0; i < size; i++) {
- count[array[i]]++;
- }
- int index = 0;
- for (int i = 0; i <= max_value; i++) {
- while (count[i] > 0) {
- array[index++] = i;
- count[i]--;
- }
- }
- delete[] count;
- }
- int* outputAnswer(int* array, int size) {
- string path;
- cout << "Отсортированный массив: " << endl;
- for (int i = 0; i < (size); i++) {
- cout << array[i] << " " ;
- }
- cout << endl;
- cout << "Введите путь файла для вывода: " << endl;
- path = inputPath();
- ofstream fout(path);
- fout << "Отсортированный массив: " << endl;
- for (int i = 0; i < (size); i++) {
- fout << array[i] << " " ;
- }
- fout.close();
- cout << "Данные успешно записаны в файл." << endl;
- return 0;
- }
- int inputSizeFromConsole() {
- int size;
- bool isNotCorrect;
- do {
- cout << "Введите размер массива: " << endl;
- isNotCorrect = false;
- cin >> size;
- if (cin.fail() or (size < 0)) {
- isNotCorrect = true;
- cout << "Ошибка ввода!" << endl;
- cin.clear();
- while (cin.get() != '\n');
- }
- } while (isNotCorrect);
- return size;
- }
- void fillArrayFromConsole(int*& array, int size) {
- bool isNotCorrect;
- for (int i = 0; i < size; i++) {
- do {
- isNotCorrect = false;
- cout << "Введите [" << i + 1 << "] элемент массива от 0 до 1000: " << endl;
- cin >> array[i];
- if (cin.fail() or (array[i] > 1000) or (array[i] < 0)) {
- cout << "Ошибка ввода! Повторите попытку." << endl;
- isNotCorrect = true;
- cin.clear();
- while (cin.get() != '\n');
- }
- } while (isNotCorrect);
- }
- }
- int* inputFromConsole(int& size) {
- int* array;
- size = inputSizeFromConsole();
- array = new int[size];
- fillArrayFromConsole(array, size);
- outputArray(array, size);
- return array;
- }
- void outputSize(int size) {
- cout << "Введенный размер с файла: " << size << endl;
- }
- void inputSizeFromFile(string path, int& size) {
- cout << "Считывание размера..." << endl;
- ifstream fin(path);
- fin >> size;
- if (fin.fail() or (size < 0)) {
- cout << "Ошибка ввода размера! Введите размер с клавиатуры." << endl;
- size = inputSizeFromConsole();
- }
- else {
- outputSize(size);
- }
- fin.close();
- }
- int exceptionRead(int i) {
- bool isNotCorrect;
- int num;
- do {
- isNotCorrect = false;
- cout << "Введите [" << i + 1 << "] элемент массива от 0 до 1000: " << endl;
- cin >> num;
- if (cin.fail() or (num > 1000)) {
- cout << "Ошибка ввода!" << endl;
- isNotCorrect = true;
- cin.clear();
- while (cin.get() != '\n');
- }
- } while (isNotCorrect);
- return num;
- }
- void inputArrayFromFile(string path, int size, int*& array) {
- string skipLine;
- cout << "Ввод массива.. " << endl;
- ifstream fin(path);
- getline(fin, skipLine);
- for (int i = 0; i < size; i++) {
- fin >> array[i];
- if (fin.fail() or (array[i] > 1000) or (array[i] < 0)) {
- cout << "Ошибка ввода элемента с индексом [" << i + 1 << "]. Введите этот элемент с клавиатуры. " << endl;
- fin.clear();
- fin.ignore(numeric_limits<streamsize>::max(), ' ');
- array[i] = exceptionRead(i);
- }
- }
- fin.close();
- }
- int* inputFromFile(int& size) {
- string path;
- int* array;
- cout << "При вводе из файла учтите, что на первой строке написан размер массива, а далее - сам массив с новой строки." << endl;
- cout << "Введите путь файла с данными: " << endl;
- path = inputPath();
- inputSizeFromFile(path, size);
- array = new int[size];
- inputArrayFromFile(path, size, array);
- outputArray(array, size);
- return array;
- }
- bool isFromFile() {
- bool isNotCorrect;
- int chooseNum;
- do {
- isNotCorrect = false;
- cout << "Выберите, откуда вводить данные: 1, если с файла; 0, если с консоли" << endl;
- cin >> chooseNum;
- if (cin.fail() or ((chooseNum != 1) and (chooseNum != 0))) {
- cout << "Ошибка ввода!" << endl;
- isNotCorrect = true;
- cin.clear();
- while (cin.get() != '\n');
- }
- } while (isNotCorrect);
- return (chooseNum == 1);
- }
- int* chooseInput(int& size) {
- int* array;
- if (isFromFile()) {
- array = inputFromFile(size);
- }
- else {
- array = inputFromConsole(size);
- }
- return array;
- }
- int main() {
- system("chcp 1251");
- int* array;
- int size;
- outputTask();
- array = chooseInput(size);
- sortArray(array, size);
- outputAnswer(array, size);
- delete[] array;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement