前言:第二道完全是按照我自己思路写的,没按题目所给的函数写
不想吐槽题目了,感觉浪费了我半个小时去写它(提交c++,因为C语言在99标准之前没有Bool类型,他题目加了枚举类型,但我没加)
1.
实现稀疏矩阵(采用三元组表示法)的基本运算(30分)
#include <stdio.h>
#define maxsize 100
typedef enum {
false,true} bool;
typedef struct
{
int data[100][100]; /*二维数组*/
int rows,cols; /*行数和列数*/
} Matrix; //稀疏矩阵
typedef struct
{
int row,col; /*行号、列号*/
int item; /*非零元素值*/
} TupleNode; /*三元组的类型 */
typedef struct
{
TupleNode data[maxsize];
int rows,cols; /*行数、列数*/
int nums; /*非零元个数*/
} TSMatrix; /*三元组顺序表的类型 */
void Compressmatrix(Matrix A, TSMatrix *B);//产生稀疏矩阵A的三元组表示t
bool Value(TSMatrix *t,int x,int i,int j);//将x赋值给三元组t中稀疏矩阵A的A[i][j],并返回true,若i,j非法,返回false
bool Assign(TSMatrix t,int *x,int i,int j); //取三元组t中稀疏矩阵A的A[i][j]的值赋给指针变量x所指变量,并返回true,若i,j非法,返回false
void Print(TSMatrix B); //输出三元组
void Transpmatrix (TSMatrix B, TSMatrix *C);//求三元组表示B的转置矩阵(仍使用三元组表示)
int main()
{
int i,j,k,x,rows,cols,N;
bool flag;
Matrix A;
TSMatrix B,C;
scanf("%d%d",&rows,&cols);
A.rows=rows;
A.cols=cols;
for ( i=0; i<rows; i++)
for (j=0; j<cols; j++)
scanf("%d",&A.data[i][j]);
Compressmatrix(A,&B);
Print(B);
Transpmatrix (B, &C);
printf("the transpose of this matrix\n");
Print(C);
scanf("%d",&N);
for (k=0; k<N; k++)
{
scanf("%d%d",&i,&j);
flag=Assign(B,&x,i,j);
if (flag==true)
{
printf("A[%d,%d]=%d\n",i,j,x);
}
else
{
printf("A[%d,%d] is wrong\n",i,j);
}
}
scanf("%d",&N