Частина тексту файла (без зображень, графіків і формул):
МІНІСТЕРСТВО ОСВІТИ І НАУКИ УКРАЇНИ
НАЦІОНАЛЬНИЙ УНІВЕРСИТЕТ ”ЛЬВІВСЬКА ПОЛІТЕХНІКА”
Кафедра “Інформаційні системи та мережі”
Лабораторна робота №3
Агрегування та композиція класів
Львів-2014
Мета роботи полягає у вивченні механізму повторного використання
коду на основі контейнерних класів.
Послідовність роботи
1. Ознайомитися з особливостями агрегування та композиції класів.
2. За виданим завданням написати програму мовою С++, створити та
реалізувати її проект.
3. Оформити звіт про роботу
Завдання
16. Клас МАТРИЦЯ містить масив об’єктів класу ВЕКТОР дійсних чисел. Визначити необхідні дані, конструктри, деструктори та методи роботи з елементами даних. Виконати додавання, віднімання матриць, множення матриці на вектор, множення матриці на матрицю.
Текст програми
#include <iostream>
#include <math.h>
using namespace std;
class Vector{
private:
double * value;
int count;
public:
Vector() { count = 0; }
Vector(int _count) { value = new double[count = _count]; }
Vector(double * _value, int _count) {
value = _value;
}
double& operator[](int);
int Count() { return count; }
friend istream& operator>>(istream&, Vector&);
friend ostream& operator<<(ostream&, Vector&);
};
double& Vector::operator[](int n){
if(n<count) return value[n];
else throw "Некороктний номер";
}
istream& operator>>(istream& stream, Vector& p){
cout<<"Enter count:"; stream>>p.count;
p.value = new double[p.count];
for (int i = 0; i < p.count; i++)
{
stream>>p[i];
}
return stream;
}
ostream& operator<<(ostream& stream, Vector& p){
for (int i = 0; i < p.count; i++)
{
stream<<p.value[i]<<"\t";
}
return stream;
}
class Matrix
{
private:
Vector * value;
int count;
public:
Matrix() {count = 0; }
Matrix(int _count) { value = new Vector[count = _count]; }
Matrix(Vector * _value, int _count){
value = new Vector[count = _count];
for (int i = 0; i < count; i++)
{
value[i] = Vector(_value[i]);
}
}
~Matrix() { }
Matrix operator+(Matrix&);
Matrix operator-(Matrix&);
Matrix operator*(Vector&);
Matrix operator*(Matrix&);
Vector& operator[](int);
int Count() { return count; }
friend istream& operator>>(istream&, Matrix&);
friend ostream& operator<<(ostream&, Matrix&);
};
Matrix Matrix::operator+(Matrix& M){
if(count != M.count || (*this)[0].Count() != M[0].Count()) throw "Неможливо додати";
Matrix R = Matrix(count);
int n = count, m = this[0].Count();
for (int i = 0; i < n; i++)
{
R[i] = Vector(m);
for (int j = 0; j < m; j++)
{
R[i][j] = (*this)[i][j] + M[i][j];
}
}
return R;
}
Matrix Matrix::operator-(Matrix& M){
if(count != M.count || (*this)[0].Count() != M[0].Count()) throw "Неможливо відняти";
Matrix R = Matrix(count);
int n = count, m = (*this)[0].Count();
for (int i = 0; i < n; i++)
{
R[i] = Vector(m);
for (int j = 0; j < m; j++)
{
R[i][j] = (*this)[i][j] - M[i][j];
}
}
return R;
}
Matrix Matrix::operator*(Vector& V){
if((*this)[0].Count() != V.Count()) throw "Неможливо перемножити";
int n = count, m = (*this)[0].Count();
Matrix R = Matrix(n);
for (int i = 0; i < n; i++)
{
R[i] = Vector(1);
int k = 0;
R[i][k] = 0;
for (int j = 0; j < m; j++)
{
R[i][k] += (*this)[i][j] * V[j];
}
}
return R;
}
Matrix Matrix::operator*(Matrix& M){
if((*this)[0].Count() != M.Count()) throw "Неможливо перемножити";
int n = count, m = (*this)[0].Count(), l = M[0].Count();
Matrix R = Matrix(n);
for (int i = 0; i < n; i++)
{
R[i] = Vector(l);
for (int k = 0; k < l; k++)
{
R[i][k] = 0;
for (int j = 0; j < m; j++)
{
R[i][k] += (*this)[i][j] * M[k][j];
}
}
}
return R;
}
Vector& Matrix::operator[](int n){
if(n<count) return value[n];
else throw "Неправильний індекс";
}
istream& operator>>(istream& stream, Matrix& p){
cout<<"Enter count:"; stream>>p.count;
p.value = new Vector[p.count];
for (int i = 0; i < p.count; i++)
{
cout<<"\t";
stream>>p[i];
}
return stream;
}
ostream& operator<<(ostream& stream, Matrix& p){
for (int i = 0; i < p.count; i++)
{
stream<<p.value[i]<<"\n";
}
return stream;
}
void main(){
Matrix m1,m2;
Vector v;
cout<<"Enter M1\n";
cin>>m1;
cout<<"Enter M2\n";
cin>>m2;
cout<<"Enter V:\n";
cin>>v;
cout<<"M1:\n"<<m1<<"\nM2:\n"<<m2<<"\nV:\n"<<v<<endl;
cout<<"M1 + M2\n"<<m1+m2<<endl;
cout<<"M1 - M2\n"<<m1-m2<<endl;
cout<<"M1 * M2\n"<<m1*m2<<endl;
cout<<"M1 * V\n"<<m1*v<<endl;
system("pause");
}
Результат виконання
/
Висновок
При виконанні даної лабораторної роботи я вивчив механізм повторного використання коду на основі контейнерних класів.