数据结构:矩阵程序C++实现

#include " SMatrix.h "
#include
" convert.h "
#include
< iostream >
#include
< fstream >
#include
< string >
#include
< vector >
#include
< typeinfo >
using   namespace  std;
template 
< class  Type >
void  Convert( const   string   & s, Type a[])
{
    
string  separate  =   " ,|  " ;
    vector
< Type >  vec;
    
int  pos  =   0 ;
    
int  pos2  =   0 ;
    
while ( string ::npos  !=  pos)
    {
        pos 
=  s.find_first_of(separate, pos2);
        
if (pos  ==  s.find_last_of(separate))
        {
            
if (strcmp(typeid(Type).name(),  " int " ==   0 )
            {
                vec.push_back(atoi(s.substr(pos2, pos 
-  pos2).c_str()));
                vec.push_back(atoi(s.substr(pos 
+   1 ).c_str()));
            }
            
break ;
        }

        
if ( 0   <  pos  -  pos2)
        {
            
if (strcmp(typeid(Type).name(),  " int " ==   0 )
                vec.push_back(atoi(s.substr(pos2, pos 
-  pos2).c_str()));
        }

        pos2 
=  pos  +   1 ;
    }

    
for ( int  i  =   0 ; i  <  vec.size(); i ++ )
        a[i] 
=  vec[i];
    
}

void  CreateSMatrix(TSMatrix  & m)
{
    ifstream f(
" smatrix.txt " );
    
char   * buf  =   new   char [ 100 ];
    
string  s;
    f.getline(buf,
100 );
    s 
=  buf;
    
int  a[ 3 ];
    Convert(s, a);
    m.mu 
=  a[ 0 ];
    m.nu 
=  a[ 1 ];
    m.tu 
=  a[ 2 ];
    
int  i  =   1 ;
    
while (f.getline(buf, 100 ))
    {
        s 
=  buf;
        Convert(s, a);
        m.data[i].i 
=  a[ 0 ];
        m.data[i].j 
=  a[ 1 ];
        m.data[i].e 
=  a[ 2 ];
        i
++ ;
    }

    f.close();
    delete[] buf;
}

void  PrintSMatrix(TSMatrix  & m)
{
    
int  i, j;
    
int  pos  =   1 ;
    
int  currentPos;
    
for (i  =   1 ; i  <=  m.tu; i ++ )
    {
        currentPos 
=  (m.data[i].i  -   1 *  m.nu  +  m.data[i].j;
        
for (j  =  pos; j  <  currentPos; j ++ )
        {
            cout
<< setw( 5 ) << 0 ;
            
if (j  %  m.nu ==   0 )
                cout
<< endl;
        }
        cout
<< setw( 5 ) << m.data[i].e;
        
if (currentPos  %  m.nu  ==   0 )
            cout
<< endl;
        pos 
=  currentPos  +   1 ;
    }

    
for (j  =  pos; j  <=  m.mu  *  m.nu; j ++ )
    {
        cout
<< setw( 5 ) << 0 ;
        
if (j  %  m.nu ==   0 )
            cout
<< endl;
    }
}

int  FastTransposeSMatrix(TSMatrix M, TSMatrix  & T)
{
    T.mu 
=  M.nu;
    T.nu 
=  M.mu;
    T.tu 
=  M.tu;

    
if (T.tu  ==   0 )
        
return   0 ;

    
int  col;
    
int  t;
    
int   * num  =   new   int [M.nu  +   1 ];
    
int   * cpot  =   new   int [M.nu  +   1 ];

    
for (col  =   1 ; col  <=  M.nu; col ++ )
        num[col] 
=   0 ;

    
for (t  =   1 ; t  <=  M.tu; t ++ )
        num[M.data[t].j]
++ ;

    cpot[
1 =   1 ;
    
for (col  =   2 ; col  <=  M.nu; col ++ )
        cpot[col] 
=  cpot[col  - 1  ]  +  num[col  - 1 ];

    
int  p, q;
    
for (p  =   1 ; p  <=  M.tu; p ++ )
    {
        col 
=  M.data[p].j;
        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]
++ ;
    }

    delete[] num;
    delete[] cpot;
    
return   1 ;
}
int  TransposeSMatrix(TSMatrix M, TSMatrix  & T)
{
    T.mu 
=  M.nu;
    T.nu 
=  M.mu;
    T.tu 
=  M.tu;

    
if (T.tu  ==   0 )
        
return   0 ;

    
int  q  =   1 ;
    
int  col;
    
int  p;
    
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   1 ;
}

void  main()
{
    TSMatrix m, t;
    CreateSMatrix(m);
    PrintSMatrix(m);
    printf(
" \n " );
    FastTransposeSMatrix(m, t);
    PrintSMatrix(t);

}

 

程序从文本文件中读取三元组数据.例如文本文件smatrix.txt中的内容是:

6,7,8
1,2,12
1,3,9
3,1,-3
3,6,14
4,3,24
5,2,18
6,1,15
6,4,-7

转载于:https://www.cnblogs.com/froster/archive/2009/06/12/1501952.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值