学习二维动态数组指针做矩阵运算的方法

2020-01-06 13:36:10王旭
  • return OK;  } 
  •   /* Release the memory space and reset the matrix data function */ 
  • void MATFree(MAT* pMat)  { 
  • free(pMat->pData);  pMat->pData = NULL; 
  • pMat->nRow = 0;  pMat->nCol = 0; 
  • }   
  • /* Import of matrix function */  int MATAssign (MAT* pMat1, const MAT* pMat2) 
  • {  MATAlloc(pMat1, pMat2->nRow, pMat2->nCol); 
  • for(int i=0; i < pMat1->nRow; ++i)  { 
  • for(int j=0; j < pMat1->nCol; ++j)  { 
  • *(pMat1->pData + i * pMat1->nCol + j) = *(pMat2->pData + i * pMat1->nCol + j);  } 
  • }  return OK;  
  • }   
  • /* Matrix sum function */  int MATAdd(const MAT* pMat1, const MAT* pMat2, MAT* pMat3) 
  • {  MATAlloc(pMat3, pMat1->nRow, pMat1->nCol); 
  • if((pMat1->nRow == pMat2->nRow) && (pMat1->nCol == pMat2->nCol))  { 
  • for(int i=0; i<pMat1->nRow; ++i)  { 
  • for(int j=0; j<pMat1->nCol; ++j)  { 
  • *(pMat3->pData + i * pMat3->nCol + j) = *(pMat1->pData + i * pMat1->nCol + j) + *(pMat2->pData + i * pMat1->nCol + j);  } 
  • }  return OK;  
  • }  else 
  • {  printf("Not add!n"); 
  • return NG;  } 
  •   } 
  •   /* Matrix subtraction function */ 
  • int MATSub(const MAT* pMat1, const MAT* pMat2, MAT* pMat3)  { 
  • MATAlloc(pMat3, pMat1->nRow, pMat1->nCol);  if((pMat1->nRow == pMat2->nRow) && (pMat1->nCol == pMat2->nCol)) 
  • {  for(int i=0; i<pMat1->nRow; ++i)