坚持到底就是胜利

用心去做好这件事情

统计

留言簿(1)

阅读排行榜

评论排行榜

matrix class

  1 //   2006.06.13  MCRC  
  2 #include < iostream >
  3 #include < vector >
  4 #include < cstdlib >
  5 using   namespace  std;
  6 template < class  T >
  7
  8 class  matrix {
  9 public :
 10     matrix( int  row, int  col);
 11      void  set_matdata(T * data);
 12      ~ matrix();
 13     T  operator ()( int  row, int  col)  const ;
 14     T  operator ()( int  rwo, int  col);
 15     matrix &   operator + ( const  matrix  & t);
 16     matrix &   operator - ( const  matrix  & t);
 17     matrix &   operator * ( const  matrix  & t);
 18     matrix &   operator = ( const  matrix  & t);
 19     matrix( const  matrix  & t);
 20      void  display();
 21
 22 private :
 23
 24  T  ** arr ;
 25   int  _row ;
 26   int  _clo ;
 27 }
;
 28
 29 template < class  T >
 30 matrix < T > ::matrix( int  row, int  col)
 31 {
 32    _row  =  row;
 33    _clo  =  col;
 34
 35    arr  =   new  T * [_row];
 36     for  ( int  i  =   0  ; i  <  _row; i ++ )
 37        arr[i]  = new  T[_clo];
 38 }

 39
 40
 41
 42 template < class  T >
 43 void  matrix < T > ::set_matdata(T  * data)
 44 {
 45          for  ( int  i  =   0 ; i  <  _row ;i  ++ )
 46          for  (  int  j  =   0  ;j  <  _clo ;j  ++ )
 47             arr[i][j]  =    * (data  +  _clo * i + j);
 48 }

 49
 50 template < class  T >
 51 T matrix < T > :: operator  ()( int  row, int  col)  const
 52 {
 53      if  (row  <  _row  &&  col  <  _clo)
 54        return  ( arr[row][col]);
 55       else
 56          exit( 0 );
 57
 58 }

 59 template < class  T >
 60 T matrix < T > :: operator  ()( int  row, int  col)
 61 {
 62      if  (row  <  _row  &&  col  <  _clo)
 63        return  ( arr[row][col]);
 64       else
 65          exit( 0 );
 66
 67 }

 68
 69 //  matrix add
 70
 71 template < class  T >
 72 matrix < T >&  matrix < T > :: operator   + ( const  matrix  & p)
 73 {
 74      for  ( int  i  =   0 ; i  <  _row ;i  ++ )
 75          for  (  int  j  =   0  ;j  <  _clo ;j  ++ )
 76             arr[i][j]  +=  p.arr[i][j];
 77          return   * this ;
 78 }

 79
 80 //  matrix sub
 81 template < class  T >
 82 matrix < T >&  matrix < T > :: operator   - ( const  matrix  & p)
 83 {
 84      for  ( int  i  =   0 ; i  <  _row ;i  ++ )
 85          for  (  int  j  =   0  ;j  <  _clo ;j  ++ )
 86             arr[i][j]  -=  p.arr[i][j];
 87          return   * this ;
 88 }

 89
 90 //  matrix multi
 91 template < class  T >
 92 matrix < T >&  matrix < T > :: operator   * ( const  matrix  & p)
 93 {
 94      T  ** arr_temp  =   new  T * [_row];
 95       for  (  int  ii  =   0  ;ii  <  _clo ;ii  ++ )
 96          arr_temp[ii]  =   new  T[_clo];
 97       for  ( ii  = 0  ;ii  <  _row ;ii  ++  )
 98           for  (  int  jj  =   0  ;jj  <  _clo; jj ++ )
 99              arr_temp[ii][jj]  =   0 ;
100
101      for  ( int   k  =   0  ; k  <  _row ; k ++ )
102      {
103          for  ( int  i  =   0  ; i  <  _row ; i ++ )
104              for  (  int  j  =   0  ;j  <  _clo ; j ++ )
105                 arr_temp[k][i]  +=  arr[k][j]  * p.arr[j][i];
106     }

107      for  ( int  i  =   0  ; i  <  _row ;i  ++ )
108          for  ( int  j  =   0  ; j  <  _clo ; j  ++ )
109             arr[i][j]  =  arr_temp[i][j];
110
111      return   * this ;
112 }

113
114 //  dispaly matrix
115 template < class  T >
116 void  matrix < T > ::display()
117 {
118     cout << " ================================== " << endl;
119      for  ( int  i  =   0 ; i  <  _row ;i  ++ )
120      {
121          for  (  int  j  =   0  ;j  <  _clo ;j  ++ )
122             cout << arr[i][j] << "    " ;
123         cout << endl;
124     }

125     cout << " ================================== " << endl << endl << endl;
126
127 }

128
129 template < class  T >
130 matrix < T >&  matrix < T > :: operator = ( const  matrix < T >   & rhs)
131 {
132      if  (  this   !=   & rhs )
133      {
134         _row  =  rhs._row;
135         _clo  =  rhs._clo;
136          for  (  int  i  =   0  ;i  <  _row;i  ++ )
137             delete [] arr[i];
138         delete[] arr;
139
140         arr  =   new  T * [_row];
141          for  (  int  j  =   0  ; j  <  _row ; j ++ )
142             arr[j]  =   new  T[_clo];
143
144          for  ( i  =   0  ; i  <  _row ; i  ++ )
145              for  ( j  =   0  ;j  <  _clo ;j  ++ )
146                 arr[i][j]  =  rhs.arr[i][j];
147     }

148      return   * this ;
149
150 }

151
152 template < class  T >
153 matrix < T > :: ~ matrix()
154 {
155
156      for  (  int  i  =   0 ; i  <  _row ; i ++  )
157         delete[] arr[i];
158
159     delete[] arr;
160
161     
162 }

163 template < class  T >
164 matrix < T > ::matrix < T > ( const  matrix < T >   & rhs)
165 {
166     _row  =  rhs._row;
167     _clo  =  rhs._clo;
168
169     arr  =   new  T * [_row];
170     
171      for int  i  =   0  ;i  <  _row ; i ++ )
172         arr[i]  =   new  T[_clo];
173      for  ( i  =   0  ; i  <  _row ; i ++ )
174          for  ( int  j  =   0  ; j  <  _clo ; j ++ )
175             arr[i][j]  =  rhs.arr[i][j];
176
177
178 }

179
180

posted on 2006-06-16 16:32 ailab 阅读(336) 评论(0)  编辑 收藏 引用 所属分类: EssentialC++


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理