PhotoShaman

Laba20

Apr 6th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.91 KB | None | 0 0
  1. #include <iostream>
  2. #include <clocale>
  3. #include <windows.h>
  4. #include <conio.h>
  5.  
  6. using namespace std;
  7.  
  8. template <typename Type>
  9. Type MagicInput(Type input) // Ввод с проверкой
  10. {
  11.     while (!(cin >> input) || (cin.peek() != '\n'))
  12.     {
  13.         cin.clear(); while (cin.get() != '\n');
  14.         cout << "Ошибка ввода. Попробуйте еще раз.\n";
  15.     }
  16.     return input;
  17. }
  18.  
  19. void SetLucidaFont()
  20. {
  21.     HANDLE hCon = CreateConsoleScreenBuffer(GENERIC_READ | GENERIC_WRITE, 0, NULL, CONSOLE_TEXTMODE_BUFFER, NULL);
  22.     if (hCon != INVALID_HANDLE_VALUE) {
  23.         CONSOLE_FONT_INFOEX cfi;
  24.         cfi.cbSize = sizeof(CONSOLE_FONT_INFOEX);
  25.         cfi.nFont = 0;
  26.         cfi.dwFontSize.X = 0;
  27.         cfi.dwFontSize.Y = 12;
  28.         cfi.FontFamily = FF_DONTCARE;
  29.         cfi.FontWeight = 400;
  30.         wcscpy_s(cfi.FaceName, L"Lucida Console");
  31.         SetCurrentConsoleFontEx(hCon, FALSE, &cfi);
  32.     }
  33. }
  34.  
  35. template<typename Type>
  36. void quickSort(Type* arr, int left, int right) {
  37.     Type tmp, pivot = arr[(left + right) / 2];
  38.     int i = left, j = right;
  39.     while (i <= j) {
  40.         while (arr[i] < pivot) i++;
  41.         while (arr[j] > pivot) j--;
  42.         if (i <= j) {
  43.             tmp = arr[i]; arr[i] = arr[j]; arr[j] = tmp;
  44.             i++; j--;
  45.         }
  46.     }
  47.     if (left < j) quickSort(arr, left, j);
  48.     if (i < right) quickSort(arr, i, right);
  49. }
  50.  
  51. template <typename Type>
  52. class Array
  53. {
  54. private:
  55.     Type* array;
  56.     int size;
  57. public:
  58.     Array(int sizeIn = 10)
  59.     {
  60.         size = sizeIn > 0 ? sizeIn : 10;
  61.         array = new Type[size];
  62.         for (int i = 0; i < size; i++) array[i] = 0;
  63.     }
  64.     void InputArray();
  65.     Type FindMin();
  66.     void ArrayOutput();
  67.     void ArraySort() { quickSort(array, 0, size - 1); }
  68. };
  69.  
  70. template <typename Type>
  71. void Array<Type>::InputArray()
  72. {
  73.     system("cls");
  74.     for (int i = 0; i < size; i++)
  75.     {
  76.         cout << "Введите " << i + 1 << " эллемент массива: ";
  77.         array[i] = MagicInput((Type)0);
  78.     }
  79. }
  80.  
  81. template <typename Type>
  82. Type Array<Type>::FindMin()
  83. {
  84.     Type min = array[0];
  85.     for (int i = 0; i < size; i++)
  86.         if (min > array[i])
  87.             min = array[i];
  88.     cout << endl << "Минимум: " << min << endl;
  89.     system("pause");
  90.     return min;
  91. }
  92.  
  93. template <typename Type>
  94. void Array<Type>::ArrayOutput()
  95. {
  96.     cout << endl;
  97.     for (int i = 0; i < size; i++)
  98.         cout << array[i] << "  ";
  99.     cout << endl; system("pause");
  100. }
  101.  
  102. void Menu()
  103. {
  104.     system("cls");
  105.     cout << "---Выберите опцию: ";
  106.     cout << endl << "--------------Int--------------";
  107.     cout << endl << "1. Ввести все элементы массива";
  108.     cout << endl << "2. Вывод минимального элемента";
  109.     cout << endl << "3. Вывод элементов массива";
  110.     cout << endl << "4. Сортировка массива";
  111.     cout << endl << "-------------Double------------";
  112.     cout << endl << "5. Ввести все элементы массива";
  113.     cout << endl << "6. Вывод минимального элемента";
  114.     cout << endl << "7. Вывод элементов массива";
  115.     cout << endl << "8. Сортировка массива";
  116.     cout << endl << "-------------------------------";
  117.     cout << endl << "ESC. Выход";
  118. }
  119.  
  120. int main()
  121. {
  122.     system("mode con cols=80 lines=30");
  123.     SetLucidaFont();
  124.     SetConsoleCP(1251);
  125.     SetConsoleOutputCP(1251);
  126.  
  127.     cout << "Введите размерность массива(Int): ";
  128.     Array<int> arrayInt(MagicInput((int)0));
  129.     cout << "Введите размерность массива(Double): ";
  130.     Array<double> arrayDouble(MagicInput((int)0));
  131.  
  132.     while (true)
  133.     {
  134.         Menu();
  135.         switch (_getch())
  136.         {
  137.         case 49/*-1-*/: arrayInt.InputArray(); break;
  138.         case 50/*-2-*/: arrayInt.FindMin(); break;
  139.         case 51/*-3-*/: arrayInt.ArrayOutput(); break;
  140.         case 52/*-4-*/: arrayInt.ArraySort(); break;
  141.         case 53/*-5-*/: arrayDouble.InputArray(); break;
  142.         case 54/*-6-*/: arrayDouble.FindMin(); break;
  143.         case 55/*-7-*/: arrayDouble.ArrayOutput(); break;
  144.         case 56/*-8-*/: arrayDouble.ArraySort(); break;
  145.         case 27/*ESC*/: return 0;
  146.         }
  147.     }
  148. }
Advertisement
Add Comment
Please, Sign In to add comment