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

2020-01-06 13:36:10王旭
  • }  } 
  • /* Matrix transpose function */  int MATTransport(const MAT* pMat1, MAT* pMat2) 
  • {  MATAlloc(pMat2, pMat1->nCol, pMat1->nRow); 
  • for(int i=0; i<pMat1->nRow; ++i)  { 
  • for(int j=0; j<pMat1->nCol; ++j)  { 
  • *(pMat2->pData + j * pMat1->nRow + i) = *(pMat1->pData + i * pMat1->nCol + j);  } 
  • }  return OK; 
  • }  /* 
  • bool Check_digit(char *kk)  { 
  • int a = strlen(kk);  for(int i = 0; i<a; ++i) 
  • {  if( ( (int) (*(kk + i) ) > 48) && ( (int) (*(kk + i) ) < 57 || (int) (*(kk + i) ) == 32) ) 
  • {  return 1; 
  • }  } 
  • return 0;  } 
  •   */ 
  •   /* Matrix initialization */ 
  • void MATinit(MAT *pMat)  { 
  • bool kos=1;  int nRow = 0, nCol = 0; 
  • printf("Please input the number of rows: ");  scanf_s("%d",&nRow); 
  • putchar('n');  printf("Please input the number of columns: "); 
  • scanf_s("%d",&nCol);  putchar('n'); 
  • printf("Please input %dX%d Matrix:n",nRow,nCol);  kos=MATAlloc(pMat,nRow,nCol); 
  • for(int i=0; i<nRow; ++i)  { 
  • for(int j=0; j<nCol; ++j)  { 
  • scanf("%d", pMat->pData + i*nCol + j);  } 
  • }  } 
  • /*char arr[100][100]={0};  for(int i=0; i<nRow; ++i) 
  • {  for(int j=0; j<nCol; ++j) 
  • {  scanf("%c", &arr[i][j]); 
  • kos = Check_digit(&arr[i][j]);  } 
  • }  //ks= atoi(arr[0]);