?? matrixexample.cpp
字號:
//MatrixExample.cpp 矩陣類中各種方法的應用示例
//#include <fstream> //文件流頭文件
#include <iostream> //輸入輸出流
#include "Matrix.h" //矩陣類頭文件
#include <complex> //復數模板類頭文件
using namespace std; //名字空間
void main() // 定義控制臺應用程序的入口點
{
//使用構造函數一定義兩矩陣對象
matrix<int> matS(2, 1); //定義整型2*1矩陣matS
matrix<int> matR(1, 2); //定義整型1*2矩陣matR
//數組元素賦值
matS(0, 0) = 1;
matS(1, 0) = 2;
matR(0, 0) = 3;
matR(0, 1) = 4;
//輸出兩矩陣
cout << endl << "matS : " << endl;
MatrixLinePrint(matS); //按行輸出矩陣matS
cout << endl << "matR : " << endl;
MatrixLinePrint(matR); //按行輸出矩陣matR
const double dma[4][4] = { { 1.2, 2.6, 3.7, 4.8 },
{ 5.3, 6.0, 7.0, 8.0 },
{ 9.4, 10.0, 11.6, 12.0 },
{ 3.5, 14.0, 15.0, 16.8 }};
const double dmb[4][4] = { { 3.0, -3.0, -2.0, 4.0 },
{ 5.0, -5.0, 1.0, 8.0 },
{ 1.0, 8.0, 5.0, -7.0 },
{ 5.0, -1.0, -3.0, -1.0 } };
//使用構造函數二定義兩矩陣對象matA, matB
const matrix<double> matA(&dma[0][0], 4, 4);
const matrix<double> matB(&dmb[0][0], 4, 4);
//使用構造函數三定義兩矩陣對象matC
matrix<double> matC(matA);
//輸出兩矩陣matA, matb
cout << endl << "matA : " << endl;
MatrixLinePrint(matA); //按行輸出矩陣matA
cout << endl << "matB : " << endl;
MatrixLinePrint(matB); //按行輸出矩陣matB
cout << endl << "matC : " << endl;
MatrixLinePrint(matC); //按行輸出矩陣matC
cout << endl << "Row of matA : " << matA.GetRowNum(); //matA行數
cout << "\t Column of matA : " << matA.GetRowNum() << endl; //matA列數
matC += matA; //矩陣自加矩陣
cout << endl << "matC += matA : " << endl;
MatrixLinePrint(matC); //按行輸出矩陣matC
matC *= matA; //矩陣自乘矩陣
cout << endl << "matC *= matA : " << endl;
MatrixLinePrint(matC); //按行輸出矩陣matC
matC -= 12.3; //矩陣自-數
cout << endl << "matC -= 12.3 : " << endl;
MatrixLinePrint(matC); //按行輸出矩陣matC
matC = -matC; //矩陣賦-號
cout << endl << "matC = -matC : " << endl;
MatrixLinePrint(matC); //按行輸出矩陣matC
matC = matA * matB; //矩陣乘矩陣
cout << endl << "matC = matA * matB : " << endl;
MatrixLinePrint(matC); //按行輸出矩陣matC
matC = matC / 2.0; //矩陣除以數
cout << endl << "matC = matC / 2.0 : " << endl;
MatrixLinePrint(matC); //按行輸出矩陣matC
matC = 0.5 * matC; //數乘以矩陣
cout << endl << "matC = 0.5 * matC : " << endl;
MatrixLinePrint(matC); //按行輸出矩陣matC
matC = matA + matC; //矩陣加矩陣
cout << endl << "matC = matA + matC : " << endl;
MatrixLinePrint(matC); //按行輸出矩陣matC
//比較兩矩陣是否相同不相同
cout << endl << "if (matA == matC) : " << (matA == matC) << endl; //是否matA與matC相同
cout << endl << "if (matA != matB) : " << (matA != matB) << endl; //是否matA與matB不相同
matrix< complex<float> > clhs(2, 1); //定義復數浮點型2*1數組clhs
matrix< complex<float> > crhs(1, 2); //定義復數浮點型1*2數組crhs
//給復數型矩陣元素賦
clhs(0, 0) = complex<float> (1, 2); //給復數型矩陣元素賦值
clhs(1, 0) = complex<float> (3, 4);
crhs(0, 0) = complex<float> (5, 6);
crhs(0, 1) = complex<float> (7, 8);
cout << endl << "ComplexMatrix clhs :" << endl;
MatrixLinePrint(clhs); //輸出復矩陣clhs
cout << endl << "ComplexMatrix crhs :" << endl;
MatrixLinePrint(crhs); //輸出復矩陣crhs
matrix< complex<float> > cmm(clhs * crhs); //cmm=clhs*crhs
cout << endl << "ComplexMatrix * ComplexMatrix :" << endl;
MatrixLinePrint(cmm); //輸出復矩陣cmm=clhs*crhs
matrix< complex<float> > cmp(cmm); //生成cmp,由cmm初始化
cmp += cmm; //cmp += cmm
cout << endl << "ComplexMatrix * ComplexMatrix :" << endl;
MatrixLinePrint(cmp); //輸出復矩陣cmp+=cmm
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -