Advertisement
Guest User

Laba_1_4_2

a guest
Oct 28th, 2016
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.00 KB | None | 0 0
  1. #include <clocale>
  2. #include <cstdio>
  3. #include <iostream>
  4. #include <cstdlib>
  5. #include <memory.h>
  6. #include <ctime>
  7.  
  8. #define Char_Height 50
  9. #define Char_Width 50
  10.  
  11. double** Input(int N, int M);
  12. void Entry(double **a, int N, int M);
  13. bool Check(char *char_num);
  14. void Rand(double **a, int N, int M);
  15. void Process(double **a, int N, int M);
  16. void Result(double **a, int N, int M);
  17. void Delete(double **a, int N);
  18. int Height();
  19. int Width();
  20.  
  21. using namespace std;
  22.  
  23. int main()
  24. {
  25.     setlocale(LC_ALL, "Russian");
  26.     setlocale(LC_ALL, "rus");
  27.  
  28.     srand(time(NULL));
  29.  
  30.     double **a = NULL;
  31.     int height = 0, width = 0;
  32.     int menu = 0, menu1 = 0, menu2 = 0;
  33.     int wasEntered = 0;
  34.     int wasInput = 1;
  35.  
  36.     cout << "Программа для нахождения минимального значения в двумерном массиве\n\n";
  37.  
  38.     do
  39.     {
  40.         cout << "1. Ввод данных\n";
  41.         cout << "2. Обработка данных\n";
  42.         cout << "3. Вывод результата на экран\n";
  43.         cout << "---------------\n";
  44.         cout << "0. Выход\n";
  45.  
  46.         cin >> menu;
  47.  
  48.         switch (menu)
  49.         {
  50.         case 1:
  51.             if (wasInput == 1)
  52.             {
  53.                 height = Height();
  54.                 width = Width();
  55.                 a = Input(height, width);
  56.  
  57.                 cout << "1. Введите значения вручную\n";
  58.                 cout <<"2. Введем случайные значения\n";
  59.  
  60.                 cin >> menu1;
  61.  
  62.                 switch (menu1)
  63.                 {
  64.                 case 1:
  65.                     Entry(a, height, width);
  66.                     break;
  67.                 case 2:
  68.                     Rand(a, height, width);
  69.                     break;
  70.                 default:
  71.                     cout << "\aНеверный пункт меню!\n";
  72.                     break;
  73.                 }
  74.                 cout << "\n";
  75.                 wasInput = 0;
  76.                 wasEntered = 1;
  77.             }
  78.             else
  79.             {
  80.                 Delete(a, height);
  81.                 cout << "\aПроизведена очистка массива!\nДля введения нового массива нажмите 1.\n\n";
  82.                 wasInput = 1;
  83.                 wasEntered = 0;
  84.             }
  85.             break;
  86.         case 2:
  87.             if (wasEntered == 1)
  88.                 Process(a, height, width);
  89.             else
  90.                 cout << "\aСначала нужно задать и ввести массив!\nВыберите пункт меню 1.\n\n";
  91.             break;
  92.         case 3:
  93.             if (wasEntered == 1)
  94.                 Result(a, height, width);
  95.             else
  96.                 cout << "\aСначала нужно задать и ввести массив!\nВыберите пункт меню 1.\n\n";
  97.             break;
  98.         case 0:
  99.             Delete(a, height);
  100.             break;
  101.         default:
  102.             cout << "\aНеверный пункт меню!\n";
  103.             break;
  104.         }
  105.     } while (menu != 0);
  106.  
  107.     return 0;
  108. }
  109.  
  110. int Height()
  111. {
  112.     int height_mass = 0;
  113.     char char_height[Char_Height] = { '0' };
  114.  
  115.     cout << "Количество строк массива: ";
  116.     cin >> char_height;
  117.     while (Check(char_height) == false || char_height[0] == '-')
  118.     {
  119.         cout << "\aНеверное значение!\nВведите новое значение: ";
  120.         cin >> char_height;
  121.     }
  122.     height_mass = atoi(char_height);
  123.  
  124.     return height_mass;
  125. }
  126.  
  127. int Width()
  128. {
  129.     int width_mass = 0;
  130.     char char_width[Char_Width] = { '0' };
  131.  
  132.     cout << "Колличество столбцов массива: ";
  133.     cin >> char_width;
  134.     while (Check(char_width) == false || char_width[0] == '-')
  135.     {
  136.         cout << "\aНеверное значение!\nВведите новое значение: ";
  137.         cin >> char_width;
  138.     }
  139.     width_mass = atoi(char_width);
  140.  
  141.     return width_mass;
  142. }
  143.  
  144. double** Input(int N, int M)
  145. {
  146.     double **a = new double*[N];
  147.     for (int i = 0; i < N; i++)
  148.         a[i] = new double[M];
  149.     return a;
  150. }
  151.  
  152. void Entry(double **a, int N, int M)
  153. {
  154.     char char_num[Char_Height];
  155.     for (int i = 0; i < N; i++)
  156.     {
  157.         for (int j = 0; j < M; j++)
  158.         {
  159.             printf("элемент [%d,%d]: ", i + 1, j + 1);
  160.             cin >> char_num;
  161.             while (Check(char_num) == false)
  162.             {
  163.                 cout << "\aВведено неверное значение!\n";
  164.                 printf("элемент [%d,%d]: ", i + 1, j + 1);
  165.                 cin >> char_num;
  166.             }
  167.             a[i][j] = atoi(char_num);
  168.         }
  169.     }
  170. }
  171.  
  172. bool Check(char *char_num)
  173. {
  174.     int i = 0;
  175.     if (char_num[0] == '-' && char_num[1] != '\0')
  176.         i++;
  177.     while (char_num[i] != '\0')
  178.     {
  179.         if (char_num[i] < '0' || char_num[i] > '9')
  180.             return false;
  181.         i++;
  182.     }
  183.     return true;
  184. }
  185.  
  186. void Rand(double **a, int N, int M)
  187. {
  188.     for (int i = 0; i < N; i++)
  189.     {
  190.         for (int j = 0; j < M; j++)
  191.         {
  192.             a[i][j] = (1 + rand() % 100) / double((1 + rand() % 10));
  193.         }
  194.     }
  195. }
  196.  
  197. void Process(double **a, int N, int M)
  198. {
  199.     double temp = 0, temp1 = 0;
  200.     double min = a[0][0];
  201.     int num_height = 0, num_width = 0;
  202.     for (int i = 0; i < N; i++)
  203.     {
  204.         for (int j = 0; j < M; j++)
  205.         {
  206.             if (a[i][j] < min)
  207.             {
  208.                 min = a[i][j];
  209.                 num_height = i;
  210.                 num_width = j;
  211.             }
  212.         }
  213.     }
  214.     printf("\nМинимальное элемент матрицы: %.2f\n", min);
  215.     printf("Номер строки минимального элемента %d\nНомер столбца минимального элемента %d\n\n", num_height + 1, num_width + 1);
  216.  
  217.     if (min != a[0][0] && min != a[num_height][0])
  218.     {
  219.         for (int i = 0; i < N; i++)
  220.         {
  221.             temp = a[i][num_width];
  222.             a[i][num_width] = a[i][0];
  223.             a[i][0] = temp;
  224.         }
  225.         min = a[num_height][0];
  226.         cout << "\nПроизведена перестановка столбцов.\n\n";
  227.     }
  228.     else
  229.         cout << "\nПерестановка столбцов не требуется, так как элемент находится в 1 столбце\n\n";
  230.  
  231.     if (min != a[0][0] && min != a[0][num_width])
  232.     {
  233.         for (int j = 0; j < M; j++)
  234.         {
  235.             temp1 = a[num_height][j];
  236.             a[num_height][j] = a[0][j];
  237.             a[0][j] = temp1;
  238.         }
  239.         cout << "\nПроизведена перестановка строк.\n\n";
  240.     }
  241.     else
  242.         cout << "\nПерестановка строк не требуется, так как элемент находится в 1 строке\n\n";
  243. }
  244.  
  245. void Result(double **a, int N, int M)
  246. {
  247.     for (int i = 0; i < N; i++)
  248.     {
  249.         for (int j = 0; j < M; j++)
  250.         {
  251.             printf("%10.2f", a[i][j]);
  252.         }
  253.         cout << "\n";
  254.     }
  255. }
  256.  
  257. void Delete(double **a, int N)
  258. {
  259.     for (int i = 0; i < N; i++)
  260.     {
  261.         delete[] a[i];
  262.     }
  263.     delete[] a;
  264. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement