# include & lt; stdio.h>
# include & lt; Stdlib. H>
# define MaxSize 100
# define ElementType int
# define the ERROR 0
//triple type definition
Typedef struct
{
Int the row, col.
ElementType e;
} Triple;
Typedef struct
{
Triple data [MaxSize + 1];//there is no data [0] unit
Int m, n, len;
} TSMatrix;
//create a matrix
Int CreateTSMatrix (TSMatrix * M)
{
Printf (" input a triple the number of rows and columns, the number of nonzero elements (separated with a space) : ");
Int cm, cn, cl;
The scanf (" % d % d % d ", & amp; Cm, & amp; Cn, & amp; Cl);
M - & gt; M=cm;
M - & gt; N=cn;
M - & gt; Len=cl;//the non-zero element number
int i=0;
Printf (" -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- \ n ");
Int cr, cc, ce;//temp
While (i{
Printf (" input the number of rows in the first % d a non-zero element, column number, value: ", I + 1);
The scanf (" % d % d % d ", & amp; Cr, & amp; Cc, & amp; Ce);
//if the input line number, column number beyond the above definition of
If (cr> Cm | | cc> Cn)
{
Printf (" this set of data is not legitimate, please input again! \n");
continue;
} the else
{
//because there is no data [0] unit - & gt; I + 1
M - & gt; Data [I + 1] row=cr;
M - & gt; Data [I + 1] col=cc;
M - & gt; Data [I + 1] e=ce;
i++;
}
}
Printf (" -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- \ n ");
return 1;
}
//print triple
Int PrintTSMatrix TSMatrix (M)
{
Int cm, cn, cl;
Cm=M.m.
Cn=M.n;
Cl=M.l en;
Int k=1;//counter
For (int I=1; i<=cm; I++)
{
for (int j=1; j<=cn; J++)
{
If (I==m. ata [k]. Row & amp; & J==m. ata [k]. Col)
{
Printf (" % d ", m. ata [k]. E);
K++;
} else {printf (" 0 "); }
}
printf("\n");
}
return 1;
}
//two triple addition
Void AddTSMatrix (TSMatrix TSMatrix M, T, TSMatrix * Sum)
{
Int I=1, j=1, k=1;//is representing the number of non-zero element
The Sum - & gt; M=T.m.
The Sum - & gt; N=T.n;
//location directly find the non-zero elements, is not each cycle is
While (i<=M.l en & amp; & j<=T.l en)
{
If (m. ata [I] row{
The Sum - & gt; Data [k]=m. ata [I];
i++;
K++;
}
Else if (m. ata [I]. Row & gt; T.d ata [j]. Journal of row)
{
The Sum - & gt; Data [k]=T.d ata [j];
j++;
K++;
}
Else if (m. ata [I]. Row==T.d ata [j]. Journal of row)
{
If (m. ata [I] col & lt; T.d ata [j]. Journal of col)
{
The Sum - & gt; Data [k]=m. ata [I];
i++;
K++;
}
Else if (m. ata [I] col & gt; T.d ata [j]. Journal of col)
{
The Sum - & gt; Data [k]=T.d ata [j];
j++;
K++;
}
Else if (m. ata [I]. Col==T.d ata [j]. Journal of col)
{
If (m. ata [I]. E + T.d ata [j]. J e!=0)
{
The Sum - & gt; Data [k]. Row=T.d ata [j]. Journal of row;
The Sum - & gt; Data [k]. Col=T.d ata [j]. Journal of col.
The Sum - & gt; Data [k]. E=T.d ata [j]. E + m. ata [I] e;
K++;
}
//k++;
j++;
i++;
}
}
While (i<=M.l en)
{
The Sum - & gt; Data [k]=m. ata [I];
K++;
i++;
}
While (j<=T.l en)
{
The Sum - & gt; Data [k]=T.d ata [j];
K++;
j++;
}
The Sum - & gt; Len=k - 1;
}
}
Int main ()
{
TSMatrix M, T, Sum;
CreateTSMatrix (& amp; M);
CreateTSMatrix (& amp; T);
Printf (" -- -- -- -- -- -- -- -- -- -- -- -- -- - the first matrix \ n ");
PrintTSMatrix (M);
Printf (" -- -- -- -- -- -- -- -- -- -- -- -- -- - the second matrix - \ n ");
PrintTSMatrix (T);
AddTSMatrix (M, T, & amp; The Sum);
Printf (" -- -- -- -- - the result of the matrix addition - \ n ");
PrintTSMatrix (Sum);
return 0;
}
CodePudding user response:
Do you want corresponding row data together? What are you the expected results should be?CodePudding user response:
For is displayed in the contents of two matrices sum matrix together with the addition of a column of different directly inCodePudding user response:
The