C++稀疏矩阵转置代码:
#include <iostream>
002 using namespace std;
003 //三元组
004 struct Trituple
005 {
006 int row, col; //非零元素的行号、列号
007 int val; //非零元素的值
008 };
009 //稀疏矩阵类声明
010 class SparseMatrix
011 {
012 //friend ostream & operator << (ostream &, SparseMatrix &);
013 //友元函数,输出流操作符重载
014 //friend istream & operator >> (istream &, SparseMatrix &);
015 //友元函数,输入流操作符重载
016 public:
017 SparseMatrix(int maxt = 100); //构造函数
018 ~SparseMatrix(); //析构函数
019 bool TransposeTo(SparseMatrix &); //转置
020 bool TransposeTo_Faster(SparseMatrix &);//快速转置
021 void AddTo(const SparseMatrix &);//加运算
022 bool Input(); //输入稀疏矩阵
023 void Output();//输出稀疏矩阵
024 private:
025 Trituple *data; //存储非零元素三元组的数组
026 int rows, cols, terms; //矩阵的行数、列数、非零元素个数
027 int maxterms; //数组data的大小
028 };
029 //构造函数,分配maxt个三元组结点的顺序空间,构造一个空的稀疏矩阵三元组表。
030 SparseMatrix::SparseMatrix(int maxt)
031 {
032 maxterms = maxt;
033 data = new Trituple [maxterms];
034 terms = rows = cols = 0;
035 }//SparseMatrix
036 //析构函数,将三元组表结构销毁。
037 SparseMatrix::~SparseMatrix()
038 {
039 if (data != NULL) delete [] data;
040 }//~SparseMatrix
041 /*//输出稀疏矩阵
042 ostream & operator << (ostream & out, SparseMatrix & M)
043 {
044 out << "rows=" << M.rows << endl;
045 out << "cols=" << M.cols << endl;
046 out << "terms=" << M.terms << endl;
047 for (int i = 0; i < M.terms; i++)
048 out << "data[" << M.data[i].row << "," << M.data[i].col << "]=" << M.data[i].val << endl;
049 return out;
050 };
051 //输入稀疏矩阵
052 istream & operator >> (istream & in, SparseMatrix & M)
053 {
054 cout << "输入稀疏矩阵的行数、列数以及非零元素个数" << endl;
055 in >> M.rows >> M.cols >> M.terms;
056 if (M.terms > M.maxterms) exit (1);
057 cout << "terms=" << M.terms << endl;
058 for (int i = 0; i < M.terms; i++){
059 cout << "输入非零元素的行号、列号和元素值 " << i + 1 << endl;
060 in >> M.data[i].row >> M.data[i].col >> M.data[i].val;
061 }
062 return in;
063 };*/
064 //转置,将*this的转置矩阵送入B
065 bool SparseMatrix::TransposeTo(SparseMatrix &B)
066 {
067 if(terms > B.maxterms)
068 return false;
069 B.rows = cols;
070 B.cols = rows;
071 B.terms = terms;
072 if (terms > 0) {
073 int p = 0;
074 for (int j = 1; j <= cols; j++)
075 for (int k = 0; k < terms; k++)
076 if (data[k].col == j) {
077 B.data[p].row = j;
078 B.data[p].col = data[k].row;
079 B.data[p].val = data[k].val;
080 p++;
081 }
082 }
083 return true;
084 }//TransposeTo
085 //快速转置,将*this的转置矩阵送入B
086 bool SparseMatrix::TransposeTo_Faster(SparseMatrix &B)
087 {
088 if (terms > B.maxterms)
089 return false;
090 B.rows = cols;
091 B.cols = rows;
092 B.terms = terms;
093 if (terms > 0) {
094 int *num, *cpot;
095 int j,k,p;
096 num = new int[cols];
097 cpot = new int[cols];
098 for (j = 0; j < cols; j++) //初始化num[]
099 num[j] = 0;
100 for (k = 0; k < terms; k++) //统计每一列的非零元素个数num[]
101 num[data[k].col - 1]++;
102 //求出B中每一行的起始下标cpot[]
103 cpot[0] = 0;
104 for(j = 1; j < cols; j++)
105 cpot[j] = cpot[j - 1] + num[j - 1];
106 //执行转置操作
107 for(k = 0; k < terms; k++) {
108 p = cpot[data[k].col - 1]++; //B中的位置
109 B.data[p].row = data[k].col;
110 B.data[p].col = data[k].row;
111 B.data[p].val = data[k].val;
112 }
113 delete [] num;
114 delete [] cpot;
115 }
116 return true;
117 }//TransposeTo_Faster
118 //输入稀疏矩阵
119 bool SparseMatrix::Input()
120 {
121 cout << "输入稀疏矩阵的行数、列数以及非零元素个数" << endl;
122 cin >> rows >> cols >> terms;
123 if (terms > maxterms) {
124 cout << "非零元个数太多!" << endl;
125 return false;
126 }
127 if (terms == 0)
128 return true;
129 cout << "按行序输入" << terms << "个非零元素的三元组" << endl;
130 for (int i = 0; i < terms; i++) {
131 cout << "输入第" << i + 1 << "个非零元素的行号、列号和元素值 " << endl;
132 cin >> data[i].row >> data[i].col >> data[i].val;
133 if (data[i].row < 1 || data[i].row > rows || data[i].col < 1 || data[i].col > cols) {
134 cout << "矩阵输入有误!" << endl;
135 return false;
136 }
137 }
138 return true;
139 }//Input
140 //输出稀疏矩阵
141 void SparseMatrix::Output()
142 {
143 cout << "rows=" << rows << endl;
144 cout << "cols=" << cols << endl;
145 cout << "terms=" << terms << endl;
146 for (int i = 0; i < terms; i++)
147 cout << "data[" << data[i].row << "," << data[i].col << "]=" << data[i].val << endl;
148 }//Output
149 int main()
150 {
151 SparseMatrix M(100);
152 if (M.Input()) {
153 cout << "原始矩阵:" << endl;
154 M.Output();
155 SparseMatrix T, S;
156 cout << "转置矩阵:" << endl;
157 if(M.TransposeTo(T))
158 T.Output();
159 if(M.TransposeTo_Faster(S))
160 S.Output();
161 system("pause");
162 return 1;
163 }
164 else
165 return 0;
166 }