⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 matrix.cpp

📁 一个实用的路径搜索图象处理程序源代码.对某些从事人工智能的算法应用有参考价值.
💻 CPP
字号:
// Matrix.cpp: implementation of the Matrix class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Matrix.h"
#include<math.h>
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

Matrix::~Matrix()
{
 if ( element != NULL)
	 delete []element;
}

Matrix::Matrix(int r, int c)
{
   rows = r; cols = c;
   element = new int [r * c];
    for ( int i = 0 ; i < r*c ; i++)
	   element[i] = 0;
}

int& Matrix::operator ()(int i, int j) const
{  
   return element[(i - 1) * cols + j - 1];


}


/*Matrix& Matrix::operator=(const Matrix& m)
{
 // Assignment. (*this) = m.
   if (this != &m) 
   {// do not copy to self
            delete [] element;
            rows = m.rows; cols = m.cols;
            element = new float [rows * cols];
            // copy each element
            for (int i = 0; i < rows * cols; i++)
               element[i] = m.element[i];
    }
   return *this;
}

Matrix Matrix::operator+(const Matrix& m) const
{
	// Return w = (*this) + m.
   if (rows != m.rows || cols != m.cols)
   {
	   AfxMessageBox("矩阵相加时出错,不匹配");
   }

   // create result matrix w
   Matrix  w(rows, cols);
   for (int i = 0; i < rows * cols; i++)
      w.element[i] = element[i] + m.element[i];

   return w;
}


Matrix Matrix::operator-(const Matrix& m) const
{
	// Return w = (*this) + m.
   if (rows != m.rows || cols != m.cols)
   {
	   AfxMessageBox("矩阵相加时出错,不匹配");
   }

   // create result matrix w
   Matrix  w(rows, cols);
   for (int i = 0; i < rows * cols; i++)
      w.element[i] = element[i] - m.element[i];

   return w;
}


Matrix Matrix::operator*(const Matrix& m) const
{
	// Matrix multiply.  Return w = (*this) * m.
   if (cols != m.rows) 
   {
	   AfxMessageBox("矩阵相乘时出错,行列不匹配");
   }

   Matrix  w(rows, m.cols);  // result matrix

   // define cursors for *this, m, and w
   // and initialize to location of (1,1)
   int ct = 0, cm = 0, cw = 0;

   // compute w(i,j) for all i and j
   for (int i = 1; i <= rows; i++) {

      // compute row i of result
      for (int j = 1; j <= m.cols; j++) {

         // compute first term of w(i,j)
         float sum =  element[ct] * m.element[cm];

         // add in remaining terms
         for (int k = 2; k <= cols; k++) {
            ct++;  // next term in row i of *this
            cm += m.cols;  // next in column j of m
            sum += element[ct] * m.element[cm];
            }
         w.element[cw++] = sum;  // save w(i,j)

         // reset to start of row and next column
         ct -= cols - 1;
         cm = j;
         }

      // reset to start of next row and first column
      ct += cols;
      cm = 0;
      }

   return w;
}
*/
Matrix::Matrix()
{
	rows = 1; cols = 1;
   try{
   element = new int [rows * cols];
    for ( int i = 0 ; i < rows*cols ; i++)
	   element[i] = 0;
  

   }
   catch(...)
   {
	   AfxMessageBox("dfD");
   }

}

void Matrix::ReSetRowCol(int r, int c)
{
   if (element != NULL)
	   delete []element;
   rows = r; cols = c;
   element = new int [r * c];
    for ( int i = 0 ; i < r*c ; i++)
	   element[i] = 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -