Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include "iostream"
- using namespace std;
- class TArr
- {
- double *Array;
- int Amount;
- public:
- TArr(int AmountAmount = 4, double ArrayArray = 1);
- TArr(TArr&obj);
- ~TArr();
- void Input();
- void Output();
- TArr&operator=(TArr&arr);
- bool operator >=(TArr &arr);
- TArr&operator+=(int x);
- friend TArr operator *=(TArr &arr,int number);
- friend bool operator==(TArr&arr1, TArr&arr2);
- };
- TArr::TArr(int AmountAmount, double ArrayArray)
- {
- Amount = AmountAmount;
- Array = new double[Amount];
- for (int i = 0; i < Amount; i++)
- Array[i] = ArrayArray;
- }
- TArr::TArr(TArr&obj)
- {
- Amount = obj.Amount;
- Array = new double[Amount];
- for (int i = 0; i < Amount; i++)
- {
- Array[i] = obj.Array[i];
- }
- }
- TArr::~TArr()
- {
- delete[]Array;
- }
- void TArr::Input()
- {
- cout << "Количество элементов: ";
- delete[]Array;
- cin >> Amount;
- Array = new double[Amount];
- for (int i = 0; i < Amount; i++)
- {
- cout << "Введите Array[" << i << "] элемент динамического массива: ";
- cin >> Array[i];
- }
- }
- void TArr::Output()
- {
- cout << "Вывод массива типа double с количеством элементов " << Amount << ": " << endl;
- for (int i = 0; i < Amount; i++)
- {
- cout << "Array" << "[" << i << "] = " << Array[i] << endl;
- }
- }
- TArr&TArr::operator=(TArr&arr)
- {
- delete[]Array;
- Amount = arr.Amount;
- Array = new double[Amount];
- for (int i = 0; i < Amount; i++)
- {
- Array[i] = arr.Array[i];
- }
- return *this;
- }
- TArr&TArr::operator+=(int x)
- {
- for (int i = 0; i < Amount; i++)
- {
- Array[i] += x;
- }
- return *this;
- }
- TArr operator*=(TArr &arr, int number) {
- arr.Amount += number;
- double *copy = new double[arr.Amount];
- for (int i = 0; i < arr.Amount; i++)
- {
- if (i < arr.Amount - number)
- copy[i] = arr.Array[i];
- else
- copy[i] = 0;
- }
- delete []arr.Array;
- arr.Array = new double[arr.Amount];
- for (int i = 0; i < arr.Amount; i++)
- {
- arr.Array[i] = copy[i];
- }
- delete[]copy;
- return arr;
- }
- bool operator==(TArr&arr1, TArr&arr2)
- {
- if (arr1.Amount != arr2.Amount) //сравниваем размеры массивов объектов
- {
- cout << "В массивах разное количество элементов" << endl;
- return 0;
- }
- else //проверяем равны ли данныев в ячейках массивов
- {
- for (int i = 0; i < arr1.Amount; i++)
- {
- if (arr1.Array[i] != arr2.Array[i])
- {
- cout << "Значения массивов не равны" << endl;
- return 0;
- }
- }
- }
- return 1;
- }
- bool TArr::operator>=(TArr &arr) {
- if (Amount >= arr.Amount)
- return true;
- else
- return false;
- }
- int main()
- {
- setlocale(LC_ALL, "Russian");
- TArr test, test2, test3(3, 4);
- test.Input();
- test.Output();
- test2.Output();
- test3.Output();
- test3 = test2;
- cout << "Пункт 2) Присвоение(test3 = test2)" << endl;
- cout << "Массив test" << endl;
- test.Output();
- cout << "Массив test2" << endl;
- test2.Output();
- cout << "Массив test3" << endl;
- test3.Output();
- test += 3;
- cout << "Пункт 3) Объект + число (test += 3)" << endl;
- cout << "Массив test" << endl;
- test.Output();
- cout << "Массив test2" << endl;
- test2.Output();
- cout << "Массив test3" << endl;
- test3.Output();
- cout << "Пункт 4) Сравнение массивов(test/test2)" << endl;
- if (test == test2)
- cout << "test = test2" << endl;
- else
- cout << "test != test2" << endl;
- cout << "Сравнение массивов(test2/test3)" << endl;
- if (test2 == test3)
- cout << "test2 = test3" << endl;
- else
- cout << "test2 != test3" << endl;
- cout << "Пункт 5) Увеличение длины массива test на 5 элементов и заполнение их нулями " << endl;
- test *= 5;
- cout << "Массив test" << endl;
- test.Output();
- cout << "Массив test2" << endl;
- test2.Output();
- cout << "Массив test3" << endl;
- test3.Output();
- cout << "Пункт 6) Сравнение количества элементов test/test2" << endl;
- if (test >= test2)
- cout << "test >= test2" << endl;
- else
- cout << "test < test2" << endl;
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement