📄 matrix.cpp
字号:
#include "stdio.h"
#include "conio.h"
#include "malloc.h"
#include "math.h"
//////////////////////////////
//这个没采用面向对象的方式:
//我个编写好完整的Matrix类,
//这是我第一次上载所以先来个简单的^_^。
//计算代数余子式,返回一个数组
//////////////////////////////
double *comp(double *mat,int i,int j,int row,int col)
{
double *com = (double*)malloc((row-1)*(col-1)*sizeof(double));
int ii = 0,jj = 0;
int k = 0;
for(ii=0;ii<row;ii++)
{
for(jj=0;jj<col;jj++)
{
if(ii!=i&&jj!=j)
{
com[k] = mat[ii*col+jj];
k++;
}
}
}
return com;
}
////////////////////////////////
//递归计算行列式的值
////////////////////////////////
double rev(double *mat,int i,int j)
{
double p = 0.0;
double *cm;
int tmp = 0;
if(i==2 && j==2)
{
return mat[i*0+0]*mat[i*1+1]-mat[j*0+1]*mat[j*1+0];
}
else
{
for(;tmp<j;tmp++)
{
cm = comp(mat,0,tmp,i,j);
p+=mat[0*0+tmp]*pow(-1,tmp)*rev(cm,i-1,j-1);
}
return p;
}
return p;
}
////////////////////
//调用
////////////////////
void main()
{
double M[3][3] =
{
{4,-6,3},
{5,2,7},
{5,-2,8}
};
double p;
int k = 0;
p = rev(&M[0][0],3,3);/*=90*/
printf("==>%lf",p);
getchar();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -