#include<stdio.h>
#include<stdlib.h>
#define OK 1
#define ERROR 0
typedef int ElemType, Status;
#define MAXSIZE 256
typedef struct
{
int i, j; //数据所在的行、列值
ElemType v; //数据元素值
}Triple;
typedef struct
{
Triple arr[MAXSIZE]; //非零元素的三元组
int Rows, Cols, Nums; //矩阵的行数、列数、非零元素个数
}SqSMatrix;
Status CtreateSMatrix(SqSMatrix& M) //矩阵的创建
{
printf("行数:");
scanf_s("%d", &M.Rows);
printf("列数:");
scanf_s("%d", &M.Cols);
printf("非零元个数:");
scanf_s("%d", &M.Nums);
printf("按行优先依次输入元素的行值、列值、数值:\n");
int i = 0;
while (i < M.Nums)
{
scanf_s("%d%d%d", &M.arr[i].i, &M.arr[i].j, &M.arr[i].v);
i++;
}
return OK;
}
Status DestroySMatrix(SqSMatrix& M) //矩阵的销毁
{
M.Cols = 0;
M.Nums = 0;
M.Rows = 0;
return OK;
}
Status PrintSMatrix(SqSMatrix M) //矩阵的输出
{
if (M.Rows == 0) return ERROR;
int cnt = 0;
for (int i = 0; i < M.Rows; i++)
{
for (int j = 0; j < M.Cols; j++)
{
if (cnt < M.Nums && M.arr[cnt].i == i && M.arr[cnt].j == j)
{
printf("%d ", M.arr[cnt].v);
cnt++;
}
printf("0 ");
}
printf("\n");
}
return OK;
}
Status CopySMatrix(SqSMatrix M, SqSMatrix &T) //矩阵的拷贝
{
三元组矩阵的基本操作——加、减、矩阵乘、转置
最新推荐文章于 2024-10-09 23:58:14 发布