【作业】DS稀疏矩阵

写了两个小时,书上代码好难看啊

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<string.h>

#define TRUE 1
#define FALSE 0
#define ERROR 0
#define OK 1
#define OVERFLOW -1

#define MAXSIZE 12500

typedef int Status;
typedef int ElemType;

using namespace std;
struct triple {
    int i, j;//row,col
    ElemType e;
    triple(int i=0, int j=0, ElemType e=0) :i(i), j(j), e(e) {}
};

typedef struct {
    triple data[MAXSIZE + 1];
    int mu, nu, tu;//row,col,ele
}TSMatrix;

typedef struct {
    triple data[MAXSIZE + 1];
    int rpos[MAXSIZE + 1];
    int mu, nu, tu;//row,col,ele
}RLSMatrix;
int num[MAXSIZE], cpot[MAXSIZE],ctemp[MAXSIZE];
TSMatrix T, M;
RLSMatrix m, n,q;


Status TransposeSMatrix(TSMatrix M, TSMatrix &T) {
    T.mu = M.nu, T.nu = M.mu, T.tu = M.tu;
    if (T.tu) {
        int q = 0;
        for (int col = 0; col < M.nu; col++) 
            for(int p=0;p<M.tu;p++)
                if (col == M.data[p].j) {
                    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 OK;
}

Status FastTransposeSMatrix(TSMatrix M, TSMatrix &T) {
    T.mu = M.nu, T.nu = M.mu, T.tu = M.tu;
    if (T.tu) {
        for (int col = 0; col < M. nu; col++)num[col] = 0;
        for (int t = 0; t < M.tu; t++)num[M.data[t].j]++;
        cpot[0] = 0;
        for (int col = 1; col < M.nu; col++)cpot[col] = cpot[col - 1] + num[col - 1];
        for (int p = 0; p < M.tu; p++) {
            int q = cpot[M.data[p].j];
            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[M.data[p].j]++;
        }
    }
    return OK;
}

Status Setrpos(RLSMatrix& M) {
    for (int row = 0; row < M.mu; row++)num[row] = 0;
    for (int t = 0; t < M.tu; t++)num[M.data[t].i]++;
    M.rpos[0] = 0;
    for (int row = 1; row < M.mu; row++)M.rpos[row] = M.rpos[row - 1] + num[row - 1];
    return OK;
}

Status MultMatrix(RLSMatrix M, RLSMatrix N, RLSMatrix &Q) {
    if (M.nu != N.mu)return ERROR;
    Q.mu = M.mu; Q.nu = N.nu; Q.tu = 0;
    if (M.tu*N.tu != 0) {
        for (int arow = 0; arow < M.mu; arow++) {
            memset(ctemp, 0, sizeof(ctemp));
            Q.rpos[arow] = Q.tu + 1;    
            int tp = 0; if (arow < M.mu-1)tp = M.rpos[arow + 1]; else tp = M.tu + 1;//tp
            for (int pp = M.rpos[arow]; pp < tp; pp++) {
                int brow = M.data[pp].j; //brow
                int t = 0; if (brow < N.mu-1)t = N.rpos[brow + 1]; else t = N.tu + 1;//t            
                for (int qq = N.rpos[brow]; qq < t; qq++) {
                    ctemp[N.data[qq].j] += M .data[pp].e*N.data[qq].e;//ctemp
                }//for q;
            }//for p
            for (int ccol = 0; ccol < Q.nu; ccol++)if (ctemp[ccol]) {
                if (++Q.tu > MAXSIZE)return ERROR;
                Q.data[Q.tu-1] = triple(arow, ccol, ctemp[ccol]);

            }
        }//for aarow
    }//if !=0
    return OK;
}

Status Read(TSMatrix& M) {
    if (scanf("%d%d%d", &M.mu, &M.nu, &M.tu) != 3)return FALSE;
    else {
        //cin >> M.mu >> M.nu >> M.tu;
        for (int i = 0; i < M.tu; i++) {
            cin >> M.data[i].i >> M.data[i].j >> M.data[i].e;
        }
        return OK;
    }
}

Status Read(RLSMatrix& M) {
    if (scanf("%d%d%d", &M.mu, &M.nu, &M.tu) != 3)return FALSE;
    else {
        //cin >> M.mu >> M.nu >> M.tu;
        for (int i = 0; i < M.tu; i++) {
            cin >> M.data[i].i >> M.data[i].j >> M.data[i].e;
        }
        return OK;
    }
}

Status Print(TSMatrix T) {
    for (int i = 0; i < T.tu; i++) {
        printf("%5d%5d%5d\n", T.data[i].i, T.data[i].j, T.data[i].e);
    }
    return OK;
}

Status Print(RLSMatrix T) {
    for (int i = 0; i < T.tu; i++) {
        printf("%5d%5d%5d\n", T.data[i].i, T.data[i].j, T.data[i].e);
    }
    return OK;
}

Status PrintFormat(TSMatrix T) {
    int cnt = 0;
    for (int i = 0; i < T.mu; i++) {
        for (int j = 0; j < T.nu; j++) {
            if (T.data[cnt].i == i&&T.data[cnt].j == j)
                printf("%5d", T.data[cnt++].e);
            else printf("%5d", 0);
        }
        cout << endl;
    }
    return OK;
}

Status PrintFormat(RLSMatrix T) {
    int cnt = 0;
    for (int i = 0; i < T.mu; i++) {
        for (int j = 0; j < T.nu; j++) {
            if (T.data[cnt].i == i&&T.data[cnt].j == j)
                printf("%5d", T.data[cnt++].e);
            else printf("%5d", 0);
        }
        cout << endl;
    }
    return OK;
}


int main() {
    while (Read(m)) {
        Read(n);
        //cout << 1 << endl;
        
        Setrpos(m); Setrpos(n);
        //Print(M); cout << endl;
        puts("A:");
        PrintFormat(m); cout << endl;
        puts("B:");
        PrintFormat(n); cout << endl;
        MultMatrix(m, n, q);
        puts("A*B:");
        PrintFormat(q);
        //Print(q);
    }
    //system("pause");
}
/*
3 4 4
0 0 3
0 3 5
1 1 -1
2 0 2

4 2 4
0 1 2
1 0 1
2 0 -2
2 1 4


*/

 

posted @ 2018-05-04 15:52  SuuTTT  阅读(162)  评论(0编辑  收藏  举报