- •Задания задание 1
- •Задание 2
- •Задание 3
- •3. Разработать классы Point и Array, позволяющие использовать их в следующей программе:
- •Задание 4
- •Задание 5
- •Задание 6
- •6. Разработать классы Point и Array, позволяющие использовать их в следующей программе:
- •Задание 7
- •Задание 8
- •Задание 9
- •Задание 10
- •Задание 11
- •Задание 12
- •Задание 13
- •17. Разработать классы Point и Array, позволяющие использовать их в следующей программе:
- •Задание 19
- •19. Разработать классы Point и Array, позволяющие использовать их в следующей программе:
- •Задание 20
- •Задание 21
- •Задание 22
- •22. Разработать классы Point и Array, позволяющие использовать их в следующей программе:
- •Задание 23
- •Задание 24
- •Задание 25
- •Задание 26
- •Задание 27
- •Задание 28
- •Задание 29
- •Задание 30
- •Решения Вариант 1
- •Вариант 2
- •Вариант 3
- •Вариант 4
- •Вариант 5
- •Вариант 7
- •Вариант 8
- •Вариант 9
- •Вариант 10
- •Вариант 11
- •Вариант 12
- •Вариант 13
- •Вариант 14
- •Вариант 15
- •Вариант 16
- •Вариант 6, 17, 19
- •Вариант 18
- •Вариант 20
- •Вариант 21
- •Вариант 22
- •Вариант 23
- •Вариант 24
- •Вариант 25
- •Вариант 26, 28
- •Вариант 27, 29.
- •Вариант 30
Вариант 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;
}
