Advertisement
boyan16-z

Vector

Apr 30th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.71 KB | None | 0 0
  1. //Vector.cpp: определяет точку входа для консольного приложения.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <Windows.h>
  6. #include "cstdlib"
  7. #include "stdio.h"
  8.  
  9. class Vector {
  10. public:
  11.     Vector() {
  12.         m_pArray = NULL;
  13.         m_count = 0;
  14.     }
  15.  
  16.     Vector(unsigned int count) { //Заполнение случайными числами
  17.         m_pArray = new int[count];
  18.         m_count = count;
  19.         for (int i = 0; i < count; i++) m_pArray[i] = rand() % 5;
  20.     }
  21.  
  22.     Vector(int *pArray, unsigned int count) //Копирование %1
  23.     {
  24.         m_pArray = NULL;
  25.         m_count = 0;
  26.         Assign(pArray, count);
  27.     }
  28.  
  29.     Vector(const Vector &bit) { //Копирование %2
  30.         m_pArray = NULL;
  31.         m_count = 0;
  32.         Assign(bit.m_pArray, bit.m_count);
  33.     }
  34.  
  35.     Vector & operator = (const Vector &bit) { //Копирование %3 через перезагрузку операции присваивания
  36.         Clear();
  37.         Assign(bit.m_pArray, bit.m_count);
  38.         return *this;
  39.     }
  40.  
  41.     void Sort() { //Сортировка
  42.         int temp;
  43.         for (int i = 0; i < m_count - 1; i++)
  44.             for (int j = 0; j < m_count - i - 1; j++)
  45.                 if (m_pArray[j] > m_pArray[j + 1]) {
  46.             temp = m_pArray[j];
  47.             m_pArray[j] = m_pArray[j + 1];
  48.             m_pArray[j + 1] = temp;
  49.                 }
  50.     }
  51.  
  52.     float Аverage() { //Среднее арифметическое
  53.         float mid;
  54.         for (int i = 0; i < m_count; i++) mid = +m_pArray[i];
  55.         return (mid / m_count);
  56.     }
  57.     void Show() {
  58.         for (int i = 0; i < m_count; i++) printf("%d ", m_pArray[i]);
  59.         printf("\n");
  60.     }
  61.  
  62.     void Assign(int *pArray, unsigned int count) { //Метод копирования содержимого
  63.         m_count = count;
  64.         m_pArray = new int[count];
  65.         for (int i = 0; i < count; i++) m_pArray[i] = pArray[i];
  66.     }
  67.  
  68.     void Repeat() {
  69.         for (int i = 0; i < m_count; i++) {
  70.             for (int j = i + 1; j < m_count; j++) {
  71.                 if (i != j) {
  72.                     if (m_pArray[i] == m_pArray[j]) {
  73.                         for (int g = j; g < m_count - 1; g++) {
  74.                             m_pArray[g] = m_pArray[g + 1];
  75.                         }
  76.                         m_count--;
  77.                     }
  78.                     if (m_pArray[i] == m_pArray[j]) j--;
  79.                 }
  80.             }
  81.         }
  82.     }
  83.  
  84.  
  85.  
  86.     void Clear() { //Очистка
  87.         if (m_pArray != NULL && m_count > 0) {
  88.             delete[] m_pArray;
  89.             m_pArray = NULL;
  90.             m_count = 0;
  91.         }
  92.     }
  93.  
  94.     ~Vector() {
  95.         Clear();
  96.     }
  97.  
  98. private:
  99.     int *m_pArray;
  100.     int m_count;
  101. };
  102.  
  103.  
  104. int _tmain(int argc, _TCHAR* argv[])
  105. {
  106.     int n;
  107.     float s = 0;
  108.     scanf("%d", &n);
  109.     Vector x(n);
  110.     Vector y(n);
  111.     printf("Vector x: ");
  112.     x.Show();
  113.     printf("Vector y: ");
  114.     y.Show();
  115.     printf("Vector z: ");
  116.     Vector z(x);
  117.     z.Show();
  118.     s = x.Аverage();
  119.     printf("The arithmetic mean: %0.2f\n", s);
  120.     printf("Sort x: ");
  121.     x.Sort();
  122.     x.Show();
  123.     x.Repeat();
  124.     x.Show();
  125.     y.Repeat();
  126.     y.Show();
  127.     return 0;
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement