求稀疏矩阵(n*n,n>=10)转置矩阵..要求用三元组表显示
那个哥哥知道请把全部程序告诉我,,,,明天一个MM上机要考试这...
急用啊!!.我有不是这个专业的.哎~~~~~~~~~~~~~
#include<iostream> #include<stdlib.h> using namespace std;
template<typename T>class SeqSpaMat; template<typename T>class Term{ int row; int col; T value; public: friend class SeqSpaMat<T>; }; template<typename T>class SeqSpaMat{ private: int rows; int cols; Term<T> *arr; int size; int MaxSize; public: SeqSpaMat(int maxTerm=100); ~SeqSpaMat(void){ delete []arr;} void Transpose(SeqSpaMat<T> &a); friend ostream& operator<<(ostream& out,SeqSpaMat<T>& a); friend istream &operator>>(istream &in,const SeqSpaMat<T> &a); }; template<typename T>SeqSpaMat<T>::SeqSpaMat(int maxTerm){ if(maxTerm<1) { cout<<"初始化值错!"<<endl; exit(1); } MaxSize=maxTerm; arr=new Term<T>[MaxSize]; size=rows=cols=0; } template<typename T>void SeqSpaMat<T>::Transpose(SeqSpaMat<T> &a){ int j; if(size>a.MaxSize){ cout<<"空间不够无法转置!"<<endl; exit(1); } a.cols=rows; a.rows=cols; a.size=size;
int *colSize,*rowNext; colSize=new int[cols]; rowNext=new int[rows];
for(int i=0;i<cols;i++) colSize[i]=0; for(i=0;i<size;i++) colSize[arr[i].col]++; rowNext[0]=0; for(i=1;i<cols;i++) rowNext[i]=rowNext[i-1]+colSize[i-1]; for(i=0;i<size;i++) { j=rowNext[arr[i].col]++; a.arr[j].row=arr[i].col; a.arr[j].col=arr[i].row; a.arr[j].value=arr[i].value; } delete []colSize; delete []rowNext; } template<typename T>ostream& operator<<(ostream& out,SeqSpaMat<T>& a){ out<<"矩阵行数为:"<<a.rows<<endl; out<<"矩阵列数为:"<<a.cols<<endl; out<<"矩阵非零元个数为:"<<a.size<<endl; out<<"矩阵非零元三元组为:"<<a.rows<<endl;
for(int i=0;i<a.size;i++) out<<"a("<<a.arr[i].row<<','<<a.arr[i].col<<")="<<a.arr[i].value<<endl; return out; } template<typename T>istream &operator>>(istream &in,const SeqSpaMat<T> &a){ cout<<"输入行数、列数、和非零元个数:"; in>>a.rows>>a.cols>>a.size;
for(int i=0;i<a.size;i++){ cout<<"输入行号、列号、和元素值:"; in>>a.arr[i].row>>a.arr[i].col>>a.arr[i].value; } return in; } void main(){ SeqSpaMat<int> A; SeqSpaMat<int> B; cin>>A; cout<<"原矩阵为:"<<endl; cout<<A; A.Transpose(B); cout<<"转置后的矩阵为:"<<endl; cout<<B; } 希望不会太迟了