c++实验4
面向对象的矩阵运算编程
#pragma once
#define _CRT_SECURE_NO_WARNINGS
#include <iomanip>
#include <exception>
#include <typeinfo>
#include <string.h>
template <typename T>
class MAT {
T* const e;
const int r, c;
public:
MAT(int r, int c);
MAT(const MAT& a);
MAT(MAT&& a)noexcept;
virtual ~MAT()noexcept;
virtual T* const operator[ ](int r);
virtual MAT operator+(const MAT& a)const;
virtual MAT operator-(const MAT& a)const;
virtual MAT operator*(const MAT& a)const;
virtual MAT operator~()const;
virtual MAT& operator=(const MAT& a);
virtual MAT& operator=(MAT&& a)noexcept;
virtual MAT& operator+=(const MAT& a);
virtual MAT& operator-=(const MAT& a);
virtual MAT& operator*=(const MAT& a);
virtual char* print(char* s)const noexcept;
};
template <typename T>
MAT<T>::MAT(int r, int c) :r(r), c(c), e(nullptr) {
(T*&)e = new T[r * c];
}
template <typename T>
MAT<T>::MAT(const MAT& a) : r(a.r), c(a.c),e(nullptr) {
(T*&)e = new T[r * c];
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
this->e[i * c + j] = a.e[i * c + j];
}
}
}
template<typename T>
MAT<T>::MAT(MAT&& a)noexcept :r(a.r), c(a.c), e() {
(T*&)e = a.e;
(T*&)a.e = nullptr;
(int&)a.r = 0;
(int&)a.c = 0;
}
template<typename T>
MAT<T>::~MAT()noexcept {
if (this->e!=nullptr)delete[]e;
(T*&)e = nullptr;
}
template<typename T>
T* const MAT<T>::operator[](int r) {
if (r >= this->r) {
throw"数组越界";
return nullptr;
}
return &(e[r * c]);
}
template<typename T>
MAT<T> MAT<T>::operator+(const MAT& a)const {
if (a.r != r || a.c != c) {
throw"error";
return *this;
}
MAT<T> temp(r, c);
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
temp[i][j] = e[i * c + j] + a.e[i * c + j];
}
}
return temp;
}
template<typename T>
MAT<T> MAT<T>::operator-(const MAT& a)const {
if (a.r != r || a.c != c) {
throw"error";
return *this;
}
MAT<T> temp(r, c);
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
temp[i][j] = e[i * c + j] - a.e[i * c + j];
}
}
return temp;
}
template<typename T>
MAT<T> MAT<T>::operator*(const MAT& a)const {
if (c != a.r) {
throw"行列数不匹配,无法相乘";
return *this;
}
MAT<T> temp(r, a.c);
for (int i = 0; i < r; i++) {
for (int j = 0; j < a.c; j++) {
temp.e[i * a.c + j] = 0;
for (int k = 0; k < a.c; k++) {
temp.e[i * a.c + j] += e[i * a.c + k] * a.e[k * r + j];
}
}
}
return temp;
}
template <typename T>
MAT<T> MAT<T>::operator~()const {
if (r == 0 || c == 0) {
return *this;
}
MAT<T> temp(c, r);
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
temp.e[j * r + i] = e[i * c + j];
}
}
return temp;
}
template<typename T>
MAT<T>& MAT<T>::operator=(const MAT& a) {
if (e == a.e) {
return *this;
}
if (this->e)delete[]e;
(T*&)e = new T[r * c];
(int&)r = a.r;
(int&)c = a.c;
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
this->e[i * c + j] = a.e[i * c + j];
}
}
return *this;
}
template<typename T>
MAT<T>& MAT<T>::operator=(MAT&& a)noexcept {
if (this->e == a.e) {
return *this;
}
if (this->e)delete[]e;
(int&)r = a.r;
(int&)c = a.c;
(T*&)e = a.e;
(T*&)a.e = nullptr;
(int&)a.r = (int&)a.c = 0;
return *this;
}
template<typename T>
MAT<T>& MAT<T>::operator+=(const MAT& a) {
if (a.r != r || a.c != c) {
throw"error";
return *this;
}
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
e[i * c + j] += a.e[i * c + j];
}
}
return *this;
}
template<typename T>
MAT<T>& MAT<T>::operator-=(const MAT& a) {
if (a.r != r || a.c != c) {
throw"error";
return *this;
}
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
e[i * c + j] -= a.e[i * c + j];
}
}
return *this;
}
template<typename T>
MAT<T>& MAT<T>::operator*=(const MAT& a) {
if (this->c != a.r) {
throw"行列数不匹配,无法相乘";
return *this;
}
MAT<T> temp(r, a.c);
for (int i = 0; i < r; i++) {
for (int j = 0; j < a.c; j++) {
temp.e[i * a.c + j] = 0;
for (int k = 0; k < a.c; k++) {
temp.e[i * a.c + j] += e[i * a.c + k] * a.e[k * r + j];
}
}
}
this->operator=((MAT<T>&&)temp);
return *this;
}
template<>
char* MAT<_int64>::print(char* s)const noexcept {
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
if (i + j == 0)sprintf(s, "%d ", e[i * c + j]);
else sprintf(s + strlen(s), "%d ", e[i * c + j]);
}
sprintf(s + strlen(s), "\n");
}
return s;
}
template<>
char* MAT<
int>::print(char* s)const noexcept {
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
if (i + j == 0)sprintf(s, "%d ", e[i * c + j]);
else sprintf(s + strlen(s), "%d ", e[i * c + j]);
}
sprintf(s + strlen(s), "\n");
}
return s;
}
template<>
char* MAT<long>::print(char* s)const noexcept {
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
if (i + j == 0)sprintf(s, "%f ", e[i * c + j]);
else sprintf(s + strlen(s), "%f ", e[i * c + j]);
}
sprintf(s + strlen(s), "\n");
}
return s;
}