#include <iostream>
//#include "matrix.h"
#include <stdio.h>
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<terms;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<terms;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<cols;i++) {
for(int j=0;j<terms;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<cols;i++) rowsize[i]=0;
for(int i=0;i<terms;i++) rowsize[tem.p[i].col]++;
rowstart[0]=0;
for(int i=1;i<cols;i++) rowstart[i]=rowstart[i-1]+rowsize[i-1];
for(int i=0;i<terms;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<temp.terms;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<temp.terms;i++) {
out<<temp.p[i].row<<" "<<temp.p[i].col<<" "<<temp.p[i].value<<endl;
}
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&&j<tem.terms) {
loc1=p[i].row*cols+p[i].col;
loc2=tem.p[j].row*cols+tem.p[j].col;
if(loc1<loc2) {
C.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<terms;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<tem.terms;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<<C;
}
int main()
{
freopen("yang","r",stdin);
matrix A,B;
cin>>A;
cin>>B;
matrix C=A.transpose2(A);
cout<<"The transformed matrix is:"<<endl;
cout<<C;
cout<<"The added matrix is:"<<endl;
A.add(B);
return 0;
}
矩阵数据结构的实现
猜你喜欢
转载自blog.csdn.net/jthello123/article/details/80297988
今日推荐
周排行