Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Vector.cpp: определяет точку входа для консольного приложения.
- //
- #include "stdafx.h"
- #include <Windows.h>
- #include "cstdlib"
- #include "stdio.h"
- class Vector {
- public:
- Vector() {
- m_pArray = NULL;
- m_count = 0;
- }
- Vector(unsigned int count) { //Заполнение случайными числами
- m_pArray = new int[count];
- m_count = count;
- for (int i = 0; i < count; i++) m_pArray[i] = rand() % 5;
- }
- Vector(int *pArray, unsigned int count) //Копирование %1
- {
- m_pArray = NULL;
- m_count = 0;
- Assign(pArray, count);
- }
- Vector(const Vector &bit) { //Копирование %2
- m_pArray = NULL;
- m_count = 0;
- Assign(bit.m_pArray, bit.m_count);
- }
- Vector & operator = (const Vector &bit) { //Копирование %3 через перезагрузку операции присваивания
- Clear();
- Assign(bit.m_pArray, bit.m_count);
- return *this;
- }
- void Sort() { //Сортировка
- int temp;
- for (int i = 0; i < m_count - 1; i++)
- for (int j = 0; j < m_count - i - 1; j++)
- if (m_pArray[j] > m_pArray[j + 1]) {
- temp = m_pArray[j];
- m_pArray[j] = m_pArray[j + 1];
- m_pArray[j + 1] = temp;
- }
- }
- float Аverage() { //Среднее арифметическое
- float mid;
- for (int i = 0; i < m_count; i++) mid = +m_pArray[i];
- return (mid / m_count);
- }
- void Show() {
- for (int i = 0; i < m_count; i++) printf("%d ", m_pArray[i]);
- printf("\n");
- }
- void Assign(int *pArray, unsigned int count) { //Метод копирования содержимого
- m_count = count;
- m_pArray = new int[count];
- for (int i = 0; i < count; i++) m_pArray[i] = pArray[i];
- }
- void Repeat() {
- for (int i = 0; i < m_count; i++) {
- for (int j = i + 1; j < m_count; j++) {
- if (i != j) {
- if (m_pArray[i] == m_pArray[j]) {
- for (int g = j; g < m_count - 1; g++) {
- m_pArray[g] = m_pArray[g + 1];
- }
- m_count--;
- }
- if (m_pArray[i] == m_pArray[j]) j--;
- }
- }
- }
- }
- void Clear() { //Очистка
- if (m_pArray != NULL && m_count > 0) {
- delete[] m_pArray;
- m_pArray = NULL;
- m_count = 0;
- }
- }
- ~Vector() {
- Clear();
- }
- private:
- int *m_pArray;
- int m_count;
- };
- int _tmain(int argc, _TCHAR* argv[])
- {
- int n;
- float s = 0;
- scanf("%d", &n);
- Vector x(n);
- Vector y(n);
- printf("Vector x: ");
- x.Show();
- printf("Vector y: ");
- y.Show();
- printf("Vector z: ");
- Vector z(x);
- z.Show();
- s = x.Аverage();
- printf("The arithmetic mean: %0.2f\n", s);
- printf("Sort x: ");
- x.Sort();
- x.Show();
- x.Repeat();
- x.Show();
- y.Repeat();
- y.Show();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement