The Code is as follows:
// Matrix triples multiplication by matrix addition # include <iostream> using namespace STD; typedef int elemtype; # define maxsize 12500 // maximum non-zero element typedef struct triple {elemtype value; int row, col;} triple; typedef struct tsmatrix {Triple Data [maxsize + 1]; int Mu, nu, Tu;} tsmatrix; tsmatrix t; void inputmatrix (tsmatrix & T) // enter T Non-zero elements {cout <"Enter the sparse matrix information (row, column, number of non-zero elements)" <Endl; CIN> T. mu> T. nu> T. tu; int I; cout <"enter the information of non-zero elements (rows, columns, values), and remind (subscript starts from 1)" <Endl; for (I = 1; I <= T. tu; ++ I) {CIN> T. data [I]. row> T. data [I]. col> T. data [I]. value ;}} void output (tsmatrix t) {cout <"Triple representation of the matrix (ROW =)" <t. mu <"Col =" <t. nu <"non-zero COUNT =" <t. tu <Endl; int I; for (I = 1; I <= T. tu; ++ I) {cout <"row (ROW):" <t. data [I]. row <"COL (column):" <t. data [I]. col <"value (value)" <t. data [I]. value <Endl ;}void transposesmatrix (tsmatrix M, tsmatrix & T) // transpose of the matrix {T. MU = m. nu; T. nu = m. mu; T. tu = m. tu; int I, j, k = 1; for (I = 1; I <= m. nu; ++ I) {for (J = 1; j <= m. tu; ++ J) if (M. data [J]. col = I) {T. data [K]. row = I; T. data [K]. col = m. data [J]. row; T. data [K]. value = m. data [J]. value; ++ K ;}} void addmastrix (tsmatrix M, tsmatrix T, tsmatrix & Q) // Add a matrix {int index_a, index_ B, I = 1, j = 1, k = 1; q. MU = m. mu; q. nu = m. nu; while (I <= m. tu & J <= T. tu) {index_a = (M. data [I]. row) * (M. data [I]. COL) + M. data [I]. col; index_ B = (T. data [J]. row) * (T. data [J]. COL) + T. data [J]. col; If (index_a <index_ B) {q. data [k] = m. data [I]; I ++; k ++;} else if (index_a> index_ B) {q. data [k] = T. data [J]; j ++; k ++;} else if (index_a = index_ B) {If (M. data [I]. value + T. data [J]. value )! = 0) {q. data [k] = m. data [I]; q. data [K]. value = m. data [I]. value + T. data [J]. value; k ++ ;}++ I ;++ J ;}// copy the remaining element for (; I <= m. tu; ++ I) {q. data [k] = m. data [I]; k ++;} For (; j <= T. tu; ++ J) Q. data [k ++] = T. data [J]; q. tu= K-1;} void multiply (tsmatrix M, tsmatrix T, tsmatrix & Q) {If (M. nu! = T. mu) {cerr <"illegal multiplication of two matrices" <Endl; return;} int * rowsize = new int [T. mu + 1]; // The number of non-zero elements in each row. int * rowstart = new int [T. mu + 2]; // each line in the matrix is at the starting position of the triplet int * temp = new int [T. nu + 1]; // stores the int I, current, K, rowm, Colm, COLB, for (I = 1; I <= T. mu; I ++) rowsize [I] = 0; for (I = 1; I <= T. tu; ++ I) rowsize [T. data [I]. row] ++; rowstart [1] = 1; for (I = 2; I <= T. mu + 1; I ++) rowstart [I] = rowstart [I-1] + rowsize [I-1]; current = 1; k = 1; while (current <= m. tu) {rowm = m. d ATA [current]. row; // The row number of the element in the current Triple Data for (I = 1; I <= T. nu; ++ I) temp [I] = 0; while (current <= m. tu & rowm = m. data [current]. row) {Colm = m. data [current]. col; // The column number of the current element. It is convenient to multiply the column number of the T matrix by (I = rowstart [Colm]; I <rowstart [Colm + 1]; I ++) // corresponds to the number of rows in the T matrix {COLB = T. data [I]. col; temp [COLB] + = (M. data [current]. value) * (T. data [I]. value);} current ++;} for (I = 1; I <= T. nu; I ++) {If (temp [I]! = 0) {q. data [K]. row = rowm; q. data [K]. col = I; q. data [K]. value = temp [I]; k ++;
}}} Q. MU = m. mu; q. nu = T. nu; q. tu= K-1;} int main () {tsmatrix T, M, Q, s; inputmatrix (m); inputmatrix (t ); cout <"multiply two matrices" <Endl; multiply (M, t, q); output (Q); cout <"add two matrices" <Endl; addmastrix (M, M, S); output (s); System ("pause"); Return 0 ;}