数组与矩阵(1)_矩阵相乘
2007-02-01 20:00
218 查看
矩阵相乘是矩阵运算中十分基本的概念。
在这里我用类的形式封装了矩阵的存储和表示。内部存储的方式是采用一维数组的形式。
另外引入了异常处理的机制。也算是对C++的基本概念进行了实际应用了。
//error class
class BadOccur
...{
public:
BadOccur(int nErrorNum)
...{
ErrorNum = nErrorNum;
}
void BadOccurPrint()
...{
cout << ErrorNum << " error occurs!" << endl;
}
private:
int ErrorNum;
};
//matrix class
class CMatrix
...{
public:
CMatrix(int Row = 0, int Col = 0)
...{
if ( Row < 0 || Col < 0)
...{
throw BadOccur(2);
}
else
...{
nRows = Row;
nCols = Col;
pValueArray = new int [Row * Col];
}
}
int operator()(int Row, int Col) const //get the element from the position you have given
...{
if ( Row < 1 || Row > nRows || Col < 1 || Col > nCols)
...{
throw BadOccur(1);//cause error 1
}
return pValueArray[(Row - 1) * nCols + Col - 1];
}
int& SetData(int i, int j)//abstract the position for you to set data
...{
return pValueArray[(i - 1)* nCols + j - 1];
}
int nRows;
int nCols;
int* pValueArray;
};
/**//*
* Function Name :MultiplyMatrix
* function :multiply two matrices
* detail :none
* author :weixiong
* time :2007-2-1
* return type :bool
* return description : true two matrices has multiplied
* false two matrices can't be multiplied
* function parameters :CMatrix& MatrixA multiply matrix A and B
* CMatrix& MatrixB
*
*/
bool MultiplyMatrix(CMatrix& MatrixA, CMatrix& MatrixB, CMatrix& ResultMatrix)
...{
if (MatrixA.nCols != MatrixB.nRows)
...{
return false;
}
else
...{
int Sum = 0;
for (int Row = 1; Row <= MatrixA.nRows; Row++)
...{
for (int Col = 1; Col <= MatrixB.nCols; Col++)
...{
for (int i = 1; i <= MatrixA.nCols; i++)
...{
Sum = Sum + MatrixA(Row, i) * MatrixB(i, Col);
}
ResultMatrix.pValueArray[(Row - 1) * MatrixB.nCols + Col - 1] = Sum;
}
}
return true;
}
}
int main()
...{
try
...{
//CMatrix ErrorMatrix(-1, 0);//example of cause error 2
int Row1 = 0;
int Col1 = 0;
cout << "Please enter the row and col for the first matrix." << endl;
cin >> Row1 >> Col1;
int Num = 1;
CMatrix MatrixA(Row1, Col1);
for (int i = 1; i <= Row1; i++)
...{
for (int j = 1; j <= Col1; j++)
...{
MatrixA.SetData(i, j) = Num++;
}
}
//int a = MatrixA(0, 2);//example of cause error 1
int Row2 = 0;
int Col2 = 0;
cout << "Please enter the row and col for the second matrix." << endl;
cin >> Row2 >> Col2;
Num = 1;
CMatrix MatrixB(Row2, Col2);
for (int i = 1; i <= Row2; i++)
...{
for (int j = 1; j <= Col2; j++)
...{
MatrixB.SetData(i, j) = Num++;
}
}
CMatrix ResultMatrix(Row1, Col2);
MultiplyMatrix(MatrixA, MatrixB, ResultMatrix);
getchar();
}
catch(BadOccur m)
...{
m.BadOccurPrint();
}
}
在这里我用类的形式封装了矩阵的存储和表示。内部存储的方式是采用一维数组的形式。
另外引入了异常处理的机制。也算是对C++的基本概念进行了实际应用了。
//error class
class BadOccur
...{
public:
BadOccur(int nErrorNum)
...{
ErrorNum = nErrorNum;
}
void BadOccurPrint()
...{
cout << ErrorNum << " error occurs!" << endl;
}
private:
int ErrorNum;
};
//matrix class
class CMatrix
...{
public:
CMatrix(int Row = 0, int Col = 0)
...{
if ( Row < 0 || Col < 0)
...{
throw BadOccur(2);
}
else
...{
nRows = Row;
nCols = Col;
pValueArray = new int [Row * Col];
}
}
int operator()(int Row, int Col) const //get the element from the position you have given
...{
if ( Row < 1 || Row > nRows || Col < 1 || Col > nCols)
...{
throw BadOccur(1);//cause error 1
}
return pValueArray[(Row - 1) * nCols + Col - 1];
}
int& SetData(int i, int j)//abstract the position for you to set data
...{
return pValueArray[(i - 1)* nCols + j - 1];
}
int nRows;
int nCols;
int* pValueArray;
};
/**//*
* Function Name :MultiplyMatrix
* function :multiply two matrices
* detail :none
* author :weixiong
* time :2007-2-1
* return type :bool
* return description : true two matrices has multiplied
* false two matrices can't be multiplied
* function parameters :CMatrix& MatrixA multiply matrix A and B
* CMatrix& MatrixB
*
*/
bool MultiplyMatrix(CMatrix& MatrixA, CMatrix& MatrixB, CMatrix& ResultMatrix)
...{
if (MatrixA.nCols != MatrixB.nRows)
...{
return false;
}
else
...{
int Sum = 0;
for (int Row = 1; Row <= MatrixA.nRows; Row++)
...{
for (int Col = 1; Col <= MatrixB.nCols; Col++)
...{
for (int i = 1; i <= MatrixA.nCols; i++)
...{
Sum = Sum + MatrixA(Row, i) * MatrixB(i, Col);
}
ResultMatrix.pValueArray[(Row - 1) * MatrixB.nCols + Col - 1] = Sum;
}
}
return true;
}
}
int main()
...{
try
...{
//CMatrix ErrorMatrix(-1, 0);//example of cause error 2
int Row1 = 0;
int Col1 = 0;
cout << "Please enter the row and col for the first matrix." << endl;
cin >> Row1 >> Col1;
int Num = 1;
CMatrix MatrixA(Row1, Col1);
for (int i = 1; i <= Row1; i++)
...{
for (int j = 1; j <= Col1; j++)
...{
MatrixA.SetData(i, j) = Num++;
}
}
//int a = MatrixA(0, 2);//example of cause error 1
int Row2 = 0;
int Col2 = 0;
cout << "Please enter the row and col for the second matrix." << endl;
cin >> Row2 >> Col2;
Num = 1;
CMatrix MatrixB(Row2, Col2);
for (int i = 1; i <= Row2; i++)
...{
for (int j = 1; j <= Col2; j++)
...{
MatrixB.SetData(i, j) = Num++;
}
}
CMatrix ResultMatrix(Row1, Col2);
MultiplyMatrix(MatrixA, MatrixB, ResultMatrix);
getchar();
}
catch(BadOccur m)
...{
m.BadOccurPrint();
}
}
相关文章推荐
- C语言动态数组在矩阵相乘中的应用
- C++中一些简单的数组算法(矩阵相乘) 第二篇
- 数组::矩阵相乘
- 两个数组各个数相加或相乘变成一个矩阵求第K大
- 【面试题】两个n*n的矩阵相乘--采用一位数组表示
- hdu4920 滚动数组的矩阵相乘
- 一维数组表示矩阵相乘
- Java基本功练习九(多维数组强化一[五子棋、九个正面和背面、矩阵相乘])
- C和指针之数组编程练习5 (矩阵相乘)
- 2维数组矩阵相乘
- 两个数组各个数相加或相乘变成一个矩阵求第K大
- c++中矩阵相乘(对象数组与对象指针的应用)
- 矩阵相乘 一维数组表示
- 伪数组代码&&矩阵相乘
- 数组与广义表_稀疏矩阵的压缩存储_三元组做存储结构_矩阵相乘
- R语言数组、矩阵、数据框与列表、控制流练习
- 数据结构实验之数组二:稀疏矩阵
- 稀疏矩阵相乘
- POJ 3318 两个大矩阵相乘是否与另一矩阵结果相等
- 矩阵相乘的快速算法(施特拉森-Strassen算法)