《数据结构、算法与应用C++语言描述》使用C++语言实现二维数组三对角矩阵

《数据结构、算法与应用C++语言描述》使用C++语言实现二维数组三对角矩阵

三对角矩阵定义

如下图所示:

代码实现

_10tridiagonalMatrix.h

模板类

cpp 复制代码
/*
Project name :			allAlgorithmsTest
Last modified Date:		2022年8月13日17点38分
Last Version:			V1.0
Descriptions:			数组存储的三对角矩阵类头文件
*/
#pragma once
#ifndef _TRIDIAGONAL_H_
#define _TRIDIAGONAL_H_
#include "_1myExceptions.h"
using namespace std;

void tridiagonalMatrixTest();//测试函数

template<class T>
class tridiagonalMatrix 
{
   public:
      tridiagonalMatrix(int theN = 10);
      ~tridiagonalMatrix() {delete [] element;}
      T get(int, int) const;
      void set(int, int, const T&);
   private:
      int n;       // matrix dimension
      T *element;  // 1D array for tridiagonal
};

template<class T>
tridiagonalMatrix<T>::tridiagonalMatrix(int theN)
{// Constructor.
   // validate theN
   if (theN < 1)
       throw illegalParameterValue("Matrix size must be > 0");

   n = theN;
   element = new T [3 * n - 2];
}

template <class T>
T tridiagonalMatrix<T>::get(int i, int j) const
{// Return (i,j)th element of matrix.

   // validate i and j
   if ( i < 1 || j < 1 || i > n || j > n)
       throw matrixIndexOutOfBounds();
 
   // determine lement to return
   switch (i - j) 
   {
      case 1: // lower diagonal
              return element[i - 2];
      case 0: // main diagonal
              return element[n + i - 2];
      case -1: // upper diagonal
              return element[2 * n + i - 2];
      default: return 0;
   }
}

template<class T>
void tridiagonalMatrix<T>::set(int i, int j, const T& newValue)
{// Store newValue as (i,j)th element

   // validate i and j
   if ( i < 1 || j < 1 || i > n || j > n)
      throw matrixIndexOutOfBounds();

   switch (i - j) 
   {
      case 1: // lower diagonal
         element[i - 2] = newValue; break;
      case 0: // main diagonal
         element[n + i - 2] = newValue; break;
      case -1: // upper diagonal
         element[2 * n + i - 2] = newValue; break;
      default: if (newValue != 0)
                  throw illegalParameterValue
                        ("non-tridiagonal elements must be zero");
   }
}

#endif

_10tridiagonalMatrix.cpp

cpp 复制代码
/*
Project name :			allAlgorithmsTest
Last modified Date:		2022年8月13日17点38分
Last Version:			V1.0
Descriptions:			测试_10tridiagonalMatrix.h头文件中的所有函数(三对角矩阵)
*/
#include <iostream>
#include "_10tridiagonalMatrix.h"

using namespace std;
void tridiagonalMatrixTest()
{
	cout << endl << "******************************tridiagonalMatrixTest()函数开始*********************************" << endl;
	tridiagonalMatrix<int> x(20);
	x.set(1, 1, 22);
	x.set(5, 5, 44);
	x.set(8, 5, 0);
	x.set(7, 8, 55);
	cout << x.get(7, 8) << endl;
	cout << x.get(5, 5) << endl;
	cout << x.get(1, 1) << endl;
	cout << x.get(10, 1) << endl;
	cout << x.get(1, 5) << endl;
	cout << "*****************************tridiagonalMatrixTest()函数结束**********************************" << endl;   
}

_1main.cpp

主函数

cpp 复制代码
/*
Project name :			allAlgorithmsTest
Last modified Date:		2022年8月13日17点38分
Last Version:			V1.0
Descriptions:			main()函数,控制运行所有的测试函数
*/
#include <iostream>
#include "_10tridiagonalMatrix.h"

int main()
{
	tridiagonalMatrixTest();
	
	return 0;
}

_1myExceptions.h

异常类汇总

cpp 复制代码
/*
Project name :			allAlgorithmsTest
Last modified Date:		2022年8月13日17点38分
Last Version:			V1.0
Descriptions:			综合各种异常
*/
#pragma once
#ifndef _MYEXCEPTIONS_H_
#define _MYEXCEPTIONS_H_
#include <string>
#include<iostream>

using namespace std;

// illegal parameter value
class illegalParameterValue 
{
   public:
      illegalParameterValue(string theMessage = "Illegal parameter value")
            {message = theMessage;}
      void outputMessage() {cout << message << endl;}
   private:
      string message;
};

// illegal input data
class illegalInputData 
{
   public:
      illegalInputData(string theMessage = "Illegal data input")
            {message = theMessage;}
      void outputMessage() {cout << message << endl;}
   private:
      string message;
};

// illegal index
class illegalIndex 
{
   public:
      illegalIndex(string theMessage = "Illegal index")
            {message = theMessage;}
      void outputMessage() {cout << message << endl;}
   private:
      string message;
};

// matrix index out of bounds
class matrixIndexOutOfBounds 
{
   public:
      matrixIndexOutOfBounds
            (string theMessage = "Matrix index out of bounds")
            {message = theMessage;}
      void outputMessage() {cout << message << endl;}
   private:
      string message;
};

// matrix size mismatch
class matrixSizeMismatch 
{
   public:
      matrixSizeMismatch(string theMessage = 
                   "The size of the two matrics doesn't match")
            {message = theMessage;}
      void outputMessage() {cout << message << endl;}
   private:
      string message;
};

// stack is empty
class stackEmpty
{
   public:
      stackEmpty(string theMessage = 
                   "Invalid operation on empty stack")
            {message = theMessage;}
      void outputMessage() {cout << message << endl;}
   private:
      string message;
};

// queue is empty
class queueEmpty
{
   public:
      queueEmpty(string theMessage = 
                   "Invalid operation on empty queue")
            {message = theMessage;}
      void outputMessage() {cout << message << endl;}
   private:
      string message;
};

// hash table is full
class hashTableFull
{
   public:
      hashTableFull(string theMessage = 
                   "The hash table is full")
            {message = theMessage;}
      void outputMessage() {cout << message << endl;}
   private:
      string message;
};

// edge weight undefined
class undefinedEdgeWeight
{
   public:
      undefinedEdgeWeight(string theMessage = 
                   "No edge weights defined")
            {message = theMessage;}
      void outputMessage() {cout << message << endl;}
   private:
      string message;
};

// method undefined
class undefinedMethod
{
   public:
      undefinedMethod(string theMessage = 
                   "This method is undefined")
            {message = theMessage;}
      void outputMessage() {cout << message << endl;}
   private:
      string message;
};
#endif
相关推荐
凤凰院凶涛QAQ3 分钟前
《Java版数据结构 & 集合类剖析》集合框架的封装设计与顺序表:“从 Iterable 到 ArrayList:集合框架的‘职业树“
java·开发语言·数据结构
大菜菜小个子3 分钟前
偏特化(Partial Specialization)理解
c++
吴可可12315 分钟前
Win7上开发CAD2004自定义实体全解析
c++·算法
YXXY31317 分钟前
二叉树中的深搜算法介绍
算法
zz345729811319 分钟前
C语言中字符串常量存储位置
c语言·开发语言·算法·青少年编程
noipp20 分钟前
推荐题目:洛谷 P16510 [GKS 2015 #C] gRanks
java·c语言·开发语言·c++·python·算法
程序喵大人20 分钟前
从内存/汇编角度看C与C++:指针、引用、对象的底层差异
c语言·汇编·c++·指针·引用·对象
菜菜的顾清寒29 分钟前
力扣HOT100(50)动态规划-零钱兑换
算法·leetcode·动态规划
周末也要写八哥32 分钟前
三分钟读懂:如何解决做题数量不足的问题?
算法
晚风吹红霞33 分钟前
C++ vector 深度剖析:从入门到模拟实现,避开所有坑
开发语言·c++