Sparse Matrix (Triple Table)
October 13, 2018
简单实现了稀疏矩阵的加法减法乘法和转置
Tsmatrix.h
#include <iostream> using namespace std; #define TRUE 1 #define FALSE 0 #define OK 1 #define ERROR 0 #define INFEASIBLE -1 #define LOVERFLOW -2 //避免与<math.h>中的定义冲突 typedef int Status; typedef int ElemType; #define MAXSIZE 12500 class TSMatrix; //类的提前声明 class Triple { protected: int i, j; ElemType e; public: friend istream& operator >> (istream &in, Triple &t); //输入一个Triple型对象 friend ostream& operator << (ostream &out, const Triple &t); //输出一个Triple型对象 friend class TSMatrix; //友元 friend istream& operator >> (istream &in, TSMatrix &m); //取代CreateSMatrix功能,以三元组表的形式输入一个稀疏矩阵 friend ostream& operator << (ostream &out, const TSMatrix &m); //取代PrintSMatrix,以矩阵形式输出 }; class TSMatrix { protected: Triple *data; int mu, nu, tu; public: TSMatrix(); //增加,对TSMatrix类的对象进行初始化,称为空胡振 TSMatrix(const TSMatrix &m); //增加的复制构造函数,完成TSMatrix类的对象的复制(三种调用时机) ~TSMatrix(); //取代DestroySMatrix /* cin/cout的重载 */ friend istream& operator >> (istream &in, TSMatrix &m); //取代CreateSMatrix功能,以三元组表的形式输入一个稀疏矩阵 friend ostream& operator << (ostream &out, const TSMatrix &m); //取代PrintSMatrix,以矩阵形式输出 /* 成员函数形式的运算符重载 */ TSMatrix& operator=(const TSMatrix m); //取代CopySMatrix,T=M表示把M矩阵复制到T中 TSMatrix operator+(const TSMatrix &m); //和=重载一起,取代AddSMatrix, Q=M+N 表示求稀疏矩阵M+N的和 TSMatrix operator-(const TSMatrix &m); //和=重载一起,取代SubSMatrix, Q=M-N 表示求稀疏矩阵M-N的差 TSMatrix operator*(const TSMatrix &m); //和=重载一起,取代MultSMatrix,Q=M*N 表示求稀疏矩阵M*N的乘积 TSMatrix operator!(); //和=重载一起,取代TranspostSMatrix,!M 表示求M的转置矩阵 };
Tsmatrix.cpp
#include <iostream> #include <stdlib.h> #include <algorithm> #include <math.h> #include <string.h> #include "Tsmatrix.h" using namespace std; istream &operator>>(istream &in, Triple &t) { return in; } ostream &operator<<(ostream &out, const Triple &t) { return out; } istream &operator>>(istream &in, TSMatrix &m) { in >> m.mu >> m.nu >> m.tu; m.data = new(nothrow) Triple[m.tu + 1]; for (int i = 1; i <= m.tu; i++) in >> m.data[i].i >> m.data[i].j >> m.data[i].e; return in; } ostream &operator<<(ostream &out, const TSMatrix &m) { for (int i = 0; i < m.mu; i++) { for (int j = 0; j < m.nu; j++) { bool flag = 0; for (int k = 1; k <= m.tu; k++) if (i == m.data[k].i&&j == m.data[k].j) { out << m.data[k].e << " "; flag = 1; break; } if (flag == 0) out << 0 << " "; } out << endl; } return out; } TSMatrix::TSMatrix() { mu = nu = tu = 0; data = NULL; } TSMatrix::TSMatrix(const TSMatrix &m) { data = new(nothrow) Triple[m.tu + 1]; for (int i = 1; i <= m.tu; i++) data[i] = m.data[i]; mu = m.mu; nu = m.nu; tu = m.tu; } TSMatrix::~TSMatrix() { delete[]data; } TSMatrix &TSMatrix::operator=(const TSMatrix m) { if (data) delete[]data; data = new(nothrow) Triple[m.tu + 1]; for (int i = 1; i <= m.tu; i++) { data[i].i = m.data[i].i; data[i].j = m.data[i].j; data[i].e = m.data[i].e; } mu = m.mu; nu = m.nu; tu = m.tu; return *this; } TSMatrix TSMatrix::operator+(const TSMatrix &m) { int i = 1, j = 1, k = 1; TSMatrix C(m); C.mu = mu; C.nu = nu; while (i <= tu && j <= m.tu) { if (data[i].i < m.data[j].i) { C.data[k].i = data[i].i; C.data[k].j = data[i].j; C.data[k].e = data[i].e; i++; k++; } else if (data[i].i > m.data[j].i) { C.data[k].i = m.data[j].i; C.data[k].j = m.data[j].j; C.data[k].e = m.data[j].e; j++; k++; } else { if (data[i].j < m.data[j].j) { C.data[k].i = data[i].i; C.data[k].j = data[i].j; C.data[k].e = data[i].e; i++; k++; } else if (data[i].j > m.data[j].j) { C.data[k].i = m.data[j].i; C.data[k].j = m.data[j].j; C.data[k].e = m.data[j].e; j++; k++; } else { if (data[i].e + m.data[j].e == 0) { i++; j++; } else { C.data[k].i = data[i].i; C.data[k].j = data[i].j; C.data[k].e = data[i].e + m.data[j].e; i++; j++; k++; } } } } while (i > tu && j <= m.tu) { C.data[k].i = m.data[j].i; C.data[k].j = m.data[j].j; C.data[k].e = m.data[j].e; j++; k++; } while (i <= tu && j > m.tu) { C.data[k].i = data[i].i; C.data[k].j = data[i].j; C.data[k].e = data[i].e; i++; k++; } C.tu = k - 1; return C; } TSMatrix TSMatrix::operator-(const TSMatrix &m) { TSMatrix c(m); for (int k = 1; k <= c.tu; k++) c.data[k].e = -c.data[k].e; return (*this) + c; } TSMatrix TSMatrix::operator*(const TSMatrix &m) { TSMatrix Q; int i = 1, j, k = 1, q; Q.data = new(nothrow) Triple[(mu * m.nu)]; Q.mu = mu; Q.nu = m.nu; while (i <= tu) { for (j = 1; j <= tu; j++) { if ((data + i)->j == (m.data + j)->i) { (Q.data + k)->i = (data + i)->i; (Q.data + k)->j = (m.data + j)->j; (Q.data + k)->e = (data + i)->e * (m.data + j)->e; for (q = 0; q < k; q++) if ((Q.data + k)->i == (Q.data + q)->i && (Q.data + k)->j == (Q.data + q)->j) (Q.data + q)->e += (Q.data + k)->e; k++; } } i++; } Q.tu = k + 1; return Q; } TSMatrix TSMatrix::operator!() { TSMatrix T; T.data = new(nothrow) Triple[tu + 1]; int q = 1; T.mu = nu; T.nu = mu; T.tu = tu; for (int j = 0; j < mu; j++) for (int p = 1; p <= tu; p++) if (data[p].j == j) { T.data[q].i = data[p].j; T.data[q].j = data[p].i; T.data[q].e = data[p].e; ++q; } return T; }