# Include <iostream> # include <iomanip> # include "windows. H "using namespace STD; struct tripple {int X, Y, value ;}; struct tripplematrix {int R, C, CNT; tripple * tripples ;}; tripplematrix * createtripplematrix (int r, int C, int maxcnt) {tripplematrix * P = (tripplematrix *) malloc (sizeof (tripplematrix); P-> r = R; p-> C = C; P-> CNT = 0; P-> tripples = new tripple [maxcnt]; return P;} tripplematrix * transpose (tripplematrix * SRC) {tripplematrix * ans = createtripplematrix (SRC-> C, Src-> r, Src-> CNT); For (INT I = 1; I <= ANS-> r; I ++) {for (Int J = 0; j <Src-> CNT; j ++) {If (SRC-> tripples [J]. y = I) {tripple newtripple; newtripple. X = I; newtripple. y = Src-> tripples [J]. x; newtripple. value = Src-> tripples [J]. value; ANS-> tripples [ANS-> CNT ++] = newtripple ;}}return ans;} void output (tripplematrix * SRC) {int cur = 0; for (INT I = 1; I <= Src-> r; I ++) {for (Int J = 1; j <= Src-> C; j ++) {If (SRC-> tripples [cur]. X = I & Src-> tripples [cur]. y = J) {setconsoletextattribute (getstdhandle (std_output_handle), foreground_intensity | foreground_green); // displays non-0 yuan cout <Src-> tripples [cur]. value <"; setconsoletextattribute (getstdhandle (std_output_handle), foreground_intensity | foreground_red | foreground_green | foreground_blue); // set three colors to add cur ++ ;} else cout <"0" ;}cout <Endl ;}void main () {setconsoletextattribute (getstdhandle (handle), role | foreground_red | foreground_green | foreground_blue ); // set the three-color addition int r = 12, c = 17, maxcnt = 40; tripplematrix * m = createtripplematrix (R, C, maxcnt); For (INT I = 1; I <= r; I ++) {for (Int J = 1; j <= C; j ++) {If (RAND () % 10 = 1 & M-> CNT <maxcnt) {M-> tripples [M-> CNT]. X = I; m-> tripples [M-> CNT]. y = J; m-> tripples [M-> CNT]. value = rand () % 10; m-> CNT ++ ;}} output (m); cout <Endl; output (transpose (m )); cin> r ;}
Stack queue and array-triple Sparse Matrix