标题:好久没帖子啦!!!!
取消只看楼主
寒风中的细雨
Rank: 17Rank: 17Rank: 17Rank: 17Rank: 17
等 级:贵宾
威 望:66
帖 子:1710
专家分:8645
注 册:2009-9-15
结帖率:100%
已结贴  问题点数:50 回复次数:3 
好久没帖子啦!!!!
矩阵类:
  实现如下系统操作:   
printf("*. 请根据下面提示操作!\n");
printf("1. 矩阵转置请按\"1\"键\n");
printf("2. 矩阵相加请按\"2\"键\n");
printf("3. 矩阵相减请按\"3\"键\n");
printf("4. 矩阵相乘请按\"4\"键\n");
printf("5. 退出系统请按\"5\"键\n");
搜索更多相关主题的帖子: 帖子 
2010-05-02 07:52
寒风中的细雨
Rank: 17Rank: 17Rank: 17Rank: 17Rank: 17
等 级:贵宾
威 望:66
帖 子:1710
专家分:8645
注 册:2009-9-15
得分:0 
尽量使用 好的方法   O( )
2010-05-02 07:55
寒风中的细雨
Rank: 17Rank: 17Rank: 17Rank: 17Rank: 17
等 级:贵宾
威 望:66
帖 子:1710
专家分:8645
注 册:2009-9-15
得分:0 
没人 自己顶
2010-05-03 22:32
寒风中的细雨
Rank: 17Rank: 17Rank: 17Rank: 17Rank: 17
等 级:贵宾
威 望:66
帖 子:1710
专家分:8645
注 册:2009-9-15
得分:0 
很粗糙     你改改把   没有处理错误操作的功能
#include <stdio.h>
#include <stdlib.h>

#define MAXSIZE  10//

typedef struct
{
    int i;
    int j;
    int e;
}Triplet;

typedef struct
{
    Triplet data[MAXSIZE+1];
    int rm;
    int cm;
    int tm;
}Tsmatrix;

void CreateSmatrix( Tsmatrix &M );//创建稀疏矩阵
void Printsmatrix( Tsmatrix M );//打印输出矩阵
void Transposesmatrix( Tsmatrix M, Tsmatrix &T );//矩阵转置
void Get_rpos( Tsmatrix M, int rpos[] );
void Multsmatrix( Tsmatrix M, Tsmatrix N, Tsmatrix &S );//两矩阵相乘
void Addsmatrix( Tsmatrix M, Tsmatrix N, Tsmatrix &Q );//两矩阵相加
void Sultsmatrix( Tsmatrix M, Tsmatrix N, Tsmatrix &Q );//两矩阵相减

int main()
{
    Tsmatrix M, N, Q;
    int i;

    while(1)
    {
        printf("*. 请根据下面提示操作!\n");
        printf("1. 矩阵转置请按\"1\"键\n");
        printf("2. 矩阵相加请按\"2\"键\n");
        printf("3. 矩阵相减请按\"3\"键\n");
        printf("4. 矩阵相乘请按\"4\"键\n");
        printf("5. 退出系统请按\"5\"键\n");
        
        scanf("%d", &i);
        switch(i)
        {
        case 1:
            CreateSmatrix( M );
            Printsmatrix( M );
            Transposesmatrix( M, Q );
            printf("输出转置矩阵\n");
            Printsmatrix( Q );
            break;
        case 4:
            CreateSmatrix( M );
            Printsmatrix( M );        
            CreateSmatrix( N );
            Printsmatrix( N );   
            printf("两矩阵相乘\n");
            Multsmatrix( M, N, Q);
            Printsmatrix(Q);
            break;
        case 3:
            CreateSmatrix( M );
            Printsmatrix( M );        
            CreateSmatrix( N );
            Printsmatrix( N );   
            printf("两矩阵相减\n");
            Sultsmatrix( M, N, Q );
            Printsmatrix(Q);
            break;
        case 2:
            CreateSmatrix( M );
            Printsmatrix( M );        
            CreateSmatrix( N );
            Printsmatrix( N );
            printf("两矩阵相加\n");
            Addsmatrix( M, N, Q );
            Printsmatrix( Q );
            break;
        case 5:
            exit(0);

        }
    }

    return 0;
}


//创建稀疏矩阵
void CreateSmatrix( Tsmatrix &M )
{
    printf("分别输入矩阵的行数、列数和非零元的总数:");
    scanf("%d%d%d", &M.rm, &M.cm, &M.tm);
    if( M.tm>MAXSIZE )
        exit(0);
    printf("请按行序从小到大顺序输入非零元\n");
    for(int n=1; n<=M.tm; n++ )
    {
        printf("输入行号和列号:");
        scanf("%d%d", &M.data[n].i, &M.data[n].j);
        printf("输入元素的值:");
        scanf("%d", &M.data[n].e);
    }
    printf("矩阵构造成功!\n");
}
//打印输出矩阵
void Printsmatrix( Tsmatrix M )
{
    int r, c, q=1;

    for( r=1; r<=M.rm; r++ )
    {
        for( c=1; c<=M.cm; c++ )
        {
            if( M.data[q].i==r && M.data[q].j==c && q<=M.tm )
            {
                printf("%4d", M.data[q].e);
                ++q;
            }
            else
                printf("%4d", 0);
        }
        printf("\n");
    }
}
//矩阵转置
void Transposesmatrix( Tsmatrix M, Tsmatrix &T )
{
    T.cm = M.rm;
    T.rm = M.cm;
    T.tm = M.tm;
    if( !M.tm )
        return;

    int *num, *cpot;
    num = (int *) malloc ((M.cm+1)*sizeof(int));
    cpot = (int *) malloc ((M.cm+1)*sizeof(int));
    int c, p;

    for( c=1; c<=M.cm; c++ )
        num[c] = 0;
    for( c=1; c<=M.tm; c++ )
        ++num[M.data[c].j];
   
    cpot[1] = 1;
    for( c=2; c<=M.cm; c++ )
        cpot[c] = cpot[c-1] + num[c-1];
    p = 1;
    while( p<= M.tm )
    {
        c = M.data[p].j;
        T.data[cpot[c]].i = M.data[p].j;
        T.data[cpot[c]].j = M.data[p].i;
        T.data[cpot[c]].e = M.data[p].e;
        ++p;
        ++cpot[c];
    }
}
//
void Get_rpos( Tsmatrix M, int rpos[] )
{
    int r, *num;
    num = (int *) malloc ((M.rm+1)*sizeof(int));

    for( r=1; r<=M.rm; r++ )
        num[r] = 0;
    for( r=1; r<=M.tm; r++ )
        ++num[M.data[r].i];
    rpos[1] = 1;
    for( r=2; r<=M.rm; r++ )
        rpos[r] = rpos[r-1] + num[r-1];
        
}
//两矩阵相乘
void Multsmatrix( Tsmatrix M, Tsmatrix N, Tsmatrix &S )
{
    if( M.cm != N.rm )
        return;
    if( M.tm*N.tm==0 )
        return;
    S.cm = N.cm;
    S.rm = M.rm;
    S.tm = 0;
   
    int qrow, qcol, tp, t, p, q;
    int *rposM, *rposN, *ctemp;
    rposM = (int *) malloc ((M.rm+1)*sizeof(int));
    rposN = (int *) malloc ((N.rm+1)*sizeof(int));
    ctemp = (int *) malloc ((N.cm+1)*sizeof(int));

    Get_rpos( M, rposM );
    Get_rpos( N, rposN );

    for( qrow=1; qrow<=M.rm; ++qrow )
    {
        for( int i=1; i<=N.cm; ++i )
            ctemp[i] = 0;
        if( qrow<M.rm )
            tp = rposM[qrow+1];
        else
            tp = M.tm +1;
        for( p=rposM[qrow]; p<tp; ++p )
        {
            if( M.data[p].j<N.rm )
                t = rposN[M.data[p].j+1];
            else
                t = N.tm + 1;
            for( q=rposN[M.data[p].j]; q<t; ++q )
            {
                qcol = N.data[q].j;
                ctemp[qcol] += M.data[p].e * N.data[q].e;
            }
        }
        for( qcol=1; qcol<=S.cm; ++qcol )
            if( ctemp[qcol] )
            {
                if( ++S.tm > MAXSIZE)
                    return;
                S.data[S.tm].i = qrow;
                S.data[S.tm].j = qcol;
                S.data[S.tm].e = ctemp[qcol];
            }
    }
}
//两矩阵相加
void Addsmatrix( Tsmatrix M, Tsmatrix N, Tsmatrix &Q )
{
    if( M.cm!=N.cm || M.rm!=N.rm )
        return;
    Q.cm = M.cm;
    Q.rm = M.rm;
    Q.tm = 0;
    int m = 1, n = 1;
    while( m<=M.tm && n<=N.tm )
    {
        if( M.data[m].i > N.data[n].i )
        {
            ++Q.tm;
            Q.data[Q.tm].i = N.data[n].i;
            Q.data[Q.tm].j = N.data[n].j;   
            Q.data[Q.tm].e = N.data[n].e;
            ++n;
        }
        else if( M.data[m].i == N.data[n].i )
        {
            if( M.data[m].j > N.data[n].j )
            {
                ++Q.tm;
                Q.data[Q.tm].i = N.data[n].i;
                Q.data[Q.tm].j = N.data[n].j;   
                Q.data[Q.tm].e = N.data[n].e;
                ++n;
            }
            else if( M.data[m].j == N.data[n].j )
            {
                ++Q.tm;
                Q.data[Q.tm].i = N.data[n].i;
                Q.data[Q.tm].j = N.data[n].j;   
                Q.data[Q.tm].e = N.data[n].e + M.data[m].e;
                ++n;
                ++m;
                if( !Q.data[Q.tm].e )
                    --Q.tm;
            }
            else if( M.data[m].j < N.data[n].j)
            {            
                ++Q.tm;
                Q.data[Q.tm].i = M.data[m].i;
                Q.data[Q.tm].j = M.data[m].j;   
                Q.data[Q.tm].e = M.data[m].e;
                ++m;
            }
        }
        else if( M.data[m].i < N.data[n].i )
        {   
            ++Q.tm;
            Q.data[Q.tm].i = M.data[m].i;
            Q.data[Q.tm].j = M.data[m].j;   
            Q.data[Q.tm].e = M.data[m].e;
            ++m;
        }
    }
    if(m>M.tm && n<=N.tm)
        while(n<=N.tm)
        {
            ++Q.tm;
            Q.data[Q.tm].i = N.data[n].i;
            Q.data[Q.tm].j = N.data[n].j;   
            Q.data[Q.tm].e = N.data[n].e;
            ++n;
        }
    if(m<=M.tm && n>N.tm )
        while(m<=M.tm)
        {
            ++Q.tm;
            Q.data[Q.tm].i = M.data[m].i;
            Q.data[Q.tm].j = M.data[m].j;   
            Q.data[Q.tm].e = M.data[m].e;
            ++m;
        }
}
//两矩阵相减
void Sultsmatrix( Tsmatrix M, Tsmatrix N, Tsmatrix &Q )
{
    if( M.cm!=N.cm && M.rm!=N.rm )
        return;
    Q.cm = M.cm;
    Q.rm = M.rm;
    Q.tm = 0;
    int m = 1, n = 1;
    while( m<=M.tm && n<=N.tm )
    {
        if( M.data[m].i > N.data[n].i )
        {
            ++Q.tm;
            Q.data[Q.tm].i = N.data[n].i;
            Q.data[Q.tm].j = N.data[n].j;   
            Q.data[Q.tm].e = - N.data[n].e;
            ++n;
        }
        else if( M.data[m].i == N.data[n].i )
        {
            if( M.data[m].j > N.data[n].j )
            {
                ++Q.tm;
                Q.data[Q.tm].i = N.data[n].i;
                Q.data[Q.tm].j = N.data[n].j;   
                Q.data[Q.tm].e = - N.data[n].e;
                ++n;
            }
            else if( M.data[m].j == N.data[n].j )
            {
                ++Q.tm;
                Q.data[Q.tm].i = N.data[n].i;
                Q.data[Q.tm].j = N.data[n].j;   
                Q.data[Q.tm].e =  M.data[m].e - N.data[n].e;
                ++n;
                ++m;
                if( !Q.data[Q.tm].e )
                    --Q.tm;
            }
            else
            {            
                ++Q.tm;
                Q.data[Q.tm].i = M.data[m].i;
                Q.data[Q.tm].j = M.data[m].j;   
                Q.data[Q.tm].e = M.data[m].e;
                ++m;
            }
        }
        else if( M.data[m].i < N.data[n].i )
        {   
            ++Q.tm;
            Q.data[Q.tm].i = M.data[m].i;
            Q.data[Q.tm].j = M.data[m].j;   
            Q.data[Q.tm].e = M.data[m].e;
            ++m;
        }
    }
    if(m>M.tm && n<=N.tm)
        while(n<=N.tm)
        {
            ++Q.tm;
            Q.data[Q.tm].i = N.data[n].i;
            Q.data[Q.tm].j = N.data[n].j;   
            Q.data[Q.tm].e = -N.data[n].e;
            ++n;
        }
    if(m<=M.tm && n>N.tm )
        while(m<=M.tm)
        {
            ++Q.tm;
            Q.data[Q.tm].i = M.data[m].i;
            Q.data[Q.tm].j = M.data[m].j;   
            Q.data[Q.tm].e = M.data[m].e;
            ++m;
        }
}
2010-05-05 22:48



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-305104-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 1.221177 second(s), 8 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved