📄 ch4p1_matrixmult.cpp
字号:
/*
#############################################################################
Ch4p1_MatrixMult: Shows how to multiply two matrices programmatically.
#############################################################################
*/
// Include Directives ///////////////////////////////////////////////////////
#include <iostream.h>
#include <memory.h>
#include <ctype.h>
// Local Constants, namespace usage /////////////////////////////////////////
const int MAXDIM = 100;
// Local Variables / functions //////////////////////////////////////////////
//===========================================================================
//
// class: CMatrix. Represents a matrix of arbitrary width and height.
//
//===========================================================================
class CMatrix
{
public:
CMatrix() { m_iWidth = 0; m_iHeight = 0; memset(&m_Elements, 0, MAXDIM*MAXDIM); }
int GetWidth(void) const { return(m_iWidth); }
int GetHeight(void) const { return(m_iHeight); }
void VerifyDims(int x, int y) const {
if (x < 0 || x > m_iWidth) { throw("CMatrix::GetAt(): x out of bounds!\n"); }
if (y < 0 || y > m_iHeight) { throw("CMatrix::GetAt(): y out of bounds!\n"); }
}
float GetAt(int x, int y) const {
VerifyDims(x,y);
return(m_Elements[x][y]);
}
void SetAt(int x, int y, float fValue) {
VerifyDims(x,y);
m_Elements[x][y] = fValue;
}
void SetSize(int iWidth, int iHeight) {
m_iWidth = iWidth;
m_iHeight = iHeight;
}
void Multiply(const CMatrix &A, const CMatrix &B);
private:
int m_iWidth, m_iHeight;
float m_Elements[MAXDIM][MAXDIM];
};
/****************************************************************************
Multiply: The money function. This multiplies two matrices together.
****************************************************************************/
void CMatrix::Multiply(const CMatrix &A, const CMatrix &B)
{
// the multiplied matrix will have dimensions of A's width and B's height
m_iWidth = A.GetWidth();
m_iHeight = B.GetHeight();
// for each row in the new matrix
for (int x = 0; x < m_iWidth; x++) {
// for each column in the new matrix
for (int y = 0; y < m_iHeight; y++) {
// our "answer" for this element is initially 0
double f = 0;
// loop through the rows and columns (the sigma of the equation)
for (int k=0; k < B.GetWidth(); k++) {
// add to our answer the product of A's row and B's column
f += A.GetAt(k,x) * B.GetAt(y,k);
}
// our answer for this element is the sum.
SetAt(x,y,(float)f);
}
}
}
/****************************************************************************
GetMatrix: Prompts the user for a matrix. It first asks for the size, then
it asks for each element of the matrix.
****************************************************************************/
void GetMatrix(CMatrix &matrix, const char *strName)
{
int width=0, height=0;
cout << "Enter the width of matrix " << strName << ": ";
cin >> width;
cout << "Enter the height of matrix " << strName << ": ";
cin >> height;
if (width > MAXDIM || width < 0) { throw("Invalid maxtrix width!"); }
if (height > MAXDIM || height < 0) { throw("Invalid maxtrix height!"); }
matrix.SetSize(width, height);
// loop through each element and prompt for its value
for (int iLoopX=0; iLoopX < width; iLoopX++) {
for (int iLoopY=0; iLoopY < height; iLoopY++) {
float fElement = 0;
cout << "Enter the element at (" << iLoopX << "," << iLoopY << "): ";
cin >> fElement;
matrix.SetAt(iLoopX,iLoopY, fElement);
}
}
}
/****************************************************************************
PrintMatrix: Outputs a matrix to the screen. (tab delimited)
****************************************************************************/
void PrintMatrix(const CMatrix &matrix)
{
for (int y = 0; y < matrix.GetHeight(); y++) {
for (int x = 0; x < matrix.GetWidth(); x++) {
cout << matrix.GetAt(x,y) << "\t";
}
cout << endl;
}
}
/****************************************************************************
main: program begins here.
****************************************************************************/
int main(int argc, char* argv[])
{
CMatrix mtxA;
CMatrix mtxB;
CMatrix mtxC;
char ynAnother = 'Y';
cout << "Matrix Multiplier:" << endl;
cout << "==================" << endl;
while (toupper(ynAnother) == 'Y') {
try {
// get the first matrix from the user
GetMatrix(mtxA, "A");
// get the second matrix from the user
GetMatrix(mtxB, "B");
// multiply A * B into C
mtxC.Multiply(mtxA, mtxB);
// output C matrix to screen
cout << "Result matrix:" << endl;
PrintMatrix(mtxC);
cout << endl;
}
catch(char *msg) {
cout << "ERROR: " << msg;
}
catch(...) {
cout << "ERROR: **UNKNOWN**";
}
// ask the user if they want to do another one.
cout << "Do another one? (Y/N): ";
cin >> ynAnother;
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -