初步构建完成

This commit is contained in:
iridiumR 2022-03-12 12:31:50 +08:00
parent f0d64dcdc9
commit de11c25836
3 changed files with 170 additions and 14 deletions

View file

@ -1,7 +1,23 @@
/*
* @Author: iR
* @Date: 2022-03-11 20:22:54
* @LastEditors: iR
* @LastEditTime: 2022-03-12 12:30:06
* @FilePath: \Code\ex01\main.cpp
* @Description:
*
* Copyright (c) 2022 by iR, All Rights Reserved.
*/
#include "../inc/matrix.hpp" #include "../inc/matrix.hpp"
#include <stdio.h> #include <stdio.h>
int main() int main()
{ {
Matrix<int> a(2,2); double data[] = {1, 2, 3, 4};
printf("哈哈了"); Matrix<double> a(2, 2, data);
Matrix<double> b(2, 2, 5);
Matrix<double> c = a;
Matrix<double> d;
return 0;
} }

View file

@ -1,6 +1,19 @@
/*
* @Author: iR
* @Date: 2022-03-11 16:44:46
* @LastEditors: iR
* @LastEditTime: 2022-03-12 12:30:44
* @FilePath: \Code\inc\matrix.hpp
* @Description:
*
* Copyright (c) 2022 by iR, All Rights Reserved.
*/
#ifndef _MATRIX_HPP_ #ifndef _MATRIX_HPP_
#define _MATRIX_HPP_ #define _MATRIX_HPP_
#include <iostream>
#include <stdio.h>
#include "../inc/vector.hpp" #include "../inc/vector.hpp"
template <class T> template <class T>
@ -12,29 +25,87 @@ private:
Vector<T> *_mat; Vector<T> *_mat;
protected: protected:
public: public:
/**
// 确定矩阵元素大小,不初始化 * @description: , 2*2, 0
* @param {int} w
* @param {int} h
*/
Matrix();
/**
* @description: 0
* @param {int} w
* @param {int} h
*/
Matrix(int w, int h); Matrix(int w, int h);
// 以相同值初始化矩阵元素
/**
* @description:
* @param {int} w
* @param {int} h
* @param {T} init_value
*/
Matrix(int w, int h, T init_value); Matrix(int w, int h, T init_value);
// 以顺序访问数组的值初始化矩阵元素
Matrix(int w, int h, const double *elements); /**
// 以另一个矩阵初始化当前矩阵的元素,实际是元素拷贝 * @description: 访
* @note: ,
* @param {int} w
* @param {int} h
* @param {const T} elements[]
*/
Matrix(int w, int h, const T elements[]);
/**
* @description:
* @param {Matrix} &another
*/
Matrix(const Matrix &another); Matrix(const Matrix &another);
//只读访问内容
/**
* @description: 访
* @param {int} w
* @param {int} h
* @return {*}
*/
const T get(int w, int h) const; const T get(int w, int h) const;
//运算符重载
T &operator[](int i) { return _mat[i]; } /**
* @description:
*/
const T operator[](int i) const { return _mat->get(i); }
/**
* @description:
*/
T operator=(const Matrix &b);
/**
* @description: 访
* @param {int} w
* @param {int} h
* @return {*}
*/
T &at(int w, int h) { return _mat[h * _width + w]; }
const int getWidth() const { return _width; }
const int getHeight() const { return _height; }
}; };
template <class T>
Matrix<T>::Matrix()
{
_width = 2;
_height = 2;
_mat = new Vector<T>(2 * 2, 0);
}
template <class T> template <class T>
Matrix<T>::Matrix(int w, int h) Matrix<T>::Matrix(int w, int h)
{ {
_width = w; _width = w;
_height = h; _height = h;
_mat = new Vector<T>(w * h); _mat = new Vector<T>(w * h, 0);
} }
template <class T> template <class T>
@ -45,10 +116,68 @@ Matrix<T>::Matrix(int w, int h, T init_value)
_mat = new Vector<T>(w * h, init_value); _mat = new Vector<T>(w * h, init_value);
} }
template <class T>
Matrix<T>::Matrix(int w, int h, const T elements[])
{
int size = w * h;
try
{
// if (w * h > size)
// throw "Error: The number of array elements is less than the matrix elements.\n";
// if (w * h < size)
// throw "Error: The number of array elements is greater than the matrix elements.\n";
_width = w;
_height = h;
_mat = new Vector<T>;
for (int i = 0; i < size; i++)
_mat->put(i, elements[i]);
}
catch (const char *msg)
{
std::cout << msg << "SIZE:" << size << std::endl;
;
}
}
template <class T>
Matrix<T>::Matrix(const Matrix &another)
{
try
{
_width = another.getWidth();
_height = another.getHeight();
_mat = new Vector<T>;
for (int i = 0; i < _width * _height; i++)
_mat->put(i, another[i]);
}
catch (const char *msg)
{
std::cout << msg;
}
}
template <class T>
T Matrix<T>::operator=(const Matrix &another)
{
try
{
_width = another.getWidth();
_height = another.getHeight();
_mat = new Vector<T>;
for (int i = 0; i < _width * _height; i++)
_mat->put(i, another[i]);
}
catch (const char *msg)
{
std::cout << msg;
}
}
template <class T> template <class T>
const T Matrix<T>::get(int w, int h) const const T Matrix<T>::get(int w, int h) const
{ {
return _mat[w * _width + h]; return _mat[h * _width + w];
} }
#endif #endif

View file

@ -1,3 +1,13 @@
/*
* @Author: iR
* @Date: 2022-03-11 16:47:31
* @LastEditors: iR
* @LastEditTime: 2022-03-12 12:20:35
* @FilePath: \Code\inc\vector.hpp
* @Description:
*
* Copyright (c) 2022 by iR, All Rights Reserved.
*/
#ifndef _VECTOR_HPP_ #ifndef _VECTOR_HPP_
#define _VECTOR_HPP_ #define _VECTOR_HPP_
@ -149,6 +159,7 @@ T Vector<T>::put(int a, T value)
} }
_v[a] = value; _v[a] = value;
return _v[a];
} }
template <class T> template <class T>