희소 행렬의 삼원조 순서표의 C 언어 구현

4407 단어 C 언어
다음 프로그램에서 삼원조로 두 개의 덧셈 행렬을 실현하는 데 버그가 있어서 한참을 생각했지만 해결 방법을 생각해 내지 못했다.o(╯□╰)o
#include <stdio.h>
#include <stdlib.h>

#ifndef OK
#define OK 1
#endif
#ifndef ERROR
#define ERROR 0
#endif

#define MAXSIZE 12500

typedef int Status;
typedef int ElemType;
typedef struct{
	int	i, j;
	ElemType e;
}Triple;
typedef struct{
	Triple	data[MAXSIZE + 1];
	int	mu, nu, tu;
}TSMatrix;

TSMatrix *CreateMatrix(TSMatrix *M);	//create TSMatrix
Status 	InsertElem(TSMatrix *M, int row, int col, ElemType e);
Status	PrintSMatrix(TSMatrix M);	//output the element of M
TSMatrix *CopySMatrix(TSMatrix M, TSMatrix *T);//copy TSMatrix M to TSMatrix T
TSMatrix *AddSMatrix(TSMatrix M, TSMatrix N, TSMatrix *Q);//Q=M+N
TSMatrix *TransposeSMatrix(TSMatrix M, TSMatrix *T);
#if 0
void	DestroyMatrix(TSMatrix *M); //destroy TSMatrix
Status	SubSMatrix(TSMatrix M, TSMatrix N, TSMatrix *Q);//Q=M-N
Status	MultSMatrix(TSMatrix M, TSMatrix N, TSMatrix *Q);//Q=M*N
#endif

TSMatrix *CreateMatrix(TSMatrix *M)
{
	M = (TSMatrix *)malloc(sizeof(TSMatrix));
	if(!M){
		printf("alloc memory failed.
"); return ERROR; } printf("please input the column number:"); scanf("%d", &M->mu); printf("please input the line number:"); scanf("%d", &M->nu); M->tu = 0; return M; } Status InsertElem(TSMatrix *M, int row, int col, ElemType e) { int p, i, t; //check out the segment if(M->tu >= MAXSIZE) return ERROR; if(row > M->nu || col > M->mu) return ERROR; p = 1; if(M->tu == 0){ M->data[p].i = row; M->data[p].j = col; M->data[p].e = e; M->tu++; return OK; } p = M->tu+1; for(t = M->tu; t >= 1; t--){ if((row <= M->data[t].i)&&(col <= M->data[t].j))p--; } if((row == M->data[p].i && col == M->data[p].j)){ M->data[p].e = e; return OK; } for(i = M->tu; i >= p; i--){ M->data[i+1].i = M->data[i].i; M->data[i+1].j = M->data[i].j; M->data[i+1].e = M->data[i].e; } M->data[p].i = row; M->data[p].j = col; M->data[p].e = e; M->tu++; return OK; } //copy TSMatrix M to TSMatrix T TSMatrix *CopySMatrix(TSMatrix M, TSMatrix *T) { int i; T = (TSMatrix *)malloc(sizeof(TSMatrix)); if(!T){ printf("alloc memory failed.
"); return ERROR; } T->mu = M.mu; T->nu = M.nu; T->tu = M.tu; for(i = 1; i <= M.tu; i++){ T->data[i].i = M.data[i].i; T->data[i].j = M.data[i].j; T->data[i].e = M.data[i].e; } return T; } TSMatrix *AddSMatrix(TSMatrix M, TSMatrix N, TSMatrix *Q) { int i, j, p; int row, col, data; Q = (TSMatrix *)malloc(sizeof(TSMatrix)); if(!Q){ printf("alloc memory for Q failed.
"); return ERROR; } Q->mu = M.mu; Q->nu = M.nu; Q->tu = 0; p = 1; for(i = 1; i <= M.tu; i++){ Q->data[p].i = M.data[i].i; Q->data[p].j = M.data[i].j; Q->data[p].e = M.data[i].e; Q->tu++; for(j = 1; j <= N.tu; j++){ if((M.data[i].i == N.data[j].i)&&(M.data[i].j == N.data[j].j)){ Q->data[p].e += N.data[j].e; } } p++; } return Q; } TSMatrix *TransposeSMatrix(TSMatrix M, TSMatrix *T) { int q, col, p; T = (TSMatrix *)malloc(sizeof(TSMatrix)); if(!T){ printf("alloc memory failed.
"); return ERROR; } T->mu = M.nu; T->nu = M.mu; T->tu = M.tu; if(T->tu){ q = 1; for(col = 1; col <= M.nu; ++col) for(p = 1; p <= M.tu; ++p) 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; } Status PrintSMatrix(TSMatrix M) { int i = 1; while(i <= M.tu){ printf("%d ", M.data[i].e); i++; } putchar('
'); return OK; } int main(void) { TSMatrix *M, *T, *Q, *X; M = CreateMatrix(M); InsertElem(M, 2, 3, 4); InsertElem(M, 3, 4, 2); InsertElem(M, 3, 3, 1); InsertElem(M, 3, 2, 1); InsertElem(M, 4, 4, 3); printf("M->mu = %d.
", M->mu); printf("M->nu = %d.
", M->nu); printf("M->tu = %d.
", M->tu); PrintSMatrix(*M); T = CopySMatrix(*M, T); PrintSMatrix(*T); InsertElem(T, 4, 3, 3); Q = AddSMatrix(*M, *T, Q); PrintSMatrix(*Q); X = TransposeSMatrix(*M, X); PrintSMatrix(*X); return 0; }

좋은 웹페이지 즐겨찾기