程序结果三元组不按行序递增输出?如何修改???
/*参考输入数:
0 0 0 0
1 0 0 2
0 0 0 0
0 0 3 0
0 0 0 6*/
#include<iostream>
using namespace std;
#define ElemType int
#define Max 100
const int M=5;
const int N=4;
typedef struct
{
int r;
int c;
ElemType d;
}TupNode;
typedef struct
{
int rows;
int cols;
int nums;
TupNode data[Max];
}TsMat;
void create(TsMat &t,ElemType A[M][N])
{
int i,j;
t.rows=M;t.cols=N;t.nums=0;
for(i=0;i<M;i++)
{ for(j=0;j<N;j++)
if(A[i][j]!=0)
{ t.data[t.nums].r=i;
t.data[t.nums].c=j;
t.data[t.nums].d=A[i][j];t.nums++;
}
}
}
int Value(TsMat &t,ElemType x,int rs,int cs)
{
int i,k=0,s=0;
if(rs>=t.rows|| cs>=t.cols)
return 0;
while (k<t.nums && rs>t.data[k].r) k++;
while (k<t.nums && cs>t.data[k].c) k++;//主要由于此处出现了一点不合理!如何修改??
if(t.data[k].r==rs && t.data[k].c==cs)
t.data[k].d=x;
else
{ for(i=t.nums-1;i>=k;i--)
{
t.data[i+1].r=t.data[i].r;
t.data[i+1].c=t.data[i].c;
t.data[i+1].d=t.data[i].d;
}
t.data[k].r=rs;
t.data[k].c=cs;
t.data[k].d=x;
t.nums++;
}
return 1;
}
int Assign(TsMat t ,ElemType&x,int rs,int cs)
{ int k=0;
if(rs>=t.rows|| cs>=t.cols)
return 0;
while (k<t.nums && rs>t.data[k].r) k++;
while (k<t.nums && cs>t.data[k].c) k++;
if(t.data[k].r==rs && t.data[k].c==cs)
{t.data[k].d=x;return 1;}
else
return 0;
}
void Display(TsMat t)
{
int i;
if (t.nums<=0)return ;
printf("\t%d\t%d\t%d\n",t.rows,t.cols,t.nums);
printf("-------------------------------------\n");
for( i=0;i<t.nums;i++)
printf("\t%d\t%d\t%d\n",t.data[i].r,t.data[i].c,t.data[i].d);
}
void main()
{ElemType x=5,y;
ElemType A[M][N];
for(int i=0;i<M;i++)
for(int j=0;j<N;j++)
cin>>A[i][j];
TsMat t;
create(t,A);
Display(t);
cout<<endl<<endl;
Value(t,x,2,3);
Display(t);
cout<<endl<<endl;
cout<<Assign(t,y,1,2)<<endl;
Display(t);
}