在线求助·· 实在没办法了 小弟初学扎到不知道错在哪里
#include<stdio.h>#include<stdlib.h>
#include<conio.h>
#include<malloc.h>
#define MAXS 50
struct Triple{
int i,j;
int e;
};
struct TSM {
struct Triple data[MAXS];
int mu,nu,tu;
};//声明结构体。
void input(struct TSM *T) //对此三元组付值
{ int r=0;
printf("mn: ");scanf("%d",&T->mu);
printf("nu: ");scanf("%d",&T->nu);
printf("tu: ");scanf("%d",&T->tu);
printf("\n");
for(r=0;r<T->tu;r++)
{ printf("i: ");
scanf("%d",&T->data[r].i);
printf("j: ");
scanf("%d",&T->data[r].j);
printf("e: ");
scanf("%d",&T->data[r].e);
printf("\n");
}
}
void print(struct TSM *T)//输出此三元组的矩阵形势
{int i=0,j=0,r=0;
for(i=0;i<T->mu;i++)
{ printf("\n");
for(j=0;j<T->nu;j++)
{if(i==T->data[r].i&&j==T->data[r].j)
{printf(" %d ",T->data[r].e);r++;}
else printf(" 0 ");
}
printf("\n");
}
}
void add(struct TSM *A,struct TSM *B,struct TSM *C)//对两矩阵A和B进行相加运算,利用三个IF语句实现
{ int r=0,k=0,c=0,ai,aj,bi,bj;printf("c==%d",A->data[r].e);
if(A->mu!=B->mu||A->nu!=B->nu)
exit(0);
else
C->mu=A->mu;C->nu=A->nu;
while(r<A->tu&&k<B->tu)
{
ai=A->data[r].i;aj=A->data[r].j;
bi=B->data[k].i;bj=B->data[k].j;
if(ai<bi)
{
C->data[c].e=A->data[r].e;printf("c==%d,%d",A->data[r].e,c)
C->data[c].i=A->data[r].i;
C->data[c].j=A->data[r].j;
r++; printf("*<* r=%d ",r);printf("k=%d ",k);
c++;printf("c==%d,%d",A->data[r].e,c);
}
else if(ai==bi)
{
if(aj==bj)
{
C->data[c].e=A->data[r].e+B->data[k].e;
C->data[c].i=A->data[r].i;
C->data[c].j=A->data[r].j;
r++;
c++;
k++;printf("*==*r=%d ",r);printf("k=%d ",k);printf("c==%d,%d",C->data[c].e,c);
}
else if(aj<bj)
{
C->data[c].e=A->data[r].e;
C->data[c].i=A->data[r].i;
C->data[c].j=A->data[r].j;
r++;c++;printf("*=<*r=%d ",r);printf("k=%d ",k);
printf("c==%d,%d",C->data[c].e,c);
}
else if(aj>bj)
{
C->data[c].e=B->data[k].e;
C->data[c].i=B->data[k].i;
C->data[c].j=B->data[k].j;
k++;c++;printf("*=>*r=%d ",r);printf("k=%d ",k);
printf("c==%d,%d",C->data[c].e,c);
}
}
else if(ai>bi)
{
C->data[c].e=B->data[k].e;
C->data[c].i=B->data[k].i;
C->data[c].j=B->data[k].j;
k++;c++;printf("*>*r=%d ",r);printf("k=%d ",k);
printf("c==%d,%d",C->data[c].e,c);
}
}
while(k<B->tu)
{
C->data[c].e=B->data[k].e;
C->data[c].i=B->data[k].i;C->data[c].j=B->data[k].j;
k++;c++;printf("*b*r=%d ",r);printf("k=%d ",k);
printf("c==%d,%d",C->data[c].e,c);
}
while(r<A->tu)
{
C->data[c].e=A->data[r].e;
C->data[c].i=A->data[r].i;C->data[c].j=A->data[r].j;
r++;c++;printf("*a*r=%d ",r);printf("k=%d ",k);
printf("c==%d,%d",C->data[c].e,c);
}printf("*ziji*r=%d ",r);printf("k=%d ",k);
printf("c==%d,%d",C->data[c].e,c);
}
void main()
{struct TSM *A,*B,*C;int q=0;
clrscr();
printf(" =========the game begin==========\n");
memset(A,0,sizeof(struct TSM));
memset(B,0,sizeof(struct TSM));
memset(C,0,sizeof(struct TSM));
input(A);
printf("\n A= \n");
print(A);
[bo]for(;q<A->tu;q++)printf("%d%d",A->data[q].i,A->data[q].j);[/bo]
printf("\n");
input(B);
printf("\n B= \n");
print(B);
printf("\n");
add(A,B,C);
printf("\n so have A+B= \n");
print(C);
printf("\n =========the game over==========");
}
只要加上另颜色的段,程序就正确,没的话就···
现在问题是找到了 但却不知道这是为什么?
[[it] 本帖最后由 ll431 于 2008-4-25 23:29 编辑 [/it]]