nmatrix.h

来自「数据结构c++语言描述 Borland C++实现」· C头文件 代码 · 共 53 行

H
53
字号


// N-matrix

#ifndef NMatrix_
#define NMatrix_

#include<stdlib.h>
#include<iostream.h>
#include "xcept.h"


template<class T>
class NMatrix {
   public:
      NMatrix(int size = 10)
         {n = size; t = new T [3*n-2];}
      ~NMatrix() {delete [] t;}
      NMatrix<T>& Store(const T& x, int i, int j);
      T Retrieve(int i, int j);
   private:
      int n;    // dimension
      T *t;     // 1D array
};

template<class T>
NMatrix<T>& NMatrix<T>::
     Store(const T& x, int i, int j)
{// Store x as N(i,j).
   if ( i < 1 || j < 1 || i > n || j > n)
       throw OutOfBounds();
   if (j == 1) t[i-1] = x;          // first column
   else if (j == n) t[n+i-1] = x;   // last column
        else if (i == j) t[2*n+i-2] = x;
                         // rest of diagonal
             else if (x != 0) throw MustBeZero();
   return *this;
}
  
template <class T>
T NMatrix<T>::Retrieve(int i, int j)
{// Retrieve C(i,j)
   if ( i < 1 || j < 1 || i > n || j > n)
       throw OutOfBounds();
   if (j == 1) return t[i-1];   // first column
   if (j == n) return t[n+i-1]; // last column
   if (i == j) return t[2*n+i-2]; 
                  // rest of diagonal
   return 0; // outside the N
}

#endif

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?