From ea2c627324c73ec9e2fabb242da7d6c47ce4cb9a Mon Sep 17 00:00:00 2001 From: iridiumR Date: Fri, 11 Mar 2022 20:27:54 +0800 Subject: [PATCH] =?UTF-8?q?=E5=93=88=E5=93=88=E4=BA=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SoftwareDesign/Code/ex01/main.cpp | 7 +- SoftwareDesign/Code/inc/matrix.hpp | 54 ++++++++ SoftwareDesign/Code/inc/vector.hpp | 201 +++++++++++++++++++++++++++++ 3 files changed, 259 insertions(+), 3 deletions(-) create mode 100644 SoftwareDesign/Code/inc/matrix.hpp create mode 100644 SoftwareDesign/Code/inc/vector.hpp diff --git a/SoftwareDesign/Code/ex01/main.cpp b/SoftwareDesign/Code/ex01/main.cpp index 8e9d5f2..caee0c6 100644 --- a/SoftwareDesign/Code/ex01/main.cpp +++ b/SoftwareDesign/Code/ex01/main.cpp @@ -1,6 +1,7 @@ +#include "../inc/matrix.hpp" #include int main() { - printf("hello world"); - return 0; -} \ No newline at end of file + Matrix a(2,2); + printf("哈哈了"); +} diff --git a/SoftwareDesign/Code/inc/matrix.hpp b/SoftwareDesign/Code/inc/matrix.hpp new file mode 100644 index 0000000..f34b1fa --- /dev/null +++ b/SoftwareDesign/Code/inc/matrix.hpp @@ -0,0 +1,54 @@ +#ifndef _MATRIX_HPP_ +#define _MATRIX_HPP_ + +#include "../inc/vector.hpp" + +template +class Matrix +{ +private: + int _height; + int _width; + Vector *_mat; + +protected: + +public: + + // 确定矩阵元素大小,不初始化 + Matrix(int w, int h); + // 以相同值初始化矩阵元素 + Matrix(int w, int h, T init_value); + // 以顺序访问数组的值初始化矩阵元素 + Matrix(int w, int h, const double *elements); + // 以另一个矩阵初始化当前矩阵的元素,实际是元素拷贝 + Matrix(const Matrix &another); + //只读访问内容 + const T get(int w, int h) const; + //运算符重载 + T &operator[](int i) { return _mat[i]; } +}; + +template +Matrix::Matrix(int w, int h) +{ + _width = w; + _height = h; + _mat = new Vector(w * h); +} + +template +Matrix::Matrix(int w, int h, T init_value) +{ + _width = w; + _height = h; + _mat = new Vector(w * h, init_value); +} + +template +const T Matrix::get(int w, int h) const +{ + return _mat[w * _width + h]; +} + +#endif \ No newline at end of file diff --git a/SoftwareDesign/Code/inc/vector.hpp b/SoftwareDesign/Code/inc/vector.hpp new file mode 100644 index 0000000..4df2b5e --- /dev/null +++ b/SoftwareDesign/Code/inc/vector.hpp @@ -0,0 +1,201 @@ +#ifndef _VECTOR_HPP_ +#define _VECTOR_HPP_ + +#include + +template +class Vector +{ +private: + T *_v; + int _len; + int _used; + + void expand(); //扩容 + void shrink(); //缩容 + +protected: + void adjust(); //调整容量接口 + +public: + //无参数构造函数 + Vector(); + + //初始长度构造函数 + Vector(int len); + + //构造函数-定义指定长度,相同初始值的向量 + // len 初始长度 + // value 初始值 + Vector(int len, T value); + + //析构函数 + ~Vector() { delete[] _v; } + + //获取值 + T get(int a) const; + + //操作符重载 + T& operator[](int i); + + //某处更改为某值 + T put(int a, T value); + + //获取已使用数量 + int getused() const { return _used; } + + //模扩张到某值 + void expandTo(int a); + + int search(T value); + + void swap(int a, int b); + + int insert(int locate, int value); + + int remove(int locate, int value); + + int remove_sorted(int value); + + int find(int value); + + void bubbleSort(); + + void printall(); + + int getlen(); + + int count(int value); + + void mergeSort(int lo, int hi); +}; + +template +Vector::Vector() +{ + _used = 1; + _len = 2; + _v = new T[_len]; +} + +template +Vector::Vector(int len) +{ + _used = len; + _len = 2 * len; + _v = new T[_len]; +} + +template +Vector::Vector(int len, T value) +{ + _used = len; + _len = 2 * len; + _v = new T[_len]; + for (int i = 0; i < _len; i++) + { + _v[i] = value; + } +} + +template +T Vector::get(int a) const +{ + + try + { + if (a >= 0 && a < _used) + return _v[a]; + else + throw "Error: Get vector data out of range!\n"; + } + + //异常处理 + catch (const char *msg) + { + std::cout << msg; + return 0; + } +} + +template +T& Vector::operator[](int a) +{ + try + { + if (a >= 0 && a < _used) + return _v[a]; + else + throw "Error: Request vercor data out of range!\n"; + } + + //异常处理 + catch (const char *msg) + { + std::cout << msg; + return 0; + } +} + +template +T Vector::put(int a, T value) +{ + + //若不够,就扩容 + while (a > _used - 1) + { + _used++; + adjust(); + } + + _v[a] = value; +} + +template +void Vector::expandTo(int a) +{ + while (a > _used - 1) + { + _used++; + adjust(); + } +} + +template +void Vector::expand() +{ + int old_len = _len; + _len = _len << 1; + T *p = new T[_len]; + + for (int i = 0; i < old_len; i++) + p[i] = _v[i]; + + delete[] _v; + _v = p; +} + +template +void Vector::shrink() +{ + _len = _len >> 1; + T *p = new T[_len]; + + for (int i = 0; i < _used; i++) + p[i] = _v[i]; + + delete[] _v; + _v = p; +} + +template +void Vector::adjust() +{ + if (((double)_used / (double)_len) < 0.25) + shrink(); + else if (((double)_used / (double)_len) > 0.75) + expand(); + return; +} + +#endif \ No newline at end of file