三元组表的C++语言描述
基本运算的算法——建立稀疏矩阵的三元组表的算法、按矩阵的列序转置算法、按矩阵的行序转置算法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86 |
#include<iostream> using
namespace std; template
< class
T> class
sanyuanzu { private : struct
Node { int
i; int
j; T data; Node( int
x, int y,T a){ i = x; j = y; data = a; } }; int
length = 0; int
maxSize; int
Rows; //矩阵行数 int
Cols; //矩阵列数 int
number; //非0个数 Node * dusk; public : sanyuanzu( int
Max = 10) { cout<< "Rows:" <<endl; cout<< "Cols:" <<endl; cout<< "Number:" <<endl; cin>>Rows>>Cols>>number; maxSize = Max; dusk[maxSize]; length = 0; } void
insertdata( int
x, int y,T a) { Node tem(x,y,a); dusk[length] = tem; length ++; } void
transpose(sanyuanzu & a) { int
q = 0; for ( int
col = 1;col <= Cols;col++) { for ( int
p = 0;p < number;p++ ) { if (dusk[p].j==col) { a.dusk[q].i = dusk[p].j; a.dusk[q].j = dusk[p].i; a.dusk[q].data = dusk[p].data; q++; } } } } int
sanyuanzu_number() { return
number; } void
print() { cout<<endl; for ( int
i = 0 ; i < number ; i++) { cout<<dusk[i].i<< " | " <<dusk[i].j<< " | " <<dusk[i].data<<endl; } } }; int
main() { cout<< "duskl1" <<endl; sanyuanzu< int > duskl1; cout<< "duskl2" <<endl; sanyuanzu< int > duskl2; for ( int
i = 0 ; i < duskl1.sanyuanzu_number() ; i++) { int
x,y,z; cin>>x>>y>>z; duskl1.insertdata(x,y,z); } duskl1.transpose(duskl2); duskl2.print(); } |
原文地址:http://www.cnblogs.com/Duskcl/p/3768535.html