/************************************************************************
功能:用递归方法求行列式的值
作者:黄志斌于广西河池
在自学《高等数学(二)》时,做行列式习题所得结果有些与教材所附
答案不一致,为了验证究竟答案的正确性,编写了这个程序。
其实用MSOffice中Excel也可以验证,不过自己DIY感觉更舒服。
希望这个程序能对您有所帮助。
日期概要
-----------------------------------------------------
2001.3.23完成运算功能
2002.3.17按照匈牙利式命名规则和
林锐博士的《高质量C/C++编程指南》
增强程序可读性
2002.3.19停止使用全局变量iRowOrCol,
将原先的调试部分改为预处理
*********************************************************************/
#include"stdio.h"
#include"mem.h"
#defineIsDebug0
/*intiRowOrCol;*/
voidReadMatrix(int*ipMatrixRead,intiRowOrCol4Read)
{
intiRow4Read,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));
}
}
}
voidWriteMatrix(int*ipMatrixWrite,intp_iRowOrCol)
{
intiRow4Write,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",'┘');
#ifIsDebug==1
getch();
#endif
}
intComputMatrix(int*ipMatrixComput,intiColComput)
{
intiRow4Comput,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)))
{
#ifIsDebug==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++;
}
}
#ifIsDebug==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);
#ifIsDebug==1
printf("result=%d/n",iResult);
getch();
#endif
free(ipNewMatrix);
}
}
}
}
return(iResult);
}
voidmain(void)
{
int*ipMatrix,iRowOrCol;
charchSure;
while(1)
{
printf("/n/nEntertheroworcolofmatrix:");
scanf("%d",&iRowOrCol);
if(iRowOrCol>0)
{
if(ipMatrix=(int*)malloc(iRowOrCol*iRowOrCol*sizeof(int)))
{
while(1)
{
ReadMatrix(ipMatrix,iRowOrCol);
WriteMatrix(ipMatrix,iRowOrCol);
printf("Areyousure(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:Noenoughmemory!/n");
break;
}
}
else
{
printf("Invalidcolvalue!/n/n");
}
printf("/nComputeanotherMatrix(Y/N)?");
chSure=getche();
if(toupper(chSure)!='Y')
{
break;
}
}
}
功能:用递归方法求行列式的值
作者:黄志斌于广西河池
在自学《高等数学(二)》时,做行列式习题所得结果有些与教材所附
答案不一致,为了验证究竟答案的正确性,编写了这个程序。
其实用MSOffice中Excel也可以验证,不过自己DIY感觉更舒服。
希望这个程序能对您有所帮助。
日期概要
-----------------------------------------------------
2001.3.23完成运算功能
2002.3.17按照匈牙利式命名规则和
林锐博士的《高质量C/C++编程指南》
增强程序可读性
2002.3.19停止使用全局变量iRowOrCol,
将原先的调试部分改为预处理
*********************************************************************/
#include"stdio.h"
#include"mem.h"
#defineIsDebug0
/*intiRowOrCol;*/
voidReadMatrix(int*ipMatrixRead,intiRowOrCol4Read)
{
intiRow4Read,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));
}
}
}
voidWriteMatrix(int*ipMatrixWrite,intp_iRowOrCol)
{
intiRow4Write,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",'┘');
#ifIsDebug==1
getch();
#endif
}
intComputMatrix(int*ipMatrixComput,intiColComput)
{
intiRow4Comput,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)))
{
#ifIsDebug==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++;
}
}
#ifIsDebug==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);
#ifIsDebug==1
printf("result=%d/n",iResult);
getch();
#endif
free(ipNewMatrix);
}
}
}
}
return(iResult);
}
voidmain(void)
{
int*ipMatrix,iRowOrCol;
charchSure;
while(1)
{
printf("/n/nEntertheroworcolofmatrix:");
scanf("%d",&iRowOrCol);
if(iRowOrCol>0)
{
if(ipMatrix=(int*)malloc(iRowOrCol*iRowOrCol*sizeof(int)))
{
while(1)
{
ReadMatrix(ipMatrix,iRowOrCol);
WriteMatrix(ipMatrix,iRowOrCol);
printf("Areyousure(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:Noenoughmemory!/n");
break;
}
}
else
{
printf("Invalidcolvalue!/n/n");
}
printf("/nComputeanotherMatrix(Y/N)?");
chSure=getche();
if(toupper(chSure)!='Y')
{
break;
}
}
}