#include<iostream.h>
#include<malloc.h>
#include<iomanip.h>
struct MultiMatrix{
int * p;
int N;
};
void input(MultiMatrix & T1,MultiMatrix & T2)
{
int k=T1.N*(T1.N+1)/2;
cout<<"输入两个对称矩阵的阶数:"<<endl;
cin>>T1.N;
T2.N=T1.N;
T1.p=(int *)malloc(k*sizeof(int));
T2.p=(int *)malloc(k*sizeof(int));
cout<<"分别输入两个矩阵的下三角形元素,共连续输入"<<k<<"个元素,每两个元素之间用空格分开。"<<endl;
cout<<" 对称矩阵1下三角元素:"<<endl;
for(int i=0;i<k;i++)
cin>>T1.p[i];
cout<<" 对称矩阵2下三角元素:"<<endl;
for(int j=0;j<k;j++)
cin>>T2.p[j];
cout<<endl;
}
int Elem(MultiMatrix T,int i,int j){
int k;
if(i>=j)
k=i*(i+1)/2+j;
else
k=j*(j+1)/2+i;
return T.p[k];
}
void printT(MultiMatrix T){
for(int i=0;i<T.N;i++){
for(int j=0;j<T.N;j++)
cout<<setw(6)<<setiosflags(ios::left)<<Elem(T,i,j)<<" ";
cout<<endl;
}
}
void main(){
MultiMatrix T1,T2;
cout<<" 对称矩阵相乘"<<endl;
cout<<"先输入两个N阶矩阵的阶数N并回车,然后输入矩阵一的下三角元素并回车,\n"
<<"然后输入矩阵二的下三角元素并回车。此时,屏幕上便显示结果。\n";
int x=0;
input(T1,T2);
cout<<"结果:"<<endl;
printT(T1);
cout<<"×\n";
printT(T2);
cout<<"=\n";
MultiMatrix W;
W.N=T1.N;
W.p=(int *)malloc(T1.N*T1.N*sizeof(int));
for(int i=0;i<T1.N;i++)
for(int j=0;j<T1.N;j++){
W.p[x]=0;
for(int n=0;n<T1.N;n++)
W.p[x]+=(Elem(T1,i,n)*Elem(T2,n,j));
x++;
}
for(int mc=0;mc<W.N*W.N;mc++){
if(mc!=0&&mc%W.N==0)
cout<<endl;
cout<<setw(6)<<setiosflags(ios::left)<<W.p[mc]<<" ";
}
cout<<"\nk,";
}