用C实现:将二维数组中存放的稀疏矩阵压缩存储于三元组表中

用C实现:将二维数组中存放的稀疏矩阵压缩存储于三元组表中,第1张

#include <stdio.h>

#define maxsize 12500

#define elemtype int

typedef int status

typedef struct

{

int i,j

elemtype e

}triple

typedef struct

{

triple data[maxsize+1]

int mu,nu,tu

}tsmatrix

tsmatrix creatematrix()

{

tsmatrix M

int n

printf("输入矩阵的行数,列数,个数:\n")

scanf("%d%d%d",&M.mu,&M.nu,&M.tu)

printf("输入非零元的值(行数、列数和值):\n")

for(n=0n<M.tun++)

scanf("%d%d%d",&M.data[n].i,&M.data[n].j,&M.data[n].e)

return M

}

tsmatrix transposesmtrix(tsmatrix M,tsmatrix T)

{

int q,col,p

T.mu=M.nu T.nu=M.mu T.tu=M.tu

if(T.tu)

{

q=0

for(col=0col<M.nucol++)

for(p=0p<M.tup++)

if(M.data[p].j==col)

{

T.data[q].i=M.data[p].j

T.data[q].j=M.data[p].i

T.data[q].e=M.data[p].e

++q

}

}

return T

}

void print(tsmatrix M)

{

int x,y,n,k

for(x=0x<M.mux++)

{

for(y=0y<M.nuy++)

{

k=0

for(n=0n<M.tun++)

if((M.data[n].i==x)&&(M.data[n].j==y))

{

printf("%3d",M.data[n].e)

k=1

}

if(k==0)

printf("%3d",k)

}

printf("\n")

}

}

void main()

{

tsmatrix M,T

M=creatematrix()

printf("M矩阵为:\n")

print(M)

T=transposesmtrix(M,T)

printf("转置后的矩阵为:\n")

print(T)

}

我做的稀疏矩阵的转置,希望对你有用!

给你写好了,运行成功了。

注意有两个文件。

//array2d.h

#ifndef _ARRAY2D_H

#define _ARRAY2D_H

#include <xutility>

template<typename T>

class array2d

{

// Data

protected:

T **m_ppData

public:

long m_lRows, m_lCols

// Constructor

public:

array2d()

{

Init(0, 0)

}

array2d(long lRows, long lCols)

{

Init(lRows, lCols)

}

array2d(const array2d&other)

{

Init(other.GetRows(), other.GetCols())

T **pTmp = other.GetData()

if (m_lRows &&m_lCols)

{

copy(pTmp[0], pTmp[0]+m_lRows*m_lCols, m_ppData[0])

}

}

array2d<T>operator= (const array2d&other)

{

Destroy()

Init(other.GetRows(), other.GetCols())

T **pTmp = other.GetData()

copy(pTmp[0], pTmp[0]+m_lRows*m_lCols, m_ppData[0])

return *this

}

virtual ~array2d()

{

Destroy()

}

// Attribute

// 重载()对数组元素进行存取

T&operator()(long lRows, long lCols)

{

return m_ppData[lRows][lCols]

}

long GetRows() const

{

return m_lRows

}

void SetRows(long lRows)

{

m_lRows = lRows

}

long GetCols() const

{

return m_lCols

}

void SetCols(long lCols)

{

m_lCols = lCols

}

void GetSize(long *plRows, long *plCols) const

{

*plRows = m_lRows

*plCols = m_lCols

}

void SetSize(long lRows, long lCols)

{

m_lRows = lRows

m_lCols = lCols

}

T** GetData() const

{

return m_ppData

}

private:

void Init(long lRows, long lCols)

{

m_lRows = lRows

m_lCols = lCols

if (lRows <= 0 || lCols <= 0)

{

m_ppData = NULL

return

}

T *pTmp = new T[lRows*lCols]

m_ppData = new T*[lRows]

for (long i=0i<lRows++i)

{

m_ppData[i] = &pTmp[i*lCols]

}

}

void Destroy()

{

if (m_ppData)

{

delete []m_ppData[0]

}

delete []m_ppData

m_ppData = NULL

}

}

#endif

//Matrix.cpp,你的主文件

#include "stdafx.h"

#include "array2d.h"

#include <vector>

using namespace std

typedef array2d<int>CMatrix

typedef vector<CMatrix>MatVec

bool ReadFile(char * filename, MatVec &matvec)

{

if (!filename) return false

FILE * fp = fopen(filename, "r")

if (!fp) return false

int num

fscanf(fp, "%d", &num)

matvec = MatVec(num)

for (int k=0k<num++k)

{

int row, col

fscanf(fp, "%d", &row)

fscanf(fp, "%d", &col)

matvec[k] = CMatrix(row, col)

int i, j

int n

for (i=0i<row++i)

{

for (j=0j<col++j)

{

fscanf(fp, "%d", &n)

matvec[k](i, j) = n

}

}

}

fclose(fp)

return true

}

bool WriteFile(char * filename, MatVec &matvec)

{

if (!filename) return false

FILE * fp = fopen(filename, "w")

if (!fp) return false

for (int k=0k<matvec.size()++k)

{

fprintf(fp, "Matrix #%d\n", k+1)

int i, j

for (i=0i<matvec[k].GetRows()++i)

{

for (j=0j<matvec[k].GetRows()++j)

{

if ( matvec[k](i, j) != 0 )

{

fprintf(fp, "%d, %d\n", i+1, j+1)

}

}

}

fprintf(fp, "\n")

}

fclose(fp)

return true

}

int main(int argc, char* argv[])

{

MatVec matvec

char * inputfilename = "f:\\SampleInput.txt"

if (!ReadFile(inputfilename, matvec)) return 1

char * outputfilename = "f:\\Output.txt"

if (!WriteFile(outputfilename, matvec)) return 1

printf("Hello World!\n")

return 0

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/12124496.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-21
下一篇 2023-05-21

发表评论

登录后才能评论

评论列表(0条)

保存