int MUL(RLSMatrix *A,RLSMatrix *B,RLSMatrix *T) /*乘法*/
{
int arow,ccol,tp,p,q,blow,t,i;
int ctemp[MAXRC];
if(A->nu!=B->mu) return 0;
T->mu=A->mu;T->nu=B->nu;T->tu=0;
if(A->tu*B->tu!=0)
{
for(arow=1;arow<=A->mu;++arow)
{
for(i=1;i<=T->nu;i++)
ctemp[i]=0;
T->rpos[arow]=T->tu+1;
if(arow<A->mu) tp=A->rpos[arow+1];
else{tp=A->tu+1;}
for(p=A->rpos[arow];p<tp;++p)
{
blow=A->data[p].j;
if(blow<B->mu)
t=B->rpos[blow+1];
else
t=B->tu+1;
for(q=B->rpos[blow];q<t;++q)
{
ccol=B->data[q].j;
ctemp[ccol] += A->data[p].e*B->data[q].e;
} /*for q*/
} /*求得Q中的crow( arow)行的非零元*/
for(ccol=1;ccol<T->nu;++ccol)
{
if(ctemp[ccol])
{
if(++T->tu>MAXSIZE) return 0;
T->data[T->tu].i=arow;
T->data[T->tu].j=ccol;
T->data[T->tu].e=ctemp[ccol];
}
}
}
Print(A,B,T);
return 1;
}
}
void Printt(RLSMatrix *A) /*打印矩阵*/
{
int row,col,i=1,k=0;
for(row=1;row<=A->mu;row++)
{
for(col=1;col<=A->nu;col++)
{
if(A->data[i].i==row&&A->data[i].j==col&&i<=A->tu)
printf("%-4d",A->data[i++].e);
else printf("%-4d",k);
}
printf("\n");
}
}
void Print(RLSMatrix *A,RLSMatrix *B,RLSMatrix *T) /*打印函数*/
{
clrscr();
if(A->mu!=0) {printf("Print Matrix A:\n");Printt(A); printf("\n");}
else printf("Matrix A is blank!\n");
if(B->tu!=0) {printf("Print Matrix B:\n");Printt(B); printf("\n");}
else printf("Matrix B is blank!\n");
if(T->tu!=0) {printf("Print Matrix T:\n");Printt(T); printf("\n");}
else {printf("Matrix T is blank!\n");getch();}
getch();
}
void Enter(RLSMatrix *A,RLSMatrix *B,RLSMatrix *T) /*输入函数*/
{
int i,k,num[MAXRC];
char cmd;
clrscr();
printf("Enter the mu and nu and tu of Matrix A(mu,nu,tu): \n"); /*输入矩阵A*/
scanf("%d,%d,%d",&A->mu,&A->nu,&A->tu);
while(A->mu<=0||A->mu>MAXR||A->nu<=0||A->nu>MAXC)
{
printf("Enter the mu and nu and tu of Matrix A(mu,nu,tu): \n");
scanf("%d,%d,%d",&A->mu,&A->nu,&A->tu);
}
printf("Enter the Matrix A(i-j:e):\n");
for(i=1;i<=A->tu;i++)
scanf("%d-%d:%d",&A->data[i].i,&A->data[i].j,&A->data[i].e);
for(i=1;i<=A->mu;i++)
num[i]=0;
for(i=1;i<=A->tu;i++)
++num[A->data[i].i];
A->rpos[1]=1;
for(i=2;i<=A->mu;i++)
A->rpos[i]=A->rpos[i-1]+num[i-1];
printf("Enter the mu and nu and tu of Matrix B(mu,nu,tu): \n"); /*输入矩阵B*/
scanf("%d,%d,%d",&B->mu,&B->nu,&B->tu);
while(A->mu<=0||A->mu>MAXR||A->nu<=0||A->nu>MAXC)
{
printf("Enter the mu and nu and tu of Matrix B(mu,nu,tu): \n");
scanf("%d,%d,%d",&B->mu,&B->nu,&B->tu);
}
printf("Enter the Matrix B(i-j:e):\n");
for(i=1;i<=B->tu;i++)
scanf("%d-%d:%d",&B->data[i].i,&B->data[i].j,&B->data[i].e);
for(i=1;i<=B->mu;i++)
num[i]=0;
for(i=1;i<=B->tu;i++)
++num[B->data[i].i];
B->rpos[1]=1;
for(i=2;i<=B->mu;i++)
B->rpos[i]=B->rpos[i-1]+num[i-1];
do{
printf("Enter the operation you want:"); /*选择需要的运算*/
cmd=getche();
}while(cmd!='+'&&cmd!='-'&&cmd!='*');
getch();
switch(cmd)
{
case '+' : if(A->mu==B->mu&&A->nu==B->nu) ADD(A,B,T); break;
case '-' : if(A->mu==B->mu&&A->nu==B->nu) SUB(A,B,T); break;
case '*' : if(A->nu==B->mu) MUL(A,B,T); break;
default : printf("No thia operation!"); break;
}
}
void InitializationMatrix(RLSMatrix *A,RLSMatrix *B,RLSMatrix *T) /*初始化矩阵*/
{
A->mu=A->nu=A->tu=0;
B->mu=B->nu=B->tu=0;
T->mu=T->nu=T->tu=0;
}
/*************************************主函数************************************/
void InitializationMatrix(RLSMatrix *A,RLSMatrix *B,RLSMatrix *T);
void Enter(RLSMatrix *A,RLSMatrix *B,RLSMatrix *T);
void Print(RLSMatrix *A,RLSMatrix *B,RLSMatrix *T);
void ADD(RLSMatrix *A,RLSMatrix *B,RLSMatrix *T);
void SUB(RLSMatrix *A,RLSMatrix *B,RLSMatrix *T);
int MUL(RLSMatrix *A,RLSMatrix *B,RLSMatrix *T);
void Initialization();
void Myinformation();
void Exit();
void main()
{
int i,k,num[MAXRC];
char cmd;
RLSMatrix *A,*B,*T;
A=( RLSMatrix *)malloc(sizeof( RLSMatrix)); /*分配空间*/
B=( RLSMatrix *)malloc(sizeof( RLSMatrix));
T=( RLSMatrix *)malloc(sizeof( RLSMatrix));
Myinformation();
InitializationMatrix(A,B,T);
while(1)
{
Initialization();
do{
gotoxy(10,10);
printf("Enter E/e ,Q/q ,P/p to continue:");
cmd=getche();
printf("\n");
}while(cmd!='q'&&cmd!='Q'&&cmd!='e'&&cmd!='E'&&cmd!='p'&&cmd!='P');
switch(cmd)
{
case 'q' : Exit(); break;
case 'Q' : Exit(); break;
case 'e' : Enter(A,B,T); break;
case 'E' : Enter(A,B,T); break;
case 'p' : Print(A,B,T); break;
case 'P' : Print(A,B,T); break;
}
}
}
void Initialization() /*初始化函数*/
{
clrscr();
printf("*********************************************************************************");
gotoxy(7,2);
printf("Enter the RLSMatrix--E");
gotoxy(38,2);
printf("Print--P");
gotoxy(55,2);
printf("Quit--Q");
gotoxy(80,2);
printf("*");
printf("********************************************************************************");
gotoxy(1,22);
printf("*********************************************************************************");
gotoxy(10,23);
printf("Enter a operation code: E/e ,Q/q ,P/p to continue: ");
gotoxy(80,23);
printf("*");
printf("********************************************************************************");
}
void Myinformation() /*我的信息函数*/
{
int i;
clrscr();
for(i=1;i<=240;i++)
printf("\1");
gotoxy(15,8);
printf("This is a Matrix arithmetic calculator!");
gotoxy(15,12);
printf("Name: liyongjun.");
gotoxy(15,13);
printf("Num: 3104006893.");
gotoxy(15,14);
printf("Grade: 2004.");
gotoxy(15,15);
printf("Class: cumputer science and technology 10");
gotoxy(1,22);
for(i=1;i<=240;i++)
printf("\1");
gotoxy(1,18);
printf("Print any key to continue...");
getch();
}
void Exit() /*退出函数*/
{
char cmd;
do{
gotoxy(10,11);
printf("Do you want to out?Y/N:");
cmd=getche();
}while(cmd!='y'&&cmd!='Y'&&cmd!='n'&&cmd!='N');
if(cmd=='y'||cmd=='Y')
exit(0);
}
上一页 [1] [2]
稀疏矩阵运算器-数据结构课程设计 第2页下载如图片无法显示或论文不完整,请联系qq752018766