《数据结构、算法与应用C++语言描述》使用C++语言实现二维数组对角矩阵
对角矩阵定义
如下图所示:
代码实现
_9diagonalMatrix.h
模板类
/*
Project name : allAlgorithmsTest
Last modified Date: 2022年8月13日17点38分
Last Version: V1.0
Descriptions: 数组存储的对角矩阵类头文件
*/
#pragma once
#ifndef _DIAGONALMATRIX_H_
#define _DIAGONALMATRIX_H_
#include "_1myExceptions.h"
using namespace std;
void diagonalMatrixTest();//测试代码
template<class T>
class diagonalMatrix
{
public:
diagonalMatrix(int theN = 10);
~diagonalMatrix() {delete [] element;}
T get(int, int) const;
void set(int, int, const T&);
private:
int n; // matrix dimension
T *element; // 1D array for diagonal elements
};
template<class T>
diagonalMatrix<T>::diagonalMatrix(int theN)
{// Constructor.
// validate theN
if (theN < 1)
throw illegalParameterValue("Matrix size must be > 0");
n = theN;
element = new T [n];
}
template <class T>
T diagonalMatrix<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();
if (i == j)
return element[i-1]; // diagonal element
else
return 0; // nondiagonal element
}
template<class T>
void diagonalMatrix<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();
if (i == j)
// save the diagonal value
element[i-1] = newValue;
else
// nondiagonal value, newValue must be zero
if (newValue != 0)
throw illegalParameterValue
("nondiagonal elements must be zero");
}
#endif
_9diagonalMatrix.cpp
/*
Project name : allAlgorithmsTest
Last modified Date: 2022年8月13日17点38分
Last Version: V1.0
Descriptions: 测试_9diagonalMatrix.h头文件中的所有函数(对角矩阵)
*/
#include <iostream>
#include "_9diagonalMatrix.h"
using namespace std;
void diagonalMatrixTest()
{
cout << endl << "*******************************diagonalMatrixTest()函数开始***********************************" << endl;
diagonalMatrix<int> x(20);
x.set(1,1,22);
x.set(5,5,44);
x.set(8,5,0);
cout << x.get(5,5) << endl;
cout << x.get(1,1) << endl;
cout << x.get(10,1) << endl;
cout << "*******************************diagonalMatrixTest()函数结束***********************************" << endl;
}
_1main.cpp
主函数
/*
Project name : allAlgorithmsTest
Last modified Date: 2022年8月13日17点38分
Last Version: V1.0
Descriptions: main()函数,控制运行所有的测试函数
*/
#include <iostream>
#include "_9diagonalMatrix.h"
int main()
{
diagonalMatrixTest();
return 0;
}
_1myExceptions.h
异常类汇总
/*
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