有一个大的矩阵,大致可以分为3到4块(指的是对角线),每块的维数是不确定的,所以大矩阵的维数也是不确定的!!在程序执行的过程才可以知道每一块的维数,最终要把大的矩阵搭建起来!!不知道我说的意思大家明不明白!!请问大家有什么好的方法!!谢谢!!
#include<iostream.h>
double *EMGD_H;
const int GS=10;
class generator
{
public:
//generator();
void Setcanshu(double Td011,double Tq011,double Td022,double Tq022,double D1,double M1,int Mnumber1);
void GetEMGD_H1();
void GetEMGD_H2();
void GetEMGD_H3();
void GetEMGD_H4();
int Getnum(){return Mnumber;}
//~generator();
private:
double Td01,Tq01,Td02,Tq02;
double D,M;
int Mnumber;
};
void generator::Setcanshu(double Td011,double Tq011,double Td022,double Tq022,double D1,double M1,int Mnumber1)
{
Td01=Td011;
Tq01=Tq011;
Td02=Td022;
Tq02=Tq022;
D=D1;
M=M1;
Mnumber=Mnumber1;
}
void generator::GetEMGD_H1()
{
EMGD_H=new double[Mnumber*Mnumber];
EMGD_H[0]=-1/Td01;
}
void generator::GetEMGD_H2()
{
EMGD_H=new double[Mnumber*Mnumber];
EMGD_H[0]=-1/Td01;
EMGD_H[1]=0;
EMGD_H[2]=0;
EMGD_H[3]=-1/Tq01;
}
void generator::GetEMGD_H3()
{
EMGD_H=new double[Mnumber*Mnumber];
EMGD_H[0]=-1/Td01;
EMGD_H[1]=0;
EMGD_H[2]=0;
EMGD_H[3]=1/Td02-1/Td01;
EMGD_H[4]=-1/Td02;
EMGD_H[5]=0;
EMGD_H[6]=0;
EMGD_H[7]=0;
EMGD_H[8]=-1/Tq01;
}
void generator::GetEMGD_H4()
{
EMGD_H=new double[Mnumber*Mnumber];
EMGD_H[0]=-1/Td01;
EMGD_H[1]=0;
EMGD_H[2]=0;
EMGD_H[3]=0;
EMGD_H[4]=1/Td02-1/Td01;
EMGD_H[5]=-1/Td02;
EMGD_H[6]=0;
EMGD_H[7]=0;
EMGD_H[8]=0;
EMGD_H[9]=0;
EMGD_H[10]=-1/Tq01;
EMGD_H[11]=0;
EMGD_H[12]=0;
EMGD_H[13]=0;
EMGD_H[14]=1/Tq02-1/Tq01;
EMGD_H[15]=-1/Tq02;
}
/*generator::~generator()
{
delete[] EMGD_H;
}*/
void main()
{
int i,j,k,l;
int weishu;
generator GSzwj[GS];
for(i=0;i<GS;i++)
GSzwj[i].Setcanshu(1,1,1,1,1,1,1);
for(i=0;i<GS;i++)
{
weishu=GSzwj[i].Getnum();
switch(weishu)
{
case 1:GSzwj[i].GetEMGD_H1();break;
case 2:GSzwj[i].GetEMGD_H2();break;
case 3:GSzwj[i].GetEMGD_H3();break;
case 4:GSzwj[i].GetEMGD_H4();break;
default:cout<<"没有相匹配的模型"<<endl;
}
for(j=0;j<weishu*weishu;j++)
cout<<EMGD_H[j]<<endl;
cout<<endl;
cout<<endl;
delete[] EMGD_H;
}
}
以下代码VC6.0测试通过
你怎么获得矩阵ABD的维数我不知道,就用常量代替了,你改成相应的变量就行
makematrix函数,需要几个矩阵的首地址和维数,然后申请空间,把各个矩阵的值复制到对应的位置
PS:matrix是矩阵的意思把?
#include <stdio.h>
#include <stdlib.h>#define AROW 2
#define ACOL 3
#define BROW 4
#define BCOL 2
int* makematrix(int* a,int arow,int acol,int *b,int brow,int bcol,int* d,int drow,int dcol)
{
int i,j,row=arow+brow,col=acol+bcol;
int* h=(int*)malloc(sizeof(int)*row*col);
for(i=0;i<arow;i++)
for(j=0;j<acol;j++)
*(h+i*col+j)=*(a+i*acol+j);
for(i=0;i<brow;i++)
for(j=0;j<bcol;j++)
*(h+(arow+i)*col+acol+j)=*(b+i*bcol+j);
for(i=0;i<drow;i++)
for(j=0;j<dcol;j++)
*(h+i*col+acol+j)=*(d+i*dcol+j);
for(i=0;i<brow;i++)
for(j=0;j<acol;j++)
*(h+(arow+i)*col+j)=0;
return(h);
}int main()
{
int a[AROW][ACOL],b[BROW][BCOL],d[AROW][BCOL],i,j;
int *h;
printf(\"Martrix A:\n\");
for(i=0;i<AROW;i++)
{
for(j=0;j<ACOL;j++)
{
a[i][j]=i*j+10;
printf(\"%d\t\",a[i][j]);
}
printf(\"\n\");
}
printf(\"\nMartrix D:\n\");
for(i=0;i<AROW;i++)
{for(j=0;j<BCOL;j++)
{
d[i][j]=i+j+20;
printf(\"%d\t\",d[i][j]);
}
printf(\"\n\");
}
printf(\"\nMartrix B:\n\");
for(i=0;i<BROW;i++)
{
for(j=0;j<BCOL;j++)
{
b[i][j]=i-j+30;
printf(\"%d\t\",b[i][j]);
}
printf(\"\n\");
}
printf(\"\n\");
h=makematrix(a[0],AROW,ACOL,b[0],BROW,BCOL,d[0],AROW,BCOL);
for(i=0;i<AROW+BROW;i++)
{
for(j=0;j<ACOL+BCOL;j++)
printf(\"%d\t\",*(h+i*(ACOL+BCOL)+j));
printf(\"\n\");
}
return 0;
}