Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
ТП Задачи.docx
Скачиваний:
0
Добавлен:
01.07.2025
Размер:
221.76 Кб
Скачать

Вариант 21

Написать тексты h-файлов и cpp-файлов для классов Complex и Array (массив комплексных чисел). Описание классов:

Класс

Элементы данных

Интерфейс

Complex

re, im

Конструкторы, операции +,+=, =, <<

Array

pa, size

Конструкторы, friend -функции assign, add,

операции =,==, <, <<

Привести примеры создания и использования объектов классов Complex и Array. Нарисовать диаграммы классов.

main.cpp

#include "stdafx.h"

#include "Array.h"

#include <iostream>

int _tmain(int argc, _TCHAR* argv[])

{

Complex a(1, 1);

Complex b(2, -2);

Complex c(0, 0);

Complex d;

Complex e;

c += a;

d = c + b;

e = a;

Array arr(2);

Array arr1(2);

Array arr2(3);

Array arr3(4);

arr[0] = a;

arr[1] = b;

arr1[0] = c;

arr1[1] = d;

add(arr2, arr);

assign(arr, arr1);

arr3 = arr1;

std::cout << "Class Complex: " << endl;

std::cout << "a: " << a;

std::cout << "b: " << b;

std::cout << "c: " << c;

std::cout << "d: " << d;

std::cout << "e: " << e << endl;

std::cout << "Class Array: " << endl;

std::cout << "arr: \n" << arr << endl;

std::cout << "arr1: \n" << arr1 << endl;

std::cout << "arr2: \n" << arr2 << endl;

std::cout << "arr3: \n" << arr3 << endl;

std::cout << std::boolalpha;

std::cout << (arr == arr1) << endl;

std::cout << (arr < arr2) << endl;

system("pause");

return 0;

}

Complex.h

#include <iostream>

using namespace std;

class Complex

{

private:

int re;

int im;

public:

Complex();

Complex(const int, const int);

Complex operator +(const Complex &) const;

Complex operator =(const Complex &);

Complex operator +=(const Complex &);

bool operator !=(const Complex &);

friend ostream& operator <<(ostream&, const Complex &);

};

Complex.cpp

#include "stdafx.h"

#include "Complex.h"

using namespace std;

Complex::Complex() {

this->re = 0;

this->im = 0;

}

Complex::Complex(const int re, const int im) {

this->re = re;

this->im = im;

}

Complex Complex::operator +(const Complex & a) const {

return Complex(this->re + a.re, this->im + a.im);

}

Complex Complex::operator +=(const Complex & a) {

this->re += a.re;

this->im += a.im;

return *this;

}

bool Complex::operator!=(const Complex & a) {

if (this->re != a.re)

return true;

if (this->im != a.im)

return true;

return false;

}

Complex Complex::operator =(const Complex & a) {

this->re = a.re;

this->im = a.im;

return *this;

}

ostream & operator<<(ostream & s, const Complex & a) {

s << a.re;

if (a.im >= 0)

s << "+" << a.im << "j";

else

s << a.im << "j";

s << endl;

return s;

}

Array.h

#include "stdafx.h"

#include "Complex.h"

class Array : Complex

{

private:

Complex* pa;

int size;

public:

Array();

Array(const int);

Complex & operator[](const int) const;

friend void assign(Array &, const Array &);

friend void add(Array &, const Array &);

void operator =(const Array &);

bool operator ==(const Array &) const;

bool operator <(const Array&) const;

friend ostream& operator <<(ostream &, const Array &);

};

Array.cpp

#include "stdafx.h"

#include "Array.h"

Array::Array() {

pa = NULL;

size = 0;

}

Array::Array(const int size) {

this->size = size;

if (this->size > 0)

pa = new Complex[this->size];

for (int i = 0; i < this->size; i++)

pa[i] = Complex();

}

Complex & Array::operator[](const int index) const {

if (index < 0 || index > this->size) {

std::cout << "Error";

exit(1);

}

else

return pa[index];

}

void assign(Array & a, const Array & b) {

a.pa = b.pa;

}

void add(Array & a, const Array & b) {

int newSizeTempArr = a.size + b.size;

Complex *tempArr = new Complex[newSizeTempArr];

for (int i = 0; i < newSizeTempArr; i++)

tempArr[i] = (i < a.size) ? a[i] : b[i - b.size];

delete[] a.pa;

a.size = newSizeTempArr;

a.pa = new Complex[a.size];

for (int i = 0; i < a.size; i++)

a[i] = tempArr[i];

delete[] tempArr;

}

void Array::operator=(const Array & a) {

assign(*this, a);

}

bool Array::operator==(const Array & a) const {

if (this->size != a.size)

return false;

for (int i = 0; i < this->size; i++)

if (this->pa[i] != a[i])

return false;

return true;

}

bool Array::operator <(const Array & a) const {

return this->size < a.size;

}

ostream & operator<<(ostream & s, const Array & arr) {

for (int i = 0; i < arr.size; i++)

{

s << "array[" << i << "] = " << arr[i];

}

return s;

}

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]