C 언어로 특수 매트릭스 저장

4238 단어
다음에 실현된 특수 매트릭스 저장 방법
삼원조 순서 저장 방법 & 전환
#include 
#include 
#define MAXSIZE 12500
//            
// written at 2018-6-1 23:37:22

typedef int ElemType;
typedef struct{
    int i;  // row
    int j;  // column
    ElemType e;
}tripple;
typedef struct{
    tripple data[MAXSIZE+1];  // 0 not use
    int mu, nu, tu;  //  , ,     
}tsMatrix;

tripple init_tribble(int i, int j, ElemType e){
    tripple temp;
    temp.i = i;
    temp.j = j;
    temp.e = e;
    return temp;
}

void print_sparse_matrix(tsMatrix temp){
    printf("**********START************
"); printf("i\tj\tv\t
"); for (int i=1; i<=temp.tu; i++){ printf("%d\t%d\t%d\t
",temp.data[i].i,temp.data[i].j,temp.data[i].e); } printf("***********END*************
"); } void transposeMatrix(tsMatrix m, tsMatrix *t){ // t->mu = m.nu; // step 1 t->nu = m.mu; // step 1 t->tu = m.tu; // step 2 & step 3 if(t->tu){ int q=1; for (int col=1;col<=m.nu;++col){ for (int 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++; } } } } } void quickTranspose(tsMatrix m, tsMatrix *t){ // M b.data , t->mu = m.nu; t->nu = m.mu; t->tu = m.tu; if (t->tu){ int num[m.nu + 1]; // 0 not use for (int col=1; col<=m.nu; col++) num[col] = 0; // num for (int temp=1; temp<=m.tu; temp++) ++num[m.data[temp].j]; int cpot[m.nu + 1]; // 0 not use cpot[1] = 1; // // col b.data for (int col=2; col<=m.nu;++col){ cpot[col]=cpot[col-1]+num[col-1]; } for (int p=1;p<=m.tu;++p){ int col = m.data[p].j; int q=cpot[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; ++cpot[col]; } } } void main(){ // init the martix and let the matrix not empty tsMatrix temp; temp.data[1] = init_tribble(1, 2, 12); temp.data[2] = init_tribble(1, 3, 9); temp.data[3] = init_tribble(3, 1, -3); temp.data[4] = init_tribble(3, 6, 14); temp.data[5] = init_tribble(4, 3, 24); temp.data[6] = init_tribble(5, 2, 18); temp.data[7] = init_tribble(6, 1, 15); temp.data[8] = init_tribble(6, 4, -7); // 97 M temp.mu = 6; temp.nu = 7; temp.tu = 8; print_sparse_matrix(temp); tsMatrix t; tsMatrix t2; transposeMatrix(temp, &t2); quickTranspose(temp, &t); printf("\t \t
"); print_sparse_matrix(t2); printf("\t \t
"); print_sparse_matrix(t); }

출력 결과는 다음과 같습니다.
**********START************
i       j       v
1       2       12
1       3       9
3       1       -3
3       6       14
4       3       24
5       2       18
6       1       15
6       4       -7
***********END*************
            
**********START************
i       j       v
1       3       -3
1       6       15
2       1       12
2       5       18
3       1       9
3       4       24
4       6       -7
6       3       14
***********END*************
            
**********START************
i       j       v
1       3       -3
1       6       15
2       1       12
2       5       18
3       1       9
3       4       24
4       6       -7
6       3       14
***********END*************

행 논리 링크 순서표
#include 
#include 
#define MAXSIZE 12500
/*
         
                          
written at 2018-6-2 18:26:30
*/

typedef int ElemType;
typedef struct{
    int i;  // row
    int j;  // column
    ElemType e;
}tripple;
typedef struct RLSMatrix{
    tripple data[MAXSIZE+1];
    int mu, nu, tu;
    int rops[MAXRC + 1];  //             
}RLSMatrix;

좋은 웹페이지 즐겨찾기