Advertisement
MargaritaOwl

Vector

Jun 1st, 2016
70
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 <locale>
  3. using namespace std;
  4.  
  5.  
  6. void bubble(double *mas, int st, int n) // по возрастанию
  7. {
  8.     for (int i = n - 1; i >= st; i--)
  9.         for (int j = st; j<i; j++)
  10.             if (mas[j] > mas[j + 1])
  11.             {
  12.                 double tmp = mas[j];
  13.                 mas[j] = mas[j + 1];
  14.                 mas[j + 1] = tmp;
  15.             }
  16. }
  17.  
  18. void bubble_desc(double *mas, int n) //сортировка пузырьком по убыванию
  19. {
  20.     for (int i = n - 1; i >= 0; i--)
  21.         for (int j = 0; j<i; j++)
  22.             if (mas[j] < mas[j + 1])
  23.             {
  24.                 double tmp = mas[j];
  25.                 mas[j] = mas[j + 1];
  26.                 mas[j + 1] = tmp;
  27.             }
  28. }
  29.  
  30. void Shell_desc(double *mas, int n) //сортировка Шелла по убыванию
  31. {
  32.     int d, i, j;
  33.     d = n;//шаг
  34.     d = d / 2;
  35.     while (d>0)
  36.     {
  37.         for (i = 0; i< (n - d); i++)
  38.         {
  39.             j = i;
  40.             while (j >= 0 && fabs(mas[j])<fabs(mas[j + d]))
  41.             {
  42.                 double count = mas[j];
  43.                 mas[j] = mas[j + d];
  44.                 mas[j + d] = count;
  45.                 j--;
  46.             }
  47.         }
  48.         d = d / 2;
  49.     }
  50. }
  51.  
  52. void outarray(double *array, int size)
  53. {
  54.     for (int i = 0; i < size; i++)
  55.         cout << array[i] << " ";
  56.     cout << endl;
  57. }
  58.  
  59.  
  60. void input(double *array, int size, bool &falseinput)
  61. {
  62.     for (int i = 0; i < size; i++)
  63.     {
  64.         cin >> array[i];
  65.         if (!cin)
  66.         {
  67.             cout << "Вы ввели неправильное число." << endl;
  68.             falseinput = true;
  69.             break;
  70.         }
  71.     }
  72.     cout << endl;
  73. }
  74.  
  75.  
  76. void vectorC(double *arrayA, double *arrayB, int sizeA, int sizeB)
  77. {
  78.     const int size_const = 10000;
  79.     double arrayC[size_const];
  80.     int i, j;
  81.     for (i = 0, j = 0; (i < sizeA) || (i < sizeB); i++)
  82.     {
  83.         if (i < sizeA)
  84.         {
  85.             if ((int)arrayA[i] % 2 == 0)
  86.             {
  87.                 arrayC[j] = arrayA[i];
  88.                 j++;
  89.             }
  90.         }
  91.  
  92.         if (i < sizeB)
  93.         {
  94.             if ((int)arrayB[i] % 2 == 0)
  95.             {
  96.                 arrayC[j] = arrayB[i];
  97.                 j++;
  98.             }
  99.         }
  100.     }
  101.  
  102.     bubble_desc(arrayC, j);
  103.     int st = j;
  104.     for (i = 0; (i < sizeA) || (i < sizeB); i++)
  105.     {
  106.         if (i < sizeA)
  107.         {
  108.             if ((int)arrayA[i] % 2 != 0)
  109.             {
  110.                 arrayC[j] = arrayA[i];
  111.                 j++;
  112.             }
  113.         }
  114.  
  115.         if (i < sizeB)
  116.         {
  117.             if ((int)arrayB[i] % 2 != 0)
  118.             {
  119.                 arrayC[j] = arrayB[i];
  120.                 j++;
  121.             }
  122.         }
  123.     }
  124.  
  125.     bubble(arrayC, st, j);
  126.  
  127.     cout << "Задание №1: " << endl;
  128.     cout << "Вектор C :" << endl;
  129.     outarray(arrayC, j);
  130. }
  131.  
  132. void vectorA(double *array, int size)
  133. {
  134.     int min = 0, max = 0;
  135.  
  136.     for (int i = 1; i < size; i++)
  137.     {
  138.         if (fabs(array[i]) < fabs(array[min]))
  139.             min = i;
  140.  
  141.         if (fabs(array[i]) > fabs(array[max]))
  142.             max = i;
  143.     }
  144.  
  145.     if (min > max)
  146.     {
  147.         int tmp = min;
  148.         min = max;
  149.         max = tmp;
  150.     }
  151.  
  152.     double dob = 1;
  153.     while (min <= max)
  154.     {
  155.         dob *= array[min];
  156.         min++;
  157.     }
  158.  
  159.     cout << "Задание №2:" << endl;
  160.     cout << "Произведение элементов вектора А (в промежутке) = " << dob << endl;
  161. }
  162.  
  163.  
  164. int main(int argc, char *argv[])
  165. {
  166.     setlocale(LC_ALL, "rus");
  167.  
  168.     const int size_const = 10000;
  169.  
  170.     double arrayA[size_const];
  171.     double arrayB[size_const];
  172.     int sizeA, sizeB;
  173.     bool falseinput = false;
  174.  
  175.     if (argc == 1)
  176.     {
  177.         cout << "Вы не ввели размеры массивов в командной строке." << endl;
  178.         return 1;
  179.     }
  180.     else
  181.         if (argc == 2)
  182.         {
  183.             cout << "Вы не ввели размер второго массива в командной строке." << endl;
  184.             return 1;
  185.         }
  186.  
  187.     sizeA = atoi(argv[1]);
  188.     sizeB = atoi(argv[2]);
  189.  
  190.     input(arrayA, sizeA, falseinput);
  191.     input(arrayB, sizeB, falseinput);
  192.     if (falseinput)
  193.         return 1;
  194.  
  195.     cout << "Вектор А :" << endl;
  196.     outarray(arrayA, sizeA);
  197.     cout << "Вектор B :" << endl;
  198.     outarray(arrayB, sizeB);
  199.  
  200.     vectorC(arrayA, arrayB, sizeA, sizeB);
  201.  
  202.     vectorA(arrayA, sizeA);
  203.  
  204.     cout << "Задание №3:" << endl;
  205.     cout << "вектор А :" << endl;
  206.     Shell_desc(arrayA, sizeA);
  207.     outarray(arrayA, sizeA);
  208.     cout << "вектор B :" << endl;
  209.     Shell_desc(arrayB, sizeB);
  210.     outarray(arrayB, sizeB);
  211.  
  212.     system("pause");
  213.     return 0;
  214. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement