Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <windows.h>
- #include <vector>
- #include <algorithm>
- using namespace std;
- void sorting(vector<double>& double_v_array) {
- sort(double_v_array.begin(), double_v_array.end());
- }
- void sorting(vector<int>& int_v_array) {
- sort(int_v_array.begin(), int_v_array.end());
- }
- int main()
- {
- SetConsoleOutputCP(1251);
- SetConsoleCP(1251);
- vector <double> double_v_array{};
- vector <int> int_v_array{};
- int N{}, type{}, length{}, int_element{};
- double double_element{};
- while (true) {
- cout << "Введіть НОМЕР масиву, який бажаєте відсортувати " << endl << "1 - Цілочисельний" << endl << "2 - З плаваючою комою" << endl;
- cin >> type;
- if (cin.fail() || cin.peek() != '\n' || (type != 1 && type != 2)) {
- cin.clear();
- cin.ignore(32767, '\n');
- cout << "Номер масиву було введено неправильно. Спробуйте ще раз!" << endl;
- continue;
- }
- else {
- break;
- }
- }
- string words_array[10]{ "перший", "другий", "третій", "четвертий", "п'ятий", "шостий", "сьомий", "восьмий", "дев'ятий", "десятий" };
- while (true) {
- cout << "Введіть бажану кількість елементів у масиві (від 2 до 10): ";
- cin >> N;
- if (cin.fail() || cin.peek() != '\n' || N < 2 || N > 10) {
- cin.clear();
- cin.ignore(32767, '\n');
- cout << "Число було введено неправильно. Спробуйте ще раз!" << endl;
- continue;
- }
- else {
- break;
- }
- }
- for (int i = 0; i < N; i++) {
- while (true) {
- cout << "Введіть " << words_array[i] << " елемент масиву: ";
- if (type == 1) {
- cin >> int_element;
- int_v_array.push_back(int_element);
- }
- else if (type == 2) {
- cin >> double_element;
- double_v_array.push_back(double_element);
- }
- if (cin.fail()) {
- cin.clear();
- cin.ignore(32767, '\n');
- cout << "Число було введено неправильно. Спробуйте ще раз!" << endl;
- continue;
- }
- else {
- break;
- }
- }
- }
- if (type == 1) {
- sorting(int_v_array);
- cout << "Відсортований масив: ";
- length = size(int_v_array);
- for (int i = 0; i < length; i++) {
- cout << int_v_array[i] << " ";
- }
- }
- else if (type == 2) {
- sorting(double_v_array);
- cout << "Відсортований масив: ";
- length = size(double_v_array);
- for (int i = 0; i < length; i++) {
- cout << double_v_array[i] << " ";
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement