
- •Задания задание 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
Вариант 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;
}