程式設計練習 矩陣類

2021-07-31 20:27:03 字數 3022 閱讀 1782

自己嘗試以模板的形式寫了乙個基礎矩陣類,提供了矩陣運算的基本方法,包括加減乘除、轉置、求行列式、求逆,後續還會進行陸續的補充,下面添上原始碼,可能存在一些不足之處,希望能得到大家的建議。

//標頭檔案matrix.h

/**/

#pragma once

#include "stdafx.h"

#define chudeng 0 //初等變換

#define bansui 1//伴隨陣

#define rec 0//遞迴求行列式 速度太慢

#define tria 1//上三角求行列式

template

class matrix//:private mymat

;

cpp檔案

#include "stdafx.h"

#include "matrix.h"

//建構函式

template

matrix::matrix()

template

matrix::matrix(const

int &rows, const

int &cols)

//用二維陣列來構造矩陣

template

matrix::matrix(t *p, int rows, int cols)

}template

matrix::matrix(const matrix &m)

}//矩陣合併建構函式

template

matrix::matrix(const matrix&m1, const matrix&m2)

}//輸出顯示整個矩陣

template

void matrix::showmatrix()

}//過載賦值運算子

template

matrix& matrix::operator=(const matrix& m)

return *this;

}template

t* matrix::operator(const

int &i)

//0矩陣

template

void matrix::zeros()

//單位陣

template

bool matrix::unit()

//交換矩陣的兩行

template

void matrix::exchangerow(const

int &i1, const

int &i2)

//矩陣轉置

template

matrixmatrix::trans()

//求矩陣行列式

template

double matrix::det(int method)

if (method == rec)

if (i % 2 == 0) q = 1; else q = -1;

if (data[0][i] == 0) continue;

else

result += data[0][i] * q*m.det();//遞迴求行列式

}return result;

}if (method == tria)

}result = 0;

return result;

}outer:

for (int u = i+1; u < rows; u++)}}

result = 1;

for (int i = 0; i < rows; i++)

return result;

}}//矩陣相乘

template

matrixmatrix::operator*(matrix& m)

return result;

}//矩陣乘以乙個數

template

matrixoperator*(matrix& m, s num)

return result;

}//矩陣乘以乙個數

template

matrixoperator*(s num,matrix& m)

//矩陣除以乙個數

template

matrixoperator/(matrix& m, s num)

return result;

}//矩陣相加

template

matrixmatrix::operator+(matrix& m)

return result;

}//矩陣相減

template

matrixmatrix::operator-(matrix& m)

return result;

}//矩陣求逆

template

matrixmatrix::inverse(int method)

}exit(0);//矩陣行列式為0,程式退出

}outer:

for (int j = i+1; j < expand_matrix.cols; j++)

expand_matrix[i][i] = 1;

//把其它行在該列的值變為0

for (int t = 0; t < rows; t++)

expand_matrix[t][i] = 0;}}

for (int i = 0; i < rows;i++)

for (int j = 0; j < cols; j++)

return result;

}//伴隨矩陣法求逆

if (method == bansui)

return result;

}}//求i,j處的代數余子式

template

double matrix::cofactor(int i, int j)

return m.det()*p;

}//析構函式

template

matrix::~matrix()

物件導向程式設計 類的練習

c 類的例項練習。personal music manage system author weiqing jin version 2011 12 13 19 31 13 mumu.include include include include include using namespace std ...

程式設計練習 數字矩陣路徑數字和最小

給定乙個數字矩陣,試找出一條從左上角到右下角的一條路徑,要求路徑上的數字和最小。思路一 使用動態規劃思想,用當前路徑最小數字和替換原來位置上的資料,直至到達右下角 filename author zhang peng date version description include using na...

程式設計練習題 矩陣中的路徑

請設計乙個函式,用來判斷在乙個矩陣中是否存在一條包含某字串所有字元的路徑。路徑可以從矩陣中的任意乙個格仔開始,每一步可以在矩陣中向左,向右,向上,向下移動乙個格仔。如果一條路徑經過了矩陣中的某乙個格仔,則該路徑不能再進入該格仔。例如 ab cesf csad ee left begin a b c ...