用C語言編程遞迴求行列式的值

來源:互聯網
上載者:User
/************************************************************************
功能: 用遞迴方法求行列式的值
作者: 黃志斌 於 廣西河池
在自學《高等數學(二)》時,做行列式習題所得結果有些與教材所附
答案不一致,為了驗證究竟答案的正確性,編寫了這個程式。
其實用MS Office中Excel也可以驗證,不過自己 DIY  感覺更舒服。
    希望這個程式能對您有所協助。
日     期     概  要
-----------------------------------------------------
2001.3.23     完成運算功能
2002.3.17     按照匈牙利式命名規則和
              林銳博士的《高品質C/C++編程指南》
              增強程式可讀性
2002.3.19     停止使用全域變數iRowOrCol,
              將原先的調試部分改為預先處理
*********************************************************************/
#include "stdio.h"
#include "mem.h"
#define IsDebug 0

/*int iRowOrCol;*/
void ReadMatrix(int *ipMatrixRead, int iRowOrCol4Read)
{
 int iRow4Read, iCol4Read;
 for (iRow4Read = 0; iRow4Read < iRowOrCol4Read; iRow4Read ++)
 {
  for (iCol4Read = 0; iCol4Read < iRowOrCol4Read; iCol4Read ++)
  {
   printf("matrix[%d, %d]=", iRow4Read, iCol4Read);
   scanf("%d", (ipMatrixRead + iRow4Read * iRowOrCol4Read + iCol4Read));
  }
 }
}

void WriteMatrix(int *ipMatrixWrite, int p_iRowOrCol)
{
 int iRow4Write, iCol4Write;
 putch('┌');

 for (iRow4Write = 0; iRow4Write < (p_iRowOrCol + 1) * 3 - 1; iRow4Write ++)
 {
  putch(' ');
 }
 printf("%c/n", '┐');

 for (iRow4Write = 0; iRow4Write < p_iRowOrCol; iRow4Write ++)
 {
  putch('│');
  for (iCol4Write = 0; iCol4Write < p_iRowOrCol; iCol4Write ++)
  {
   printf("%3d", *(ipMatrixWrite + iRow4Write * p_iRowOrCol + iCol4Write));
  }
  printf("%c/n", '│');
 }

 putch('└');
 for (iRow4Write = 0; iRow4Write < (p_iRowOrCol + 1) * 3 -1; iRow4Write ++)
 {
  putch(' ');
 }
 printf("%c/n", '┘');

 #if IsDebug == 1
  getch();
 #endif
}

int ComputMatrix(int *ipMatrixComput, int iColComput)
{
 int iRow4Comput, iCol4Comput, iRowTmp, *ipNewMatrix, CurrentRow, iResult;

 if (1 == iColComput)
 {
  iResult = *ipMatrixComput;
 }
 else
 {
  iResult = 0;
  for (CurrentRow = 0; CurrentRow < iColComput; CurrentRow ++)
  {
   if (*(ipMatrixComput + CurrentRow * iColCom! put))
   {
    if (ipNewMatrix = (int *)malloc((iColComput - 1) * (iColComput - 1) * sizeof(int)))
    {
     #if IsDebug == 1
      printf("/n%d", *(ipMatrixComput + CurrentRow * iColComput));
     #endif

     for (iRowTmp = 0, iRow4Comput = 0; iRow4Comput < iColComput; iRow4Comput ++)
     {
      if (iRow4Comput != CurrentRow)
      {
       for (iCol4Comput = 0; iCol4Comput < iColComput -1; iCol4Comput ++)
       {
        *(ipNewMatrix + iRowTmp*(iColComput - 1) + iCol4Comput) = *(ipMatrixComput + iRow4Comput * iColComput + iCol4Comput + 1);
       }
       iRowTmp ++;
      }
     }

     #if IsDebug == 1
      for (iRow4Comput = 0; iRow4Comput < iColComput - 1; iRow4Comput ++)
      {
       for (iCol4Comput = 0; iCol4Comput < iColComput - 1; iCol4Comput ++)
       {
        printf(" %d ", *(ipNewMatrix + iRow4Comput * (iColComput - 1) + iCol4Comput));
       }
      }
      printf("]/n************/n");
      WriteMatrix(ipNewMatrix, iColComput - 1);
     #endif

     iResult += *(ipMatrixComput + CurrentRow * iColComput) * ComputMatrix(ipNewMatrix, iColComput - 1) * ((CurrentRow + 1) % 2 ? 1 : -1);

     #if IsDebug == 1
      printf("result=%d/n", iResult);
      getch();
     #endif

     free(ipNewMatrix);
    }
   }
  }
 }
 return (iResult);
}

void main(void)
{
 int *ipMatrix, iRowOrCol;
 char chSure;
 while (1)
 {
  printf("/n/nEnter the row or col of matrix:");
  scanf("%d", &iRowOrCol);
  if (iRowOrCol > 0)
  {
   if (ipMatrix = (int *)malloc(iRowOrCol * iRowOrCol * sizeof(int)))
   {
    while (1)
    {
     ReadMatrix(ipMatrix, iRowOrCol);
     WriteMatrix(ipMatrix, iRowOrCol);
     printf("Are you sure(Y/N)?");
     chSure = getche();

     if (toupper(chSure) == 'Y')
     {
      break;
     }
     else
     {
      printf("/n/n");
     }
    }
    printf("/n/n= %d/n", ComputMatrix(ipMatrix, iRowOrCol));
    free(ipMatrix);
   }
   else
   {
    printf("Error:No enough memory!/n");
    break;
   }
  }
  else
  {
   printf("Invalid col value!/n/n");
  }
  printf("/nCompute another Matrix(Y/N)?");
  chSure = getche();
  if (toupper(chSure) != 'Y')
  {
   break;
  }
 }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.