1 #include <iostream> 2 #include <fstream> 3 using namespace std; 4 5 class Det 6 { 7 public: 8 void InitializeSize(int order); 9 void Remove(void); 10 void Transpose(void); 11 void RowOperate(int objectrow, int targetrow, double times); 12 void ColumnOperate(int objectcolumn, int targetcolumn, double times); 13 void SetAValue(int row, int col, double value); 14 void InitializeValues(FILE *_fp); 15 double Calculate(void); 16 //private: 17 double **Matrix; 18 int order; 19 };//-row|column 20 void Det::RowOperate(int objectrow, int targetrow, double times) 21 { 22 for (int i = 0; i<order; i++) 23 Matrix[targetrow][i] += times*Matrix[objectrow][i]; 24 } 25 void Det::ColumnOperate(int objectcolumn, int targetcolumn, double times) 26 { 27 for (int i = 0; i<order; i++) 28 Matrix[targetcolumn][i] += times*Matrix[objectcolumn][i]; 29 } 30 void Det::Transpose() 31 { 32 double temp; 33 for (int i = 0; i<order; i++) 34 for (int j = 0; j<i - 1; j++) 35 { 36 temp = Matrix[i][j]; 37 Matrix[i][j] = Matrix[j][i]; 38 Matrix[j][i] = temp; 39 } 40 } 41 void Det::InitializeSize(int inorder) 42 { 43 order = inorder; 44 Matrix = new double *[order]; 45 for (int i = 0; i<order; i++) 46 { 47 Matrix[i] = new double[order]; 48 } 49 } 50 void Det::Remove() 51 { 52 for (int i = 0; i<order; i++) 53 delete[] Matrix[i]; 54 delete[] Matrix; 55 order = 0; 56 Matrix = NULL; 57 } 58 /*double Det::Calculate()//BUG HERE 59 { 60 for (int t = 1; t<order; t++) 61 for (int o = 0; o<t; o++) 62 Det::RowOperate(o, t, Matrix[t][o] / Matrix[o][o]); 63 double ret = 1; 64 for (int i = 0; i<order; i++) 65 ret *= Matrix[i][i]; 66 printf("%lf", ret); 67 return ret; 68 }*/ 69 double Det::Calculate()//BUG HERE 70 { 71 //备份Matrix 72 double temp[order*order]; 73 for (int i = 0; i<order; i++) 74 for (int j = 0; j<order; j++) 75 temp[i*order+j]=Matrix[i][j]; 76 //计算行列式 77 for (int t = 1; t<order; t++) 78 for (int o = 0; o<t; o++) 79 { 80 double times = Matrix[t][o] / Matrix[o][o]; 81 for (int i = 0; i < order; i++) 82 { 83 Matrix[t][i] -= times * Matrix[o][i]; 84 } 85 } 86 //统计 87 double ret = 1; 88 for (int i = 0; i<order; i++) 89 ret *= Matrix[i][i]; 90 91 //还原Matrix 92 for (int i = 0; i<order; i++) 93 for (int j = 0; j<order; j++) 94 Matrix[i][j]=temp[i*order+j]; 95 96 return ret; 97 } 98 void Det::SetAValue(int row, int col, double value) 99 { 100 Matrix[row][col] = value; 101 } 102 void Det::InitializeValues(FILE *_fp) 103 { 104 //不知道>>输入时如何去掉",",就用FILE*吧 105 /*ifstream fp; 106 fp.open(filename); 107 for(int i = 0; i<order; i++) 108 for(int j = 0; j<order; j++) 109 fp >> Matrix[i][j]; 110 fp.close();*/ 111 fseek(_fp, 0, SEEK_SET); 112 for (int i = 0; i<order; i++) 113 for (int j = 0; j<order; j++) 114 fscanf(_fp, "%lf,", &(Matrix[i][j])); 115 } 116 117 118 int main(int argc, char **argv) 119 { 120 if (2 != argc) 121 { 122 puts("Arguments Error!"); 123 exit(-1); 124 } 125 /*ifstream fp; 126 char ch; 127 int i = 0, n = 0; 128 fp.get(ch); 129 while (ch != '\n') 130 { 131 if (ch == ',') 132 n++; 133 i++; 134 fp.get(ch); 135 } 136 fp.close();*/ 137 FILE *fp = fopen(argv[1], "r"); 138 int n = 0; 139 char ch = fgetc(fp); 140 while ('\n' != ch) 141 { 142 if (ch == ',') 143 n++; 144 ch = fgetc(fp); 145 } 146 Det det; 147 det.InitializeSize(n+1); 148 det.InitializeValues(fp); 149 150 for (int i = 0; i<det.order; i++) 151 { 152 puts(" "); 153 for (int j = 0; j<det.order; j++) 154 cout<<det.Matrix[i][j]<<'\t'; 155 } 156 cout<<'\n'<<det.Calculate()<<endl; 157 fclose(fp); 158 return 0; 159 }