⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 nmatrix.h

📁 数据结构c++语言描述 Borland C++实现
💻 H
字号:


// 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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -