From 4f8ed96d88b0ee4bdc43c6060cab92a60941c75b Mon Sep 17 00:00:00 2001 From: iridiumR Date: Mon, 21 Mar 2022 20:06:32 +0800 Subject: [PATCH 1/6] gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 83e2fa2..1875561 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ c_cpp_properties.json launch.json tasks.json settings.json +main From cf2381f4649018cad4d72d7b81eed897b3cf293a Mon Sep 17 00:00:00 2001 From: iridiumR Date: Mon, 21 Mar 2022 20:47:26 +0800 Subject: [PATCH 2/6] gitignore --- .gitignore | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/.gitignore b/.gitignore index 1875561..3660287 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,78 @@ launch.json tasks.json settings.json main + +**/build* + +# This file is used to ignore files which are generated +# ---------------------------------------------------------------------------- + +*~ +*.autosave +*.a +*.core +*.moc +*.o +*.obj +*.orig +*.rej +*.so +*.so.* +*_pch.h.cpp +*_resource.rc +*.qm +.#* +*.*# +core +!core/ +tags +.DS_Store +.directory +*.debug +Makefile* +*.prl +*.app +moc_*.cpp +ui_*.h +qrc_*.cpp +Thumbs.db +*.res +*.rc +/.qmake.cache +/.qmake.stash + +# qtcreator generated files +*.pro.user* + +# xemacs temporary files +*.flc + +# Vim temporary files +.*.swp + +# Visual Studio generated files +*.ib_pdb_index +*.idb +*.ilk +*.pdb +*.sln +*.suo +*.vcproj +*vcproj.*.*.user +*.ncb +*.sdf +*.opensdf +*.vcxproj +*vcxproj.* + +# MinGW generated files +*.Debug +*.Release + +# Python byte code +*.pyc + +# Binaries +# -------- +*.dll +*.exe From 2418962f687977331dc53c9dfde2bee78a5e9e61 Mon Sep 17 00:00:00 2001 From: iridiumR Date: Mon, 21 Mar 2022 20:47:53 +0800 Subject: [PATCH 3/6] =?UTF-8?q?=E6=96=B0=E5=BB=BA2-1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SoftwareDesign/Code/2-1/2-1.pro | 24 ++++++++++++++++++++++++ SoftwareDesign/Code/2-1/main.cpp | 11 +++++++++++ SoftwareDesign/Code/2-1/mainwindow.cpp | 15 +++++++++++++++ SoftwareDesign/Code/2-1/mainwindow.h | 21 +++++++++++++++++++++ SoftwareDesign/Code/2-1/mainwindow.ui | 22 ++++++++++++++++++++++ 5 files changed, 93 insertions(+) create mode 100644 SoftwareDesign/Code/2-1/2-1.pro create mode 100644 SoftwareDesign/Code/2-1/main.cpp create mode 100644 SoftwareDesign/Code/2-1/mainwindow.cpp create mode 100644 SoftwareDesign/Code/2-1/mainwindow.h create mode 100644 SoftwareDesign/Code/2-1/mainwindow.ui diff --git a/SoftwareDesign/Code/2-1/2-1.pro b/SoftwareDesign/Code/2-1/2-1.pro new file mode 100644 index 0000000..d01e364 --- /dev/null +++ b/SoftwareDesign/Code/2-1/2-1.pro @@ -0,0 +1,24 @@ +QT += core gui + +greaterThan(QT_MAJOR_VERSION, 4): QT += widgets + +CONFIG += c++11 + +# You can make your code fail to compile if it uses deprecated APIs. +# In order to do so, uncomment the following line. +#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 + +SOURCES += \ + main.cpp \ + mainwindow.cpp + +HEADERS += \ + mainwindow.h + +FORMS += \ + mainwindow.ui + +# Default rules for deployment. +qnx: target.path = /tmp/$${TARGET}/bin +else: unix:!android: target.path = /opt/$${TARGET}/bin +!isEmpty(target.path): INSTALLS += target diff --git a/SoftwareDesign/Code/2-1/main.cpp b/SoftwareDesign/Code/2-1/main.cpp new file mode 100644 index 0000000..fd3e533 --- /dev/null +++ b/SoftwareDesign/Code/2-1/main.cpp @@ -0,0 +1,11 @@ +#include "mainwindow.h" + +#include + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + MainWindow w; + w.show(); + return a.exec(); +} diff --git a/SoftwareDesign/Code/2-1/mainwindow.cpp b/SoftwareDesign/Code/2-1/mainwindow.cpp new file mode 100644 index 0000000..41a26bd --- /dev/null +++ b/SoftwareDesign/Code/2-1/mainwindow.cpp @@ -0,0 +1,15 @@ +#include "mainwindow.h" +#include "ui_mainwindow.h" + +MainWindow::MainWindow(QWidget *parent) + : QMainWindow(parent) + , ui(new Ui::MainWindow) +{ + ui->setupUi(this); +} + +MainWindow::~MainWindow() +{ + delete ui; +} + diff --git a/SoftwareDesign/Code/2-1/mainwindow.h b/SoftwareDesign/Code/2-1/mainwindow.h new file mode 100644 index 0000000..4643e32 --- /dev/null +++ b/SoftwareDesign/Code/2-1/mainwindow.h @@ -0,0 +1,21 @@ +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include + +QT_BEGIN_NAMESPACE +namespace Ui { class MainWindow; } +QT_END_NAMESPACE + +class MainWindow : public QMainWindow +{ + Q_OBJECT + +public: + MainWindow(QWidget *parent = nullptr); + ~MainWindow(); + +private: + Ui::MainWindow *ui; +}; +#endif // MAINWINDOW_H diff --git a/SoftwareDesign/Code/2-1/mainwindow.ui b/SoftwareDesign/Code/2-1/mainwindow.ui new file mode 100644 index 0000000..b232854 --- /dev/null +++ b/SoftwareDesign/Code/2-1/mainwindow.ui @@ -0,0 +1,22 @@ + + + MainWindow + + + + 0 + 0 + 800 + 600 + + + + MainWindow + + + + + + + + From b473499a29159038262f225682a24212f31911d3 Mon Sep 17 00:00:00 2001 From: iridiumR Date: Mon, 21 Mar 2022 22:54:10 +0800 Subject: [PATCH 4/6] =?UTF-8?q?2-1=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SoftwareDesign/Code/2-1/mainwindow.cpp | 9 ++++ SoftwareDesign/Code/2-1/mainwindow.h | 3 ++ SoftwareDesign/Code/2-1/mainwindow.ui | 58 +++++++++++++++++++++++--- 3 files changed, 65 insertions(+), 5 deletions(-) diff --git a/SoftwareDesign/Code/2-1/mainwindow.cpp b/SoftwareDesign/Code/2-1/mainwindow.cpp index 41a26bd..920463b 100644 --- a/SoftwareDesign/Code/2-1/mainwindow.cpp +++ b/SoftwareDesign/Code/2-1/mainwindow.cpp @@ -13,3 +13,12 @@ MainWindow::~MainWindow() delete ui; } + +void MainWindow::on_cal_clicked() +{ + int a = ui->num1->value(); + int b = ui->num2->value(); + ui->answer->setText(QString::number(a+b)); + +} + diff --git a/SoftwareDesign/Code/2-1/mainwindow.h b/SoftwareDesign/Code/2-1/mainwindow.h index 4643e32..07b6ec6 100644 --- a/SoftwareDesign/Code/2-1/mainwindow.h +++ b/SoftwareDesign/Code/2-1/mainwindow.h @@ -15,6 +15,9 @@ public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); +private slots: + void on_cal_clicked(); + private: Ui::MainWindow *ui; }; diff --git a/SoftwareDesign/Code/2-1/mainwindow.ui b/SoftwareDesign/Code/2-1/mainwindow.ui index b232854..033b8f0 100644 --- a/SoftwareDesign/Code/2-1/mainwindow.ui +++ b/SoftwareDesign/Code/2-1/mainwindow.ui @@ -6,15 +6,63 @@ 0 0 - 800 - 600 + 595 + 214 - MainWindow + 简易计算器 - - + + + + + 80 + 16 + 411 + 121 + + + + + 20 + + + + + + + + + + + + + + + + + + + = + + + + + + + + + + + + + 0 + 0 + 595 + 30 + + + From 35a9587d3dfeae4b5697294385b3948477d3ec71 Mon Sep 17 00:00:00 2001 From: iridiumR Date: Mon, 21 Mar 2022 23:27:34 +0800 Subject: [PATCH 5/6] =?UTF-8?q?2-2-1=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SoftwareDesign/Code/2-1/.gitignore | 0 SoftwareDesign/Code/2-2-1/2-2.pro | 24 +++++ SoftwareDesign/Code/2-2-1/main.cpp | 11 ++ SoftwareDesign/Code/2-2-1/mainwindow.cpp | 17 +++ SoftwareDesign/Code/2-2-1/mainwindow.h | 21 ++++ SoftwareDesign/Code/2-2-1/mainwindow.ui | 125 +++++++++++++++++++++++ 6 files changed, 198 insertions(+) create mode 100644 SoftwareDesign/Code/2-1/.gitignore create mode 100644 SoftwareDesign/Code/2-2-1/2-2.pro create mode 100644 SoftwareDesign/Code/2-2-1/main.cpp create mode 100644 SoftwareDesign/Code/2-2-1/mainwindow.cpp create mode 100644 SoftwareDesign/Code/2-2-1/mainwindow.h create mode 100644 SoftwareDesign/Code/2-2-1/mainwindow.ui diff --git a/SoftwareDesign/Code/2-1/.gitignore b/SoftwareDesign/Code/2-1/.gitignore new file mode 100644 index 0000000..e69de29 diff --git a/SoftwareDesign/Code/2-2-1/2-2.pro b/SoftwareDesign/Code/2-2-1/2-2.pro new file mode 100644 index 0000000..d01e364 --- /dev/null +++ b/SoftwareDesign/Code/2-2-1/2-2.pro @@ -0,0 +1,24 @@ +QT += core gui + +greaterThan(QT_MAJOR_VERSION, 4): QT += widgets + +CONFIG += c++11 + +# You can make your code fail to compile if it uses deprecated APIs. +# In order to do so, uncomment the following line. +#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 + +SOURCES += \ + main.cpp \ + mainwindow.cpp + +HEADERS += \ + mainwindow.h + +FORMS += \ + mainwindow.ui + +# Default rules for deployment. +qnx: target.path = /tmp/$${TARGET}/bin +else: unix:!android: target.path = /opt/$${TARGET}/bin +!isEmpty(target.path): INSTALLS += target diff --git a/SoftwareDesign/Code/2-2-1/main.cpp b/SoftwareDesign/Code/2-2-1/main.cpp new file mode 100644 index 0000000..fd3e533 --- /dev/null +++ b/SoftwareDesign/Code/2-2-1/main.cpp @@ -0,0 +1,11 @@ +#include "mainwindow.h" + +#include + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + MainWindow w; + w.show(); + return a.exec(); +} diff --git a/SoftwareDesign/Code/2-2-1/mainwindow.cpp b/SoftwareDesign/Code/2-2-1/mainwindow.cpp new file mode 100644 index 0000000..7c743ac --- /dev/null +++ b/SoftwareDesign/Code/2-2-1/mainwindow.cpp @@ -0,0 +1,17 @@ +#include "mainwindow.h" +#include "ui_mainwindow.h" + +MainWindow::MainWindow(QWidget *parent) + : QMainWindow(parent) + , ui(new Ui::MainWindow) +{ + ui->setupUi(this); + +} + +MainWindow::~MainWindow() +{ + delete ui; +} + + diff --git a/SoftwareDesign/Code/2-2-1/mainwindow.h b/SoftwareDesign/Code/2-2-1/mainwindow.h new file mode 100644 index 0000000..4643e32 --- /dev/null +++ b/SoftwareDesign/Code/2-2-1/mainwindow.h @@ -0,0 +1,21 @@ +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include + +QT_BEGIN_NAMESPACE +namespace Ui { class MainWindow; } +QT_END_NAMESPACE + +class MainWindow : public QMainWindow +{ + Q_OBJECT + +public: + MainWindow(QWidget *parent = nullptr); + ~MainWindow(); + +private: + Ui::MainWindow *ui; +}; +#endif // MAINWINDOW_H diff --git a/SoftwareDesign/Code/2-2-1/mainwindow.ui b/SoftwareDesign/Code/2-2-1/mainwindow.ui new file mode 100644 index 0000000..8c90bf1 --- /dev/null +++ b/SoftwareDesign/Code/2-2-1/mainwindow.ui @@ -0,0 +1,125 @@ + + + MainWindow + + + + 0 + 0 + 800 + 600 + + + + MainWindow + + + + + + 120 + 70 + 521 + 451 + + + + + 30 + + + + + 0 + + + + + 1 + + + + + + + 2 + + + + + + + 3 + + + + + + + + + 4 + + + + + + + 5 + + + + + + + 6 + + + + + + + + + + 9 + + + + + + + 7 + + + + + + + 8 + + + + + + + + + + + + 140 + 10 + 341 + 25 + + + + ***REMOVED*** ***REMOVED*** + + + + + + + From d63b1a82098072ba27607efdd6d5d06884eecec2 Mon Sep 17 00:00:00 2001 From: iridiumR Date: Tue, 22 Mar 2022 15:57:58 +0800 Subject: [PATCH 6/6] =?UTF-8?q?=E6=B4=BE=E7=94=9F=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SoftwareDesign/Code/inc/matrix.hpp | 632 +++++++++++++++-------------- 1 file changed, 321 insertions(+), 311 deletions(-) mode change 100644 => 100755 SoftwareDesign/Code/inc/matrix.hpp diff --git a/SoftwareDesign/Code/inc/matrix.hpp b/SoftwareDesign/Code/inc/matrix.hpp old mode 100644 new mode 100755 index 4243d8a..1f12f70 --- a/SoftwareDesign/Code/inc/matrix.hpp +++ b/SoftwareDesign/Code/inc/matrix.hpp @@ -1,312 +1,322 @@ -/* - * @Author: iR - * @Date: 2022-03-11 16:44:46 - * @LastEditors: iR - * @LastEditTime: 2022-03-12 17:25:28 - * @FilePath: \Code\inc\matrix.hpp - * @Description: - * - * Copyright (c) 2022 by iR, All Rights Reserved. - */ -#ifndef _MATRIX_HPP_ -#define _MATRIX_HPP_ - -#include -#include - -#include "../inc/vector.hpp" - -template -class Matrix -{ -private: - int _height; - int _width; - Vector *_mat; - -protected: -public: - ~Matrix() - { - delete _mat; - } - /** - * @description: 极度缺省初始化, 大小为2*2, 初值为0 - * @param {int} w - * @param {int} h - */ - Matrix(); - /** - * @description: 缺省初始化,初值为0 - * @param {int} w - * @param {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); - - /** - * @description: 以顺序访问数组的值初始化矩阵元素 - * 必须给出正确的数组,因为在函数内无法判定数组是否正确 - * @param {int} w - * @param {int} h - * @param {const T} elements[] - */ - Matrix(int w, int h, const T elements[]); - - // /** - // * @description: 以顺序访问指针的值初始化矩阵元素 - // * @note: 必须给出正确的数组,因为在函数内无法判定数组是否正确 - // * @param {int} w - // * @param {int} h - // * @param {T*} data - // */ - // Matrix(int w, int h, T *data); - - /** - * @description: 以另一个矩阵初始化当前矩阵的元素,实际是元素拷贝 - * @param {Matrix} &another - */ - Matrix(const Matrix &another); - - /** - * @description: 只读访问内容 - * @param {int} w - * @param {int} h - * @return {*} - */ - const T get(int w, int h) const; - - /** - * @description: []运算符重载 - */ - const T operator[](int i) const { return _mat->get(i); } - - /** - * @description: =运算符重载 - */ - T operator=(const Matrix &b); - - /** - * @description: +=运算符重载 - * @note: 即 a += b,其中*this即为a - * @param {*} - * @return None - */ - void operator+=(const Matrix &b); - - /** - * @description: +=运算符重载 - * @note:即 c = a + b,其中*this即为a,返回值即为c - * @param {*} - * @return None - */ - Matrix operator+(const Matrix &b) const; - - /** - * @description: 可读可写访问 - * @param {int} w - * @param {int} h - * @return {*} - */ - T &at(int w, int h) - { - T &ref = _mat->get(h * _width + w); - return ref; - } - - /** - * @description: 只读引用访问 - * @param {int} w - * @param {int} h - * @return {*} - */ - const T &at(int w, int h) const - { - T &ref = _mat->get(h * _width + w); - return ref; - } - - const int getWidth() const { return _width; } - const int getHeight() const { return _height; } -}; - -template -Matrix::Matrix() -{ - _width = 2; - _height = 2; - _mat = new Vector(2 * 2, 0); -} - -template -Matrix::Matrix(int w, int h) -{ - _width = w; - _height = h; - _mat = new Vector(w * h, 0); -} - -template -Matrix::Matrix(int w, int h, T init_value) -{ - _width = w; - _height = h; - _mat = new Vector(w * h, init_value); -} - -template -Matrix::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(w * h); - for (int i = 0; i < size; i++) - _mat->put(i, elements[i]); - } - catch (const char *msg) - { - std::cout << msg << "SIZE:" << size << std::endl; - ; - } -} - -//这个方法似乎有问题 -// template -// Matrix::Matrix(int w, int h, T *data) -// { -// 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"; -// if (data == NULL) -// throw 1; -// _width = w; -// _height = h; -// _mat = new Vector; -// for (int i = 0; i < size; i++) -// _mat->put(i, *(data+i)); -// } - -// catch (const int a) -// { -// if (a == 1) -// { -// std::cout << "Error: Empty array. Set elements to 0." << std::endl; -// for (int i = 0; i < size; i++) -// _mat->put(i, 0); -// } -// } -// } - -template -Matrix::Matrix(const Matrix &another) -{ - try - { - _width = another.getWidth(); - _height = another.getHeight(); - _mat = new Vector; - for (int i = 0; i < _width * _height; i++) - _mat->put(i, another[i]); - } - catch (const char *msg) - { - std::cout << msg; - } -} - -template -T Matrix::operator=(const Matrix &another) -{ - try - { - _width = another.getWidth(); - _height = another.getHeight(); - _mat = new Vector; - for (int i = 0; i < _width * _height; i++) - _mat->put(i, another[i]); - } - catch (const char *msg) - { - std::cout << msg; - } -} - -template -const T Matrix::get(int w, int h) const -{ - return _mat->get(h * _width + w); -} - -template -void Matrix::operator+=(const Matrix &b) -{ - try - { - if (this->_height == b.getHeight() && this->_width == b.getWidth()) - { - for (int i = 0; i < _width * _height; i++) - this->_mat->put(i, (*this)[i] + b[i]); - - return; - } - throw "Error: Different size."; - } - catch (const char *msg) - { - std::cout << msg; - } -} - -/** - * @description: +=运算符重载 - * @note:即 c = a + b,其中*this即为a,返回值即为c - * @param {*} - * @return None - */ -template -Matrix Matrix::operator+(const Matrix &b) const -{ - int h = this->_height; - int w = this->_width; - try - { - if (h == b.getHeight() && w == b.getWidth()) - { - Matrix temp(w, h, T(0)); - temp += *this; - temp += b; - return temp; - } - - throw "Error: Different size."; - } - catch (const char *msg) - { - std::cout << msg; - Matrix temp(w, h, T(0)); - return temp; - } -} - +/* + * @Author: iR + * @Date: 2022-03-11 16:44:46 + * @LastEditors: iR + * @LastEditTime: 2022-03-15 17:02:42 + * @FilePath: \Code\inc\matrix.hpp + * @Description: + * + * Copyright (c) 2022 by iR, All Rights Reserved. + */ +#ifndef _MATRIX_HPP_ +#define _MATRIX_HPP_ + +#include +#include + +#include "../inc/vector.hpp" + +template +class MatBase +{ // 定义基类,其中包含了矩阵元素读取的纯虚函数 +public: + virtual T &at(int y, int x) = 0; // 定义纯虚函数 + virtual const T &at(int y, int x) const = 0; // 定义纯虚函数 +}; + +template +class Matrix : public MatBase +{ +private: + int _height; + int _width; + Vector *_mat; + +protected: +public: + ~Matrix() + { + delete _mat; + } + /** + * @description: 极度缺省初始化, 大小为2*2, 初值为0 + * @param {int} w + * @param {int} h + */ + Matrix(); + /** + * @description: 缺省初始化,初值为0 + * @param {int} w + * @param {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); + + /** + * @description: 以顺序访问数组的值初始化矩阵元素 + * 必须给出正确的数组,因为在函数内无法判定数组是否正确 + * @param {int} w + * @param {int} h + * @param {const T} elements[] + */ + Matrix(int w, int h, const T elements[]); + + // /** + // * @description: 以顺序访问指针的值初始化矩阵元素 + // * @note: 必须给出正确的数组,因为在函数内无法判定数组是否正确 + // * @param {int} w + // * @param {int} h + // * @param {T*} data + // */ + // Matrix(int w, int h, T *data); + + /** + * @description: 以另一个矩阵初始化当前矩阵的元素,实际是元素拷贝 + * @param {Matrix} &another + */ + Matrix(const Matrix &another); + + /** + * @description: 只读访问内容 + * @param {int} w + * @param {int} h + * @return {*} + */ + const T get(int w, int h) const; + + /** + * @description: []运算符重载 + */ + const T operator[](int i) const { return _mat->get(i); } + + /** + * @description: =运算符重载 + */ + void operator=(const Matrix &b); + + /** + * @description: +=运算符重载 + * @note: 即 a += b,其中*this即为a + * @param {*} + * @return None + */ + void operator+=(const Matrix &b); + void operator+=(T &a) + { + for (int i = 0; i < _width * _height;i++) + { + _mat[i] = a; + } + } + + /** + * @description: +=运算符重载 + * @note:即 c = a + b,其中*this即为a,返回值即为c + * @param {*} + * @return None + */ + Matrix operator+(const Matrix &b) const; + + /** + * @description: 可读可写访问 + * @param {int} w + * @param {int} h + * @return {*} + */ + T &at(int w, int h) + { + T &ref = _mat->get(h * _width + w); + return ref; + } + + /** + * @description: 只读引用访问 + * @param {int} w + * @param {int} h + * @return {*} + */ + const T &at(int w, int h) const + { + T &ref = _mat->get(h * _width + w); + return ref; + } + + const int getWidth() const { return _width; } + const int getHeight() const { return _height; } +}; + +template +Matrix::Matrix() +{ + _width = 2; + _height = 2; + _mat = new Vector(2 * 2, 0); +} + +template +Matrix::Matrix(int w, int h) +{ + _width = w; + _height = h; + _mat = new Vector(w * h, 0); +} + +template +Matrix::Matrix(int w, int h, T init_value) +{ + _width = w; + _height = h; + _mat = new Vector(w * h, init_value); +} + +template +Matrix::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(w * h); + for (int i = 0; i < size; i++) + _mat->put(i, elements[i]); + } + catch (const char *msg) + { + std::cout << msg << "SIZE:" << size << std::endl; + ; + } +} + +//这个方法似乎有问题 +// template +// Matrix::Matrix(int w, int h, T *data) +// { +// 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"; +// if (data == NULL) +// throw 1; +// _width = w; +// _height = h; +// _mat = new Vector; +// for (int i = 0; i < size; i++) +// _mat->put(i, *(data+i)); +// } + +// catch (const int a) +// { +// if (a == 1) +// { +// std::cout << "Error: Empty array. Set elements to 0." << std::endl; +// for (int i = 0; i < size; i++) +// _mat->put(i, 0); +// } +// } +// } + +template +Matrix::Matrix(const Matrix &another) +{ + try + { + _width = another.getWidth(); + _height = another.getHeight(); + _mat = new Vector; + for (int i = 0; i < _width * _height; i++) + _mat->put(i, another[i]); + } + catch (const char *msg) + { + std::cout << msg; + } +} + +template +void Matrix::operator=(const Matrix &another) +{ + try + { + _width = another.getWidth(); + _height = another.getHeight(); + _mat = new Vector; + for (int i = 0; i < _width * _height; i++) + _mat->put(i, another[i]); + + } + catch (const char *msg) + { + std::cout << msg; + } +} + +template +const T Matrix::get(int w, int h) const +{ + return _mat->get(h * _width + w); +} + +template +void Matrix::operator+=(const Matrix &b) +{ + try + { + if (this->_height == b.getHeight() && this->_width == b.getWidth()) + { + for (int i = 0; i < _width * _height; i++) + this->_mat->put(i, (*this)[i] + b[i]); + + return; + } + throw "Error: Different size."; + } + catch (const char *msg) + { + std::cout << msg; + } +} + +template +Matrix Matrix::operator+(const Matrix &b) const +{ + int h = this->_height; + int w = this->_width; + try + { + if (h == b.getHeight() && w == b.getWidth()) + { + Matrix temp(w, h, T(0)); + temp += *this; + temp += b; + return temp; + } + + throw "Error: Different size."; + } + catch (const char *msg) + { + std::cout << msg; + Matrix temp(w, h, T(0)); + return temp; + } +} + #endif \ No newline at end of file