sputnik吧 关注:25贴子:1,891
  • 3回复贴,共1

对了,发点东西。

只看楼主收藏回复

百度。
c++数据结构的破烂玩意儿,给吧里增加的活力。
高手勿喷,我菜比我自重。


1楼2011-06-19 20:57回复
    期末大作业,优化的稀疏矩阵运算。
    #include <iostream>#include <iomanip>using namespace std;
    template <class T> //定义结点类型 struct B{ int i,j; //非零元素的行、列 T v; //非零元素值 };
    template <class T>class X_Array{ private: int mm,nn,tt; //稀疏矩阵的行列数和非零元素个数 B<T> *bb; //三列二维数组空间 int *pos; //某行第一个非零元素在b的下标 int *num; //某行非零元素的个数 public: void in_X_Array(); //三元组形式输入稀疏矩阵非零元素 void cp_X_Array(int,int,int,B<T>[]); //复制三元组数组 void th_X_Array(int,int,T[]); //由一般稀疏矩阵转换 void prt_X_Array(); //按行输出 X_Array tran_X_Array(); //转置 X_Array operator+(X_Array &); //相加(相减就不再赘述) X_Array operator*(X_Array &); //相乘 };
    template <class T>void X_Array<T>::in_X_Array() //三元组形式输入稀疏矩阵非零元素{ int k,m,n; cout<<"输入行数 列数 非零元素个数:"; cin>>mm>>nn>>tt; bb=new B<T>[tt]; //三列二维数组空间 cout<<"输入行号 列号 非零元素值:" <<endl; for (k=0;k<tt;k++) { cin>>m>>n>>bb[k].v; bb[k].i=m-1;bb[k].j=n-1; } pos=new int[mm]; num=new int[mm]; for (k=0;k<mm;k++) num[k]=0; for (k=0;k<tt;k++) { num[bb[k].i]=num[bb[k].i]+1; } pos[0]=0; for (k=1;k<mm;k++) { pos[k]=pos[k-1]+num[k-1]; } return;}
    template <class T>void X_Array<T>::cp_X_Array(int m,int n, int t, B<T>b[]) //复制三元组数组{ int k; mm=m;nn=n;tt=t; bb=new B<T>[tt]; for (k=0;k<t;k++) //复制三列二维数组 { bb[k].i=b[k].i-1; bb[k].j=b[k].j-1; bb[k].v=b[k].v; } pos=new int[mm]; num=new int[mm]; for (k=0;k<mm;k++) num[k]=0; for (k=0;k<tt;k++) {num[bb[k].i]=num[bb[k].i]+1;} pos[0]=0; for (k=1;k<mm;k++) { pos[k]=pos[k-1]+num[k-1]; } return;}
    template <class T>void X_Array<T>::th_X_Array(int m, int n, T a[]) //由一般稀疏矩阵转换{ int k,t=0,p,q; T d; for (k=0;k<m*n;k++) //非零元素个数 { if (a[k]!=0) t=t+1; } mm=m;nn=n;tt=t; bb=new B<T>[tt]; k=0; for (p=0;p<m;p++) { for(q=0;q<n;q++) { d=a<p*n+q>;</p*n+q> if (d!=0) { bb[k].i=p;bb[k].j=q;bb[k].v=d; k=k+1; } } } pos=new int[mm]; num=new int[mm]; for (k=0;k<mm;k++) num[k]=0; for (k=0;k<tt;k++) {num[bb[k].i]=num[bb[k].i]+1;} pos[0]=0; for (k=1;k<mm;k++) { pos[k]=pos[k-1]+num[k-1]; } return;}
    template <class T>void X_Array<T>::prt_X_Array() //稀疏矩阵输出{ int k,kk,p; for (k=0;k<mm;k++) { p=pos[k]; for(kk=0;kk<nn;kk++) if((bb.i==k)&&(bb.j==kk)) { cout<<setw(8)<<bb.v; p=p+1; } else cout<<setw(8)<<0; cout<<endl; } return;}
    template <class T>X_Array<T> X_Array<T>::tran_X_Array() //稀疏矩阵转置{ X_Array<T> at; int k,p,q; at.mm=nn; at.nn=mm; at.tt=tt; at.bb=new B<T>[tt]; k=0; for (p=0;p<nn;p++) { for (q=0;q<tt;q++) { if (bb[q].j==p) //将非零元素信息一次存放到转置矩阵的三列二维数组中 { at.bb[k].i=bb[q].j; at.bb[k].j=bb[q].i; at.bb[k].v=bb[q].v; k=k+1; } } } at.pos=new int[at.mm]; at.num=new int[at.mm]; for (k=0;k<at.mm;k++) at.num[k]=0; for (k=0;k<at.tt;k++) {at.num[at.bb[k].i]=at.num[at.bb[k].i]+1;} at.pos[0]=0; for (k=1;k<at.mm;k++) { at.pos[k]=at.pos[k-1]+at.num[k-1]; } return(at);}
    


    4楼2011-06-19 21:00
    回复
      我去,这格式……
      其他几个不发了。。。
      悲剧。
      诶。


      6楼2011-06-19 21:00
      回复
        话说我们计算机会考的时候编程做网页的一个插图就是C++......


        IP属地:美国7楼2011-06-25 12:16
        回复