矩阵数据结构的实现

稀疏矩阵的压缩存储:
实现稀疏矩阵压缩存储,并实现矩阵转置和求和。
输入矩阵时,首先需要输入非零元素的个数,然后分别输入矩阵的 行号,列号和值。
输完2个矩阵后,自动进行计算第一个矩阵的转置以及两个矩阵的和。
例如:输入如下:
100 90 5 //矩阵的行数为100,列数为90,共5个非零元素。
1 10 100 //a(1,10)=100
50 60 200//a(50,60)=200
50 80 100//a(50,80)=100
60 60 200//a(60,60)=200
99 89 10//a(99,89)=10
100 90 2 //矩阵b的行数为100,列数为90,共2个非零元素。
1 1 10 //b(1,1)=10
50 60 -200//b(50,60)=-200

#include 
//#include "matrix.h"
#include 
using namespace std;
class matrix;
class tuple1{
    friend class matrix;
public:
    int row;
    int col;
    int value;
public:
    tuple1(int row1=0,int col1=0,int value1=0):row(row1),col(col1),value(value1){}
    //tuple(const tuple & tem);
//    tuple &operator=(tuple & tem);
    //friend istream &operator>>(istream &in,matrix &temp);
   // friend ostream &operator<<(ostream &out,matrix &temp);
};

//tuple::tuple(const tuple &tem) {
//    row=tem.row;
//    col=tem.col;
//    value=tem.value;
//}
//
//tuple& tuple::operator=(tuple &tem) {
//    row=tem.row;
//    col=tem.col;
//    value=tem.value;
//    return *this;
//}

class matrix{
public:
    int rows,cols,terms;
    int maxsize;
    tuple1 *p;
public:
    matrix(int maxSize1=100);
    ~matrix(){delete []p;}
    matrix(const matrix &tem);
    matrix &operator=(matrix &tem);
    matrix transpose1(matrix tem);
    matrix transpose2(matrix tem);
    void add(matrix tem);
    friend istream &operator>>(istream &in,matrix &temp);
    friend ostream &operator<<(ostream &out,matrix &temp);
};

matrix::matrix(int maxSize1)  {
    maxsize=maxSize1;
    p=new tuple1[maxsize];
    cols=rows=terms=0;
}

matrix::matrix(const  matrix &tem) {
    maxsize=tem.maxsize;
    cols=tem.cols;
    rows=tem.rows;
    terms=tem.terms;
    p=new tuple1[maxsize];
    for(int i=0;i
       p[i].col=tem.p[i].col;
       p[i].row=tem.p[i].row;
       p[i].value=tem.p[i].value;
    }
}

matrix & matrix::operator=(matrix &tem)  {
    maxsize=tem.maxsize;
    cols=tem.cols;
    rows=tem.rows;
    terms=tem.terms;
    p=new tuple1[maxsize];
    for(int i=0;i
       p[i].col=tem.p[i].col;
       p[i].row=tem.p[i].row;
       p[i].value=tem.p[i].value;
    }
    return *this;
}

matrix  matrix::transpose1(matrix tem)  {     //
    matrix q(maxsize);
    q.cols=tem.rows;
    q.rows=tem.cols;
    q.terms=tem.terms;
    int current=0;
    for(int i=0;i
        for(int j=0;j
            if(tem.p[j].col==i) {
               q.p[current].col=tem.p[j].row;
               q.p[current].row=tem.p[j].col;
               q.p[current].value=tem.p[j].value;
               current++;
            }
        }
    }
    return q;
}

matrix matrix::transpose2(matrix tem) {
   int *rowsize=new int[cols];
   int *rowstart=new int[cols];
   matrix q(maxsize);
   for(int i=0;i
   for(int i=0;i
   rowstart[0]=0;
   for(int i=1;i
   for(int i=0;i
       int j=rowstart[tem.p[i].col];
       q.p[j].row=tem.p[i].col;
       q.p[j].col=tem.p[i].row;
       q.p[j].value=tem.p[i].value;
       rowstart[tem.p[i].col]++;
   }
   q.terms=tem.terms;
   q.cols=tem.rows;
   q.rows=tem.cols;
   delete []rowsize;
   delete []rowstart;
   return q;
}

istream &operator>>(istream &in,matrix &temp) {
   in>>temp.rows>>temp.cols>>temp.terms;
   for(int i=0;i
    in>>temp.p[i].row>>temp.p[i].col>>temp.p[i].value;
   }
   return in;
}

ostream &operator<<(ostream &out,matrix &temp) {
   int i;
   for(i=0;i
    out<.p[i].row<<" "<.p[i].col<<" "<.p[i].value<;
   }
   return out;
}

void matrix::add(matrix tem){
    matrix C(maxsize);
    int k=0,i=0,j=0;
    int loc1=0,loc2=0;
    int flag=1;
    while(i.terms)  {
        loc1=p[i].row*cols+p[i].col;
        loc2=tem.p[j].row*cols+tem.p[j].col;
        if(loc1.p[k].value=p[i].value;
                C.p[k].row=p[i].row;
                C.p[k].col=p[i].col;
            i++;
        }
        else if(loc1>loc2) {
                   C.p[k].value=tem.p[j].value;
                   C.p[k].row=tem.p[j].row;
                   C.p[k].col=tem.p[j].col;
                   j++;
        }
        else
        {
              flag=p[i].value+tem.p[j].value;
              if(flag==0){  i++;j++;
                continue;}
              else{
                C.p[k].value=p[i].value+tem.p[j].value;
                C.p[k].row=p[i].row;
                C.p[k].col=p[i].col;
                i++;
                j++;
             }
        }
        k++;
    }
    for(;i
       C.p[k].value=p[i].value;
                C.p[k].row=p[i].row;
                C.p[k].col=p[i].col;
        k++;
    }
    for(;j
        C.p[k].value=tem.p[j].value;
        C.p[k].row=tem.p[j].row;
        C.p[k].col=tem.p[j].col;
        k++;
    }
    C.terms=k;
    cout<;
}

int main()
{
    freopen("yang","r",stdin);
    matrix A,B;
    cin>>A;
    cin>>B;
    matrix C=A.transpose2(A);
    cout<<"The transformed matrix is:"<;
    cout<;
    cout<<"The added matrix is:"<;
    A.add(B);
    return 0;
}

你可能感兴趣的:(数据结构,链表,矩阵)