Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dolzhunkova-02-lab #106

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
217 changes: 209 additions & 8 deletions include/tmatrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ class TDynamicVector
public:
TDynamicVector(size_t size = 1) : sz(size)
{
if (sz == 0)
if (size > MAX_VECTOR_SIZE)
throw out_of_range("Vector size should not be greater than max size");
if (size == 0)
throw out_of_range("Vector size should be greater than zero");
pMem = new T[sz]();// {}; // У типа T д.б. констуктор по умолчанию
pMem = new T[size]();// {}; // У типа T д.б. констуктор по умолчанию
}
TDynamicVector(T* arr, size_t s) : sz(s)
{
Expand All @@ -37,65 +39,151 @@ class TDynamicVector
}
TDynamicVector(const TDynamicVector& v)
{
this->sz = v.sz;
this->pMem = new T[this->sz];
for (int i = 0; i < sz; i++)
this->pMem[i] = v.pMem[i];
}
TDynamicVector(TDynamicVector&& v) noexcept
{
}

~TDynamicVector()
{
if (pMem != NULL)
{
delete[] pMem;
pMem = NULL;
sz = 0;
}
}
TDynamicVector& operator=(const TDynamicVector& v)
{
if (this != &v)
{
if (sz != v.sz)
{
delete[] pMem;
sz = v.sz;
pMem = new T[sz];
}
for (int i = 0; i < sz; i++)
pMem[i] = v.pMem[i];
}
return *this;
}
TDynamicVector& operator=(TDynamicVector&& v) noexcept
{
if (this != &v)
{
if (sz != v.sz)
{
delete[] pMem;
sz = v.sz;
pMem = new T[sz];
}
for (int i = 0; i < sz; i++)
pMem[i] = v.pMem[i];
}
return *this;
}

size_t size() const noexcept { return sz; }

// индексация
T& operator[](size_t ind)
{
if (ind < 0 || ind >= sz)
abort(); // принудительное завершение программы
return pMem[ind];
}
const T& operator[](size_t ind) const
{
if (ind < 0 || ind >= n)
abort(); // принудительное завершение программы
return pMem[ind];
}
// индексация с контролем
T& at(size_t ind)
{
if (ind >= sz || ind < 0) throw "Index error";
return pMem[ind];
}
const T& at(size_t ind) const
{
if (ind >= sz || ind < 0) throw "Index error";
return pMem[ind];
}

// сравнение
bool operator==(const TDynamicVector& v) const noexcept
{
if (this->sz != v.sz) return false;
else
for (int i = 0; i < this->sz; i++) {
if (this->pMem[i] != v.pMem[i]) return false;
}
return true;


}
bool operator!=(const TDynamicVector& v) const noexcept
{
if (this->sz != v.sz) return true;
else
for (int i = 0; i < this->sz; i++) {
if (this->pMem[i] != v.pMem[i]) return true;
}
return false;
}

// скалярные операции
TDynamicVector operator+(T val)
{
TDynamicVector t1(this->size());
for (int i = 0; i < this->size(); i++) {
t1.pMem[i] = this->pMem[i] + val;
}
return t1;
}
TDynamicVector operator-(T val)
{
TDynamicVector t1(this->size());
for (int i = 0; i < this->size(); i++) {
t1.pMem[i] = this->pMem[i] - val;
}
return t1;
}
TDynamicVector operator*(T val)
{
TDynamicVector t1(this->size());
for (int i = 0; i < this->size(); i++) {
t1[i] = this->pMem[i] * val;
}
return t1;
}

// векторные операции
TDynamicVector operator+(const TDynamicVector& v)
{
if (this->sz != v.sz) throw "Должна быть одинаковая размерность";
TDynamicVector<int> res(v.sz);
for (int i = 0; i < v.sz; i++)
res[i] = v.pMem[i] + this->pMem[i];
return res;
}
TDynamicVector operator-(const TDynamicVector& v)
{
if (this->sz != v.sz) throw "Должна быть одинаковая размерность";
TDynamicVector<int> res(v.sz);
for (int i = 0; i < v.sz; i++)
res[i] = v.pMem[i] - this->pMem[i];
return res;
}
T operator*(const TDynamicVector& v) noexcept(noexcept(T()))
T operator*(const TDynamicVector& v)
{
if (this->sz != v.sz) throw "Должна быть одинаковая размерность";
T res = 0;
for (int i = 0; i < v.sz; i++)
res += v.pMem[i] * this->pMem[i];
return res;
}

friend void swap(TDynamicVector& lhs, TDynamicVector& rhs) noexcept
Expand Down Expand Up @@ -130,45 +218,158 @@ class TDynamicMatrix : private TDynamicVector<TDynamicVector<T>>
public:
TDynamicMatrix(size_t s = 1) : TDynamicVector<TDynamicVector<T>>(s)
{
for (size_t i = 0; i < sz; i++)
pMem[i] = TDynamicVector<T>(sz);
if (s > MAX_MATRIX_SIZE)
throw out_of_range("Matrix size should be smaller than max size");

for (size_t i = 0; i < s; i++)
pMem[i] = TDynamicVector<T>(s);
}

size_t size() const noexcept {
size_t pSize = pMem[0].size();
return pMem[0].size();
}

using TDynamicVector<TDynamicVector<T>>::operator[];

// сравнение
bool operator==(const TDynamicMatrix& m) const noexcept
{
if (this->sz != m.sz) return false;
else
for (int i = 0; i < this->sz; i++)
if (this->pMem[i] != m.pMem[i]) return false;
return true;
}
bool operator!=(const TDynamicMatrix& m) const noexcept
{
if (this->sz != m.sz) return true;
else
for (int i = 0; i < this->sz; i++)
if (this->pMem[i] != m.pMem[i]) return true;
return false;
}

// матрично-скалярные операции
TDynamicMatrix operator*(const T& val)
{
TDynamicMatrix m1(this->size());
for (int i = 0; i < this->size(); i++) {
m1[i] = this->pMem[i] * val;
}
return m1;
}

// матрично-векторные операции
TDynamicVector<T> operator*(const TDynamicVector<T>& v)
{
if (v.size() != this->pMem[0].size())
{
throw out_of_range("Matrix should be equal size with vector");
}
int vSize = v.size();
TDynamicVector<int> res(vSize);
for (int i = 0; i < vSize; i++)
for (int j = 0; j < vSize; j++)
res[i] += v.pMem[j] * this->pMem[i][j];
return res;
}

// матрично-матричные операции
TDynamicMatrix operator+(const TDynamicMatrix& m)
{
if (m.pMem[0].size() != this->pMem[0].size())
{
throw out_of_range("Matrices should be equal sizes");
}
int mSize = m.pMem[0].size();
TDynamicMatrix res(mSize);
for (int i = 0; i < mSize; i++)
for (int j = 0; j < mSize; j++)
res.pMem[i][j] = this->pMem[i][j] + m.pMem[i][j];
return res;
}

TDynamicMatrix operator-(const TDynamicMatrix& m)
{
if (m.pMem[0].size() != this->pMem[0].size())
{
throw out_of_range("Matrices should be equal sizes");
}
int mSize = m.pMem[0].size();
TDynamicMatrix res(mSize);
for (int i = 0; i < mSize; i++)
for (int j = 0; j < mSize; j++)
res.pMem[i][j] = this->pMem[i][j] - m.pMem[i][j];
return res;
/*if (this.str != m.str || this.stlb != this.stlb)
{
cout << "Должна быть одинаковая размерность" << endl;
break;
}
T** res = new T[this.str][this.stlb];
for (int i = 0; i < this.str; i++)
for (int j = 0; j < this.stlb; j++)
res.pMem[i][j] = this.pMem[i][j] - m.pMem[i][j];
return res;*/
}
TDynamicMatrix operator*(const TDynamicMatrix& m)
{
if (this.str != m.stlb || this.stlb != m.str)
{
cout << "Число столбцов m1 должно совпадать с числом строк m2, а число строк m1 должно совпадать с числом столбцов m2" << endl;
break;
}
T** res = new T[this.str][m.stlb];
for (int i = 0; i < this.str; i++)
for (int j = 0; j < m.stlb; j++)
for (int k = 0; k < this.stlb; k++)
res[i][j] += this.pMem[i][k] * m.pMem[k][j];
return res;
}

// ввод/вывод
friend istream& operator>>(istream& istr, TDynamicMatrix& v)
{
int str_;
int stlb_;
cout << "Введите количество строк:";
istr >> str_;
cout << endl << "Введите количество строк:";
istr >> stlb_;
if (v.str != str_ || v.stlb != stlb_)
{
if (v.pMem != NULL)
for (int i = 0; i < stlb; i++)
{
delete[] v.pMem[i];
}
delete[] v.pMem;
v.str = str_;
v.stlb = stlb_;
v.pMem = new T[str_][stlb_];
}
for (int i = 0; i < v.str; i++)
for (int j = 0; j < v.stlb; j++)
{
cout << endl << "Введите значение:";
istr >> v.pMem[i][j];
}
return istr;
}
friend ostream& operator<<(ostream& ostr, const TDynamicMatrix& v)
{
/* for (int i = 0; i < v.str; i++)
{
ostr << "(";
for (int j = 0; j < v.stlb; j++)
ostr << v.pMem[i][j] << " ";
ostr << ")" << endl;
}
ostr << endl;*/
return ostr;
}

};

#endif
4 changes: 2 additions & 2 deletions sln/gtest.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@
<ConfigurationType>StaticLibrary</ConfigurationType>
<CharacterSet>Unicode</CharacterSet>
<WholeProgramOptimization>true</WholeProgramOptimization>
<PlatformToolset>v142</PlatformToolset>
<PlatformToolset>v143</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>StaticLibrary</ConfigurationType>
<CharacterSet>Unicode</CharacterSet>
<PlatformToolset>v142</PlatformToolset>
<PlatformToolset>v143</PlatformToolset>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
Expand Down
4 changes: 2 additions & 2 deletions sln/sample_utmatrix.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@
<ConfigurationType>Application</ConfigurationType>
<CharacterSet>Unicode</CharacterSet>
<WholeProgramOptimization>true</WholeProgramOptimization>
<PlatformToolset>v142</PlatformToolset>
<PlatformToolset>v143</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<CharacterSet>Unicode</CharacterSet>
<PlatformToolset>v142</PlatformToolset>
<PlatformToolset>v143</PlatformToolset>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
Expand Down
4 changes: 2 additions & 2 deletions sln/test_utmatrix.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@
<ConfigurationType>Application</ConfigurationType>
<CharacterSet>Unicode</CharacterSet>
<WholeProgramOptimization>true</WholeProgramOptimization>
<PlatformToolset>v142</PlatformToolset>
<PlatformToolset>v143</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<CharacterSet>Unicode</CharacterSet>
<PlatformToolset>v142</PlatformToolset>
<PlatformToolset>v143</PlatformToolset>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
Expand Down
Loading