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

Вариант 18

Задание

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

Класс

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

Интерфейс

Complex

re, im

Конструкторы, функции(friend) assign, add, plus, print

Array

pa, size

Конструкторы, операции +,+=(friend), =, [], <<

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

//main.cpp

#include "Array.h"

void main() {

Complex a, b(1, -2), c(b);

assign(a, b);

add(a, plus(b, c));

print(cout, a);

cout << endl;

Array arr1, arr2(3), arr3(arr2);

assign(arr2[1], a);

assign(arr2[2], b);

arr1 = arr2 + arr3;

arr2 += arr1;

cout << arr1 << '\n' << arr2 << '\n' << arr3 << endl;

system("pause");

}

//Complex.h

#include <iostream>

using namespace std;

class Complex {

private:

int re;

int im;

public:

Complex();

Complex(const int, const int);

Complex(const Complex&);

friend Complex& assign(Complex&, const Complex&);

friend Complex& add(Complex&, const Complex&);

friend Complex plus(const Complex&, const Complex&);

friend void print(ostream& s, const Complex&);

};

//Complex.cpp

#include "Complex.h"

Complex::Complex() {

re = 0;

im = 0;

}

Complex::Complex(const int a, const int b) {

re = a;

im = b;

}

Complex::Complex(const Complex& c) {

assign(*this, c);

}

Complex& assign(Complex& a, const Complex& b) {

a.re = b.re;

a.im = b.im;

return a;

}

Complex& add(Complex& a, const Complex& b) {

a.re += b.re;

a.im += b.im;

return a;

}

Complex plus(const Complex& a, const Complex& b) {

return Complex(a.re + b.re, a.im + b.im);

}

void print(ostream& s, const Complex& c) {

s << c.re << (c.im < 0 ? '-' : '+') << 'i' << (c.im < 0 ? -c.im : c.im);

}

//Array.h

#include "Complex.h"

class Array {

private:

Complex *pa;

int size;

public:

Array();

Array(const int);

Array(const Array&);

~Array();

Array operator +(const Array&) const;

friend Array& operator +=(Array&, const Array&);

Array& operator =(const Array&);

Complex& operator [](const int) const;

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

};

//Array.cpp

#include "Array.h"

Array::Array() {

pa = NULL;

size = 0;

}

Array::Array(const int s) {

size = s;

pa = new Complex[s];

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

pa[i] = Complex();

}

Array::Array(const Array& a) {

*this = a;

}

Array::~Array() {

if (size > 0)

delete [] pa;

}

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

int newSize = size + a.size;

Array newArr(newSize);

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

newArr.pa[i] = i < size ? pa[i] : a.pa[i - size];

return newArr;

}

Array& operator +=(Array& a, const Array& b) {

int newSize = a.size + b.size;

Complex *newPa = new Complex[newSize];

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

newPa[i] = i < a.size ? a.pa[i] : b.pa[i - a.size];

if (a.size > 0)

delete [] a.pa;

a.size = newSize;

a.pa = newPa;

return a;

}

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

if (size > 0)

delete [] pa;

size = a.size;

pa = new Complex[size];

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

pa[i] = a.pa[i];

return *this;

}

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

if (i >= 0 && i < size)

return pa[i];

else

throw -1;

}

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

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

s << i << ": ";

print(s, a.pa[i]);

s << endl;

}

return s;

}

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