求行列式的值

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 int main(int argc, char **argv)
 4 {
 5     if(2!=argc)
 6     {
 7         puts("Arguments Error!");
 8         exit(-1);
 9     }
10     FILE *fp = fopen(argv[1], "r");
11     int i = 0;
12     char ch = fgetc(fp);
13     while ('\n' != ch)
14     {
15         if (ch == ',')
16             i++;
17         ch = fgetc(fp);
18     }
19     int n = i + 1;    //n是阶数
20     double *mat = (double *)calloc(n * n, 8);
21 
22     fseek(fp, 0, SEEK_SET);
23     i = 0;
24     while (EOF != fscanf(fp, "%lf,", mat+i))
25     i++;
26 
27     //使用三角阵进行计算
28     int j, k;
29     for (j = 1; j<n; j++)
30         for (k = 0; k < j; k++)
31         {
32             double times = mat[j*n + k] / mat[k*n + k];
33             for (i = 0; i < n; i++)
34             {
35                 mat[j*n + i] -= times * mat[k*n + i];
36             }
37         }
38     double sum = 1;
39     for (i = 0; i<n; i++)
40         sum *= mat[i*n + i];
41     printf("%f", sum);
42 
43     free(mat);
44     return 0;
45 }

 

上一篇:python文件和目录操作方法大全


下一篇:fp-ts函数式编程 - taskeither