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

Куракин Матвей #13

Open
wants to merge 9 commits 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
152 changes: 131 additions & 21 deletions include/utmatrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class TVector
public:
static const size_t max_size = std::numeric_limits<unsigned int>::max();

TVector() {}
TVector():size(0), startIndex(0), pVector(nullptr) {}
TVector(int size, int startIndex = 0); //конструктор инициализации
TVector(size_t size, size_t startIndex = 0); //конструктор инициализации

Expand All @@ -29,11 +29,11 @@ class TVector

TVector(const TVector &v); // конструктор копирования
~TVector();
size_t getSize() { return size; } // размер вектора
size_t getStartIndex(){ return startIndex; } // индекс первого элемента
T& getElement(int i);
T& getElement(size_t i);
void setElement(int index, T element);
size_t getSize() const { return this->size; } // размер вектора
size_t getStartIndex() const{ return this->startIndex; } // индекс первого элемента
//T& getElement(int i) const;
T& getElement(size_t i) const;
//void setElement(int index, T element);
void setElement(size_t index, T element);

T& operator[](int pos); // доступ
Expand All @@ -56,104 +56,178 @@ class TVector
// ввод-вывод
friend std::istream& operator>>(std::istream &in, TVector &v)
{

return in;
}
friend std::ostream& operator<<(std::ostream &out, const TVector &v)
{
for (int i = 0; i < v.getSize()-v.getStartIndex() ; i++)
out << v.getElement(i)<<" ";
return out;
}
};

template <class T>//конструктор инициализации
TVector<T>::TVector(int _size, int startIndex)
TVector<T>::TVector(int _size, int startIndex): size(_size), startIndex(startIndex)
{
pVector = new T[_size];
if (size > max_size || startIndex < 0 || startIndex >= size)
throw 1;
pVector = new T[size-startIndex];
for (int i = 0;i<size-startIndex;i++)
pVector[i]= T();
} /*-------------------------------------------------------------------------*/

template <class T>//конструктор инициализации
TVector<T>::TVector(size_t _size, size_t startIndex)
TVector<T>::TVector(size_t _size, size_t startIndex) : size(_size), startIndex(startIndex)
{
if (size > max_size || startIndex < 0 || startIndex >= size)
throw 1;
pVector = new T[size-startIndex];
for (int i = 0; i < size - startIndex; i++)
pVector[i] = (T)0;
} /*-------------------------------------------------------------------------*/

template <class T> //конструктор копирования
TVector<T>::TVector(const TVector<T> &v)
TVector<T>::TVector(const TVector<T> &v) : size(0), startIndex(0), pVector(nullptr)
{
*this = v;
} /*-------------------------------------------------------------------------*/

template <class T> //деструктор
TVector<T>::~TVector()
{
delete[] pVector;
} /*-------------------------------------------------------------------------*/

template <class T> // доступ
T& TVector<T>::operator[](int pos)
{
return *pVector;
if (pos-startIndex < 0 || pos >= size)
throw 1;
return pVector[pos-startIndex];
} /*-------------------------------------------------------------------------*/

template <class T> // доступ
T& TVector<T>::operator[](size_t pos)
{
return *pVector;
if (pos - startIndex < 0 || pos >= size)
throw 1;
return *pVector[pos - startIndex];
} /*-------------------------------------------------------------------------*/

template <class T> // сравнение
bool TVector<T>::operator==(const TVector &v) const
bool TVector<T>::operator==(const TVector& v) const
{
return false;
if (size != v.size || startIndex != v.startIndex) return false;
for (int i = 0; i < size-startIndex; i++)
if (pVector[i] != v.pVector[i]) return false;
return true;
} /*-------------------------------------------------------------------------*/

template <class T> // сравнение
bool TVector<T>::operator!=(const TVector &v) const
{
return !(*this==v);
} /*-------------------------------------------------------------------------*/

template <class T> // присваивание
TVector<T>& TVector<T>::operator=(const TVector &v)
{
return TVector<T>();
if (this == &v) return *this;
if(size!=0)
delete[] pVector;
size = v.size;
startIndex = v.startIndex;
pVector = new T[size-startIndex];
for (int i = 0; i < size-startIndex; i++)
pVector[i] = v.pVector[i];
return *this;
} /*-------------------------------------------------------------------------*/

template <class T> // прибавить скаляр
TVector<T> TVector<T>::operator+(const T &val)
{
TVector<T> a(size, startIndex);
for (int i = 0; i < size-startIndex; i++)
a.pVector[i] = pVector[i] + val;
return a;
} /*-------------------------------------------------------------------------*/

template <class T> // вычесть скаляр
TVector<T> TVector<T>::operator-(const T &val)
{
TVector<T> a(size, startIndex);
for (int i = 0; i < size - startIndex; i++)
a.pVector[i] = pVector[i] - val;
return a;
} /*-------------------------------------------------------------------------*/

template <class T> // умножить на скаляр
TVector<T> TVector<T>::operator*(const T &val)
{
TVector<T> a(size, startIndex);
for (int i = 0; i < size-startIndex; i++)
a.pVector[i] = pVector[i] * val;
return a;
} /*-------------------------------------------------------------------------*/

template <class T> // сложение
TVector<T> TVector<T>::operator+(const TVector<T> &v)
{
if (size != v.size || startIndex != v.startIndex) throw 1;
TVector<T> a(size, startIndex);
for (int i = 0; i < size-startIndex; i++)
a.pVector[i] = pVector[i] + v.pVector[i];
return a;

} /*-------------------------------------------------------------------------*/

template <class T> // вычитание
TVector<T> TVector<T>::operator-(const TVector<T> &v)
{
if (size != v.size || startIndex != v.startIndex) throw 1;
TVector<T> a(size, startIndex);
for (int i = 0; i < size-startIndex; i++)
a.pVector[i] = pVector[i] - v.pVector[i];
return a;
} /*-------------------------------------------------------------------------*/

template <class T> // скалярное произведение
T TVector<T>::operator*(const TVector<T> &v)
T TVector<T>::operator*(const TVector<T>& v)
{
return T();
if (size != v.size || startIndex != v.startIndex) throw 1;
T summ = 0;
for (int i = 0; i < size - startIndex; i++)
summ += pVector[i] * v.pVector[i];
return summ;
} /*-------------------------------------------------------------------------*/

/*template <class T>
T& TVector<T>::getElement(int index) const
{
if (index < startIndex || index>= size) throw 1;
return pVector[index];
}*/

template <class T>
T& TVector<T>::getElement(int index)
T& TVector<T>::getElement(size_t index) const
{
return *pVector;
if (index < startIndex || index >= size) throw 1;
return pVector[index-startIndex];
}

template <class T>
/*template <class T>
void TVector<T>::setElement(int index, T element)
{
if(index < startIndex || index>= size) throw 1;
pVector[index-startIndex] = element;
}*/

template <class T>
void TVector<T>::setElement(size_t index, T element)
{
if (index < startIndex || index >= size) throw 1;
pVector[index - startIndex] = element;
}

// Верхнетреугольная матрица
Expand Down Expand Up @@ -196,8 +270,24 @@ class TMatrix : public TVector<TVector<T> >
};

template <class T>
TMatrix<T>::TMatrix(int s): TVector<TVector<T> >(s)
TMatrix<T>::TMatrix(int s): TVector<TVector<T>>(s)
{
//unsigned long long x = (s * s);
if (static_cast<unsigned long long>(static_cast<unsigned long long>(s)* static_cast<unsigned long long>(s)) > static_cast<unsigned long long>(TVector<T>::max_size))
throw 1;
for (int i = 0; i < s; i++) {
TVector<TVector<T>>::pVector[i] = TVector<T>(s, i);
}
} /*-------------------------------------------------------------------------*/
template <class T>
TMatrix<T>::TMatrix(size_t s): TVector<TVector<T>>(s)
{
//unsigned long long x = (s * s);
if (static_cast<unsigned long long>(static_cast<unsigned long long>(s)* static_cast<unsigned long long>(s)) > static_cast<unsigned long long>(TVector<T>::max_size))
throw 1;
for (int i = 0; i < s; i++) {
TVector<TVector<T>>::pVector[i] = TVector<T>(s, i);
}
} /*-------------------------------------------------------------------------*/

template <class T> // конструктор копирования
Expand All @@ -211,27 +301,47 @@ TMatrix<T>::TMatrix(const TVector<TVector<T> > &mt):
template <class T> // сравнение
bool TMatrix<T>::operator==(const TMatrix<T> &mt) const
{
if (TVector<TVector<T>>::size != mt.size || TVector<TVector<T>>::startIndex != mt.startIndex)
return false;
for (int i = 0; i < TVector<TVector<T>>::size; i++) {
if (TVector<TVector<T>>::pVector[i] != mt.pVector[i])
return false;
}
return true;
} /*-------------------------------------------------------------------------*/

template <class T> // сравнение
bool TMatrix<T>::operator!=(const TMatrix<T> &mt) const
{
return !(*this == mt);
} /*-------------------------------------------------------------------------*/

template <class T> // присваивание
TMatrix<T>& TMatrix<T>::operator=(const TMatrix<T> &mt)
{
if (this == &mt) return *this;
this->size = mt.size;
this->startIndex = mt.startIndex;
delete[] this->pVector;
this->pVector = new TVector<T>[this->size];

for (int i = 0; i < this->size; i++)
this->setElement(i, mt.getElement(i));
return *this;
} /*-------------------------------------------------------------------------*/

template <class T> // сложение
TMatrix<T> TMatrix<T>::operator+(const TMatrix<T> &mt)
{
for (int i = 0; i < this->size; i++)
this->setElement(i, this->getElement(i) + mt.getElement(i));
return *this;
} /*-------------------------------------------------------------------------*/

template <class T> // вычитание
TMatrix<T> TMatrix<T>::operator-(const TMatrix<T> &mt)
{
for (int i = 0; i < this->size; i++)
this->setElement(i, this->getElement(i) - mt.getElement(i));
return *this;
} /*-------------------------------------------------------------------------*/
13 changes: 11 additions & 2 deletions samples/sample_matrix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,14 @@

int main()
{
TMatrix<int> a(5), b(5), c(5);
TMatrix<int> m(4);
m[0].setElement(0,4);

std::cout << m[0].getElement(0);
// << "\n" << m2;
//std::cout << a;
//std::cout << a;
/*TMatrix<int> a(5), b(5), c(5);
int i, j;
setlocale(LC_ALL, "Russian");
std::cout << "Òåñòèðîâàíèå ïðîãðàìì ïîääåðæêè ïðåäñòàâëåíèÿ òðåóãîëüíûõ ìàòðèö"
Expand All @@ -25,6 +32,8 @@ int main()
std::cout << "Matrix a = " << std::endl << a << std::endl;
std::cout << "Matrix b = " << std::endl << b << std::endl;
std::cout << "Matrix c = a + b" << std::endl << c << std::endl;
return 0;
TMatrix<int> d = a;
std::cout << d;
return 0;*/
}
//---------------------------------------------------------------------------
Loading
Loading