#include<iostream.h>
#include<stdlib.h>
#include<iomanip.h>
void caidan();
#define max 12500
typedef struct{
int e;
int i; //行下标
int j; //列下标
}triple;
typedef struct{
triple data[max+1];
int rpos[10];
int m,n,t;
}ts;
//矩阵的转置实现函数
void fasttransposesmatrix(ts M,ts &T)
{
int col,num[10],cpot[10]={0},p,q;
T.m=M.n;
T.n=M.m;
T.t=M.t;
if(T.t){
for(col=1;col<=M.n;++col)
num[col]=0;
for(int t1=1;t1<=M.t;++t1)
++num[M.data[t1].j];
cpot[1]=1;
for(col=2;col<=M.n;++col)
cpot[col]=cpot[col-1]+num[col-1];
for(p=1;p<=M.t;++p){
col=M.data[p].j;
q=cpot[col];
T.data[q].i=M.data[p].j;
T.data[q].j=M.data[p].i;
T.data[q].e=M.data[p].e;
++cpot[col];
}
}
}
//矩阵的乘法Y=M*Q实现函数
void multsmatrix(ts M,ts Q,ts &Y){
int arow,brow=0,p,q,i1,tp,t1,ctemp[10],ccol;
if(M.n!=Q.m)
cout<<"错误!"<<endl;
Y.m=M.m;
Y.n=Q.n;
Y.t=0;
if(M.t*Q.t!=0)
{
for(arow=1;arow<=M.m;++arow)
{
for(i1=1;i1<=M.m;i1++)
ctemp[i1]=0;
Y.rpos[arow]=Y.t+1;
if(arow<M.m)
tp=M.rpos[arow+1];
else
tp=M.t+1;
for(p=M.rpos[arow];p<tp;++p)
{
brow=M.data[p].j;
if(brow<Q.m)
t1=Q.rpos[brow+1];
else
t1=Q.t+1;
for(q=Q.rpos[brow];q<t1;++q)
{
ccol=Q.data[q].j;
ctemp[ccol]+=M.data[p].e*Q.data[q].e;
}
}
for(ccol=1;ccol<=Y.n;++ccol)
if(ctemp[ccol]){
++Y.t;
if(++Y.t>max)
cout<<"错误"<<endl;
Y.data[Y.t].i=arow;
Y.data[Y.t].j=ccol;
Y.data[Y.t].e=ctemp[ccol];
cout<<Y.data[Y.t].i<<Y.data[Y.t].j<<Y.data[Y.t].e<<endl;
}
}
}
}
//将数组转换成三元组
void exchange1(int **a,ts &M,int m,int n)
{
int i1,j1,z=1,pos[10];
for(i1=0;i1<=n;i1++)
{
pos[i1]=0;
M.rpos[i1]=0;
}
pos[0]=1;
for(i1=0;i1<m;i1++)
for(j1=0;j1<n;j1++)
if(a[i1][j1])
{
M.data[z].i=i1+1;
M.data[z].j=j1+1;
M.data[z].e=a[i1][j1];
z++;
pos[i1+1]++;
}
for(i1=0;i1<=m;i1++)
{
M.rpos[i1+1]=M.rpos[i1]+pos[i1];
}
M.t=z-1;
M.m=m;
M.n=n;
}
//将三元组转换成数组形式
void exchange2(int **a,ts &T)
{
int i1,j1,z1;
for(i1=0;i1<T.m;i1++)
for(j1=0;j1<T.n;j1++)
{
a[i1][j1]=0;
}
for(z1=1;z1<=T.t;z1++)
for(i1=0;i1<T.m;i1++)
for(j1=0;j1<T.n;j1++)
if((i1==T.data[z1].i-1)&&(j1==T.data[z1].j-1))
{
a[i1][j1]=T.data[z1].e;
}
}
//输出结果
void output(int **a,int m,int n)
{
int i1,j1;
for(i1=0;i1<m;i1++)
{
for(j1=0;j1<n;j1++)
{
cout<<a[i1][j1]<<',';
}cout<<endl;
}
}
//转置的操作函数
void zhuanzhi(){
int i1,j1,m,n;
cout<<"初始矩阵:"<<endl;
cout<<"请输入你要输入的数组的行数:"<<endl;
cin>>m;
cout<<"请输入你要输入的数组的列数:"<<endl;
cin>>n;
int**pp=new int*[m];
for(i1=0;i1<m;i1++)
pp[i1]=new int[n];
cout<<"请依次输入矩阵的元素:"<<endl;
for(i1=0;i1<m;i1++)
for(j1=0;j1<n;j1++)
{
cin>>pp[i1][j1];
}
cout<<endl;
ts M,T;
exchange1(pp,M,m,n);
fasttransposesmatrix(M,T);
int**pp1=new int*[T.m];
for(i1=0;i1<T.m;i1++)
pp1[i1]=new int[T.n];
exchange2(pp1,T);
cout<<"转置后的矩阵是:"<<endl;
output(pp1,T.m,T.n);
delete pp;
delete pp1;
caidan();
}
//相乘的操作函数
void xiangcheng(){
ts M,Q,Y;
int i1,j1,m,n;
cout<<"请输入你要输入的数组的行数:"<<endl;
cin>>m;
cout<<"请输入你要输入的数组的列数:"<<endl;
cin>>n;
int**p=new int*[m];
for(i1=0;i1<m;i1++)
p[i1]=new int[n];
cout<<"请依次输入矩阵的元素:"<<endl;
for(i1=0;i1<m;i1++)
for(j1=0;j1<n;j1++)
{
cin>>p[i1][j1];
}
cout<<endl;
exchange1(p,M,m,n);
cout<<"请再次输入一个矩阵(即乘数):"<<endl;
cout<<"请输入你要输入的数组的行数:"<<endl;
cin>>m;
cout<<"请输入你要输入的数组的列数:"<<endl;
cin>>n;
int**p2=new int*[m];
for(i1=0;i1<m;i1++)
p2[i1]=new int[n];
cout<<"请依次输入矩阵的元素:"<<endl;
for(i1=0;i1<m;i1++)
for(j1=0;j1<n;j1++)
{
cin>>p2[i1][j1];
}
cout<<endl;
exchange1(p2,Q,m,n);
multsmatrix(M,Q,Y);
int**p1=new int*[Y.m];
for(i1=0;i1<Y.m;i1++)
p1[i1]=new int[Y.n];
exchange2(p1,Y);
cout<<"两矩阵相乘结果为:"<<endl;
output(p1,Y.m,Y.n);
delete p2;
delete p1;
caidan();
}
//菜单
void caidan()
{
int i;
cout<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl;
cout<<" 1.矩阵的转置 2.两个矩阵的乘法 3.退出 "<<endl;
cout<<"请选择:"<<endl;
cin>>i;
switch(i){
case 1:zhuanzhi();
break;
case 2:xiangcheng();
break;
case 3: exit(0);
}
}
//主函数
void main()
{
caidan();
}
温馨提示:内容为网友见解,仅供参考