[c++]代码库
#include<iostream.h>
const int smax=50;
typedef int elemtype;
struct triple
{
int i,j;
elemtype v;
};
struct smatrix
{
int m,n,t;
triple sm[smax+1];
};
void initmatrix ( smatrix &M )
{
M.m=M.n=0;
M.t=0;
}
void inputmatrix ( smatrix &M,int m, int n )
{
M.m=m;
M.n=n;
int row,col;
elemtype val;
int k=0;
cin>>row>>col>>val;
while ( row!=0 )
{
k++;
M.sm[k].i=row;
M.sm[k].j=col;
M.sm[k].v=val;
cin>>row>>col>>val;
}
M.t=k;
}
void outputmatrix ( smatrix &M )
{
cout<<'(';
for ( int i=1; i<M.t; i++ )
{
cout<<'('<<M.sm[i].i<<',';
cout<<M.sm[i].j<<',';
cout<<M.sm[i].v<<')'<<',';
}
if ( M.t!=0 )
{
cout<<'('<<M.sm[M.t].i<<',';
cout<<M.sm[M.t].j<<',';
cout<<M.sm[M.t].v<<')';
}
cout<<')'<<endl;
}
smatrix transpose ( smatrix &M )
{
smatrix s;
initmatrix ( s );
int m,n,t;
m=M.m;
n=M.n;
t=M.t;
s.m=n;
s.n=m;
s.t=t;
if ( t==0 )
return s;
int num[smax+1];
int cpot[smax+1];
for ( int col=1; col<=n; col++ )
num[col]=0;
for ( int k=1; k<=t; k++ )
++num[M.sm[k].j];
cpot[1]=1;
for ( col=2; col<=n; col++ )
cpot[col]=cpot[col-1]+num[col-1];
for ( int p=1; p<=t; p++ )
{
col=M.sm[p].j;
int q=cpot[col];
s.sm[q].i=M.sm[p].j;
s.sm[q].j=M.sm[p].i;
s.sm[q].v=M.sm[p].v;
++cpot[col];
}
return s;
}
void main()
{
smatrix M;
int m,n;
initmatrix ( M );
cout<<"input M,N:"<<endl;
cin>>m>>n;
inputmatrix ( M,m,n );
cout<<"m="<<M.m<<',';
cout<<"n="<<M.n<<',';
cout<<"t="<<M.t<<endl;
outputmatrix ( M );
cout<<"m="<<M.m<<',';
cout<<"n="<<M.n<<',';
cout<<"t="<<M.t<<endl;
outputmatrix ( transpose ( M ) );
}
by: 发表于:2018-01-25 09:40:35 顶(0) | 踩(0) 回复
??
回复评论