#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 200
#define dim_ROW 20
#define dim_COL 20
typedef struct
{
int row;
int col;
int value;
}Triple;//进行类型描述
typedef struct
{
Triple m_data[MAX_SIZE];
int m_num[dim_ROW], m_rops[dim_ROW] ,m_row,m_col,m_without;//存放各行非零元个数以及
//存放各行第一个非零元在矩阵中的位置
}Matrix;
//创建一个稀疏矩阵
Matrix * create_matrix()
{
int index;
Matrix *temp = NULL;
temp = (Matrix*) malloc (sizeof(Matrix));
printf("\n\n*****************************\n");
printf(" 稀疏矩阵的运算程序如下\n");
printf("\n请输入矩阵的行数,列数,非零元的个数: ");
scanf("%d%d%d", &(temp->m_row), &(temp->m_col), &(temp->m_without));
if (NULL == temp)
{//判断分配是否成功 ,如果溢出设定值则结束
exit(0);
}
if(temp->m_col<1 || temp->m_row<1 ||
temp->m_without > temp->m_col*temp->m_row)
{
printf("稀疏矩阵出错\n");
exit(0);
}
for (index=1; index<=temp->m_row; ++index)
{
temp->m_num[index] = 0;//非零元数的个数0
temp->m_rops[index] = dim_COL;//第一个非零元数的位置
}
for (index=1; index<=temp->m_without; ++index)
{
printf("第%d个非零元row:", index);
scanf("%d", &(temp->m_data[index].row));
temp->m_num[temp->m_data[index].row]++;
printf("第%d个非零元col:", index);
scanf("%d", &(temp->m_data[index].col));
if (temp->m_rops[temp->m_data[index].row] == dim_COL)
{
temp->m_rops[temp->m_data[index].row] = temp->m_data[index].col;
}
else
{
if (temp->m_rops[temp->m_data[index].row] > temp->m_data[index].col)
{
temp->m_rops[temp->m_data[index].row] = temp->m_data[index].col;
}
}
printf("第%d个非零元value:", index);
scanf("%d", &(temp->m_data[index].value));
}
return temp;
}
Matrix * prout(int p[dim_ROW][dim_COL], Matrix *add_var, Matrix *varpm1, Matrix *varpm2)
{
int counter;//计数器
int index = 1;
int row, col;
for (row=1; row<=add_var->m_row; ++row)
{
add_var->m_num[row] = 0;
add_var->m_rops[row] = dim_COL;
counter = 0;
col = varpm1->m_rops[row] > varpm2->m_rops[row] ?
varpm2->m_rops[row] : varpm1->m_rops[row];
for (; col<=add_var->m_col; ++col)
{
if (0 != p[row][col])
{
add_var->m_without++;
add_var->m_data[index].col = col;
add_var->m_data[index].row = row;
add_var->m_data[index].value = p[row][col];
add_var->m_num[row]++;
if (add_var->m_rops[index] == dim_COL)
{
add_var->m_rops[index] = add_var->m_data[index].col;//标记第一个非零列号
}
else
{
if (add_var->m_rops[index] > add_var->m_data[index].col)
{
add_var->m_rops[index] = add_var->m_data[index].col;//变换第一非零的位置
}
}
counter++;
index++;
}
if (counter >= (varpm1->m_num[row]+varpm2->m_num[row]))
{
break;
}
}
}
return add_var;
}
//两个稀疏矩阵相加
Matrix * add_matrix(Matrix *varpm1, Matrix *varpm2)
{
int index;
int p[dim_ROW][dim_COL] = {0};
Matrix *add_var = NULL;
//判断两个矩阵是否满足相加的条件
if ((varpm1->m_col != varpm2->m_col) ||
(varpm1->m_row != varpm2->m_row))
{
printf("\n两个稀疏矩阵条件不符,不能相加!\n");
}
add_var = (Matrix*) malloc (sizeof(Matrix));
add_var->m_col = varpm1->m_col;
add_var->m_row = varpm1->m_row;
add_var->m_without = 0;
for (index=1; index<=varpm1->m_without; ++index)
{
p[varpm1->m_data[index].row][varpm1->m_data[index].col] = varpm1->m_data[index].value;
}
for (index=1; index<=varpm2->m_without; ++index)
{
if (0 == p[varpm2->m_data[index].row][varpm2->m_data[index].col])
{
p[varpm2->m_data[index].row][varpm2->m_data[index].col] = varpm2->m_data[index].value;
}
else
{
p[varpm2->m_data[index].row][varpm2->m_data[index].col] += varpm2->m_data[index].value;
}
}
return prout(p, add_var, varpm1, varpm2);
}
//相减
Matrix * sub_matrix(Matrix *varpm1, Matrix *varpm2)
{
int index;
int p[dim_ROW][dim_COL] = {0};
Matrix *add_var = NULL;
//判断两个矩阵是否满足相减的条件
if ((varpm1->m_col != varpm2->m_col) ||
(varpm1->m_row != varpm2->m_row))
{
printf("\n两个稀疏矩阵条件不符,不能相减!\n");
}
add_var = (Matrix*) malloc (sizeof(Matrix));
add_var->m_col = varpm1->m_col;
add_var->m_row = varpm1->m_row;
add_var->m_without = 0;
for (index=1; index<=varpm1->m_without; ++index)
{
p[varpm1->m_data[index].row][varpm1->m_data[index].col] = varpm1->m_data[index].value;
}
for (index=1; index<=varpm2->m_without; ++index)
{
if (0 == p[varpm2->m_data[index].row][varpm2->m_data[index].col])
{
p[varpm2->m_data[index].row][varpm2->m_data[index].col] = -varpm2->m_data[index].value;
}
else
{
p[varpm2->m_data[index].row][varpm2->m_data[index].col] -= varpm2->m_data[index].value;
}
}
return prout(p, add_var, varpm1, varpm2);
}
//相乘
Matrix * mul_matrix(Matrix *varpm1, Matrix *varpm2)
{
int sum;
int counter;//计数器
int index = 1;
int row, col;
int p1[dim_ROW][dim_COL] = {0};
int p2[dim_ROW][dim_COL] = {0};
Matrix *add_var = NULL;
if (varpm1->m_col != varpm2->m_row)
{
printf("\n两个稀疏矩阵条件不符,不能相乘!\n");
}
add_var = (Matrix*) malloc (sizeof(Matrix));
add_var->m_col = varpm2->m_col;
add_var->m_row = varpm1->m_row;
add_var->m_without = 0;
for (index=1; index<=varpm1->m_without; ++index)
{
p1[varpm1->m_data[index].row][varpm1->m_data[index].col] = varpm1->m_data[index].value;
}
for (index=1; index<=varpm2->m_without; ++index)
{
p2[varpm2->m_data[index].row][varpm2->m_data[index].col] = varpm2->m_data[index].value;
}
counter = 1;
for (row=1; row<=add_var->m_row; ++row)
{
add_var->m_num[row] = 0;
add_var->m_rops[row] = dim_COL;
for (index=1; index<=varpm2->m_col; ++index)
{
sum = 0;//求和
col = varpm1->m_rops[row];
for (; col<=varpm2->m_row; ++col)
{
sum += p1[row][col]*p2[col][index];
}
if (0 != sum)
{
add_var->m_without++;
add_var->m_data[counter].col = index;
add_var->m_data[counter].row = row;
add_var->m_data[counter].value = sum;
add_var->m_num[row]++;
if (add_var->m_rops[index] == dim_COL)
{
add_var->m_rops[index] = add_var->m_data[index].col;
}
counter++;
}
}
}
return add_var;
}
Matrix * destroy_matrix(Matrix *temp)
{
free(temp);
temp = NULL;
return temp;
}
void show_matrix(Matrix *temp)
{
int index;
int row, col;
int p[dim_ROW][dim_COL] = {0};
for (index=1; index<=temp->m_without; ++index)
{
p[temp->m_data[index].row][temp->m_data[index].col] = temp->m_data[index].value;
}
printf("\n顺序得�