addition, multiplication, transpose:
1#include <iostream>2#include <cstring>3#include <cstdio>4#include <string>5#include <cmath>6#include <algorithm>7 using namespacestd;8template<classT>structNode {9 intx, y;Ten T Val; OneNode (intXinty, T val): X (x), Y (y), Val (val) {} A Node () {} - BOOL operator< (ConstNode &_a)Const { - returnX < _a.x | | x = = _a.x && y <_a.y; the } - BOOL operator== (ConstNode &_a)Const { - returnx = = _a.x && y = =_a.y; - } +Nodeoperator+ (ConstNode &_a) { - returnNode (x, Y, Val +_a.val); + } A voidSwapxy () { at swap (x, y); - } - }; -template<classT>structMatrix { - #defineMAXN 10 -Node<t>MATRIX[MAXN]; in intTotal , N, M; - voidcreat () { toPuts"Please enter the number of non-0 elements in the sparse matrix:"); +CIN >>Total ; -Puts"Please enter the number of rows for the matrix:"); theCIN >>N; *Puts"Please enter the number of columns in the matrix:"); $CIN >>m;Panax Notoginseng for(inti =0; I < total; i++) { - intx, y; the T Val; +printf"Please enter%d non-0 elements (row-labeled column-element values):", i +1); ACin >> x >> y >>Val; the if(X > N | | y >m) { +Puts"input Error, please re-enter! "); -i--; $ Continue; $ } -Matrix[i] = node<t>(x, Y, Val); - } theSort (matrix, Matrix +Total ); - }WuyiMatrixoperator+ (ConstMatrix &_a)Const { theMatrix<t> ans, tmp = * This; -Ans.total =0; WuANS.N =N; -ANS.M =m; About for(inti =0; i < _a.total; i++) { $tmp.matrix[tmp.total++] =_a.matrix[i]; - } -Sort (Tmp.matrix, Tmp.matrix +tmp.total); - for(inti =0; i < tmp.total; i++) { A if(Tmp.matrix[i] = = Tmp.matrix[i +1] && i < tmp.total-1) Tmp.matrix[i +1] = Tmp.matrix[i] + tmp.matrix[i +1]; + Elseans.matrix[ans.total++] =Tmp.matrix[i]; the } - returnans; $ } theMatrixoperator* (ConstMatrix &_a)Const { theMatrix<t>ans, tmp; theAns.total =0; theANS.N =N; -ANS.M =_A.M; inTmp.total =0; the for(inti =0; I < total; i++) { the for(intj =0; J < _a.total; J + +) { About if(matrix[i].y = = _a.matrix[j].x) tmp.matrix[tmp.total++] = node<t> (matrix[i].x, _A.MATRIX[J].Y, Matrix[i].val *_a.matrix[j].val); the } the } theSort (Tmp.matrix, Tmp.matrix +tmp.total); + for(inti =0; i < tmp.total; i++) { - if(Tmp.matrix[i] = = Tmp.matrix[i +1] && i < tmp.total-1) Tmp.matrix[i +1] = Tmp.matrix[i] + tmp.matrix[i +1]; the Elseans.matrix[ans.total++] =Tmp.matrix[i];Bayi } the returnans; the } - voidtranspose () { - swap (n, m); the for(inti =0; I < total; i++) { the swap (matrix[i].x, matrix[i].y); the } the } - voidoutp () { thePuts"the sparse matrix is:"); the intp =0; the for(inti =1; I <= N; i++) {94 for(intj =1; J <= M; J + +) { the if(i = = matrix[p].x && J = = Matrix[p].y && p <Total ) { thecout << Matrix[p].val <<" "; thep++;98 } About Elsecout <<"0"; - }101cout <<Endl;102 }103cout <<Endl;104 } the };106 intMain ()107 {108matrix<int>G, T, H, P;109cout <<"-----------------a matrix---------------\ n"; the g.creat ();111 G.OUTP (); thecout <<"-----------------B-Matrix---------------\ n";113 t.creat (); the T.OUTP (); theH = G +T; thecout <<"-----------------A + B--------------\ n";117 H.OUTP ();118cout <<"-----------------A + B results transpose-----\ n";119 h.transpose (); - H.OUTP ();121P = G *T;122cout <<"-----------------A * B----------------\ n";123 P.OUTP ();124 return 0; the}
View Code
[Data structure experiment: sparse matrix]