c++高斯消元法求解线性方程组
#include<iostream> #include<math.h> #include<string.h> using namespace std; #define MaxNum 10 int unuse_result[MaxNum]; int GaussFun(int equ, int var, int result[],int array[MaxNum][MaxNum]) { int i, j, k, col, num1, num2; int max_r, ta, tb, gcdtemp, lcmtemp; int temp, unuse_x_num, unuse_index; col = 0; for (k = 0; k < equ && col < var; k++, col++)//循环处理增广矩阵的各行 { max_r = k; for (i = k + 1; i < equ; i++) { if (abs(array[i][col]) > abs(array[max_r][col])) { max_r = i;//保存绝对值最大的行 } } if (max_r != k) { for (j = k; j < var + 1; j++) { temp = array[k][j]; array[k][j] = array[max_r][j]; array[max_r][j] = temp; } } if (array[k][col] == 0) { k--; continue; } for (i = k + 1; i < equ; i++) { if (array[i][col] != 0) { num1 = abs(array[i][col]); num2 = abs(array[k][col]); while (num2 != 0) { temp = num2; num2 = num1 % num2; num1 = temp; } gcdtemp = num1;//最大公约数 lcmtemp = (abs(array[i][col]) * abs(array[k][col])) / gcdtemp; ta = lcmtemp / abs(array[i][col]); tb = lcmtemp / abs(array[k][col]); if (array[i][col] * array[k][col] < 0) { tb = -tb; } for (j = col; j < var + 1; j++) { array[i][j] = array[i][j] * ta - array[k][j] * tb; } } } } for (i = k; i < equ; i++) { if (array[i][col] != 0) { return -1; } } if (k < var) { for (i = k - 1; i >= 0; i--) { unuse_x_num = 0; for (j = 0; j < var; j++) { if (array[i][j] != 0 && unuse_result[j]) { unuse_x_num++; unuse_index = j; } } if (unuse_x_num > 1) { continue; } temp = array[i][var]; for (j = 0; j < var; j++) { if (array[i][j] != 0 && j != unuse_index) { temp -= array[i][j] * result[j]; } } result[unuse_index] = temp / array[i][unuse_index]; unuse_result[unuse_index] = 0; } return var - k; } for (i = var - 1; i >= 0; i--) { temp = array[i][var]; for (j = i + 1; j < var; j++) { if (array[i][j] != 0) { temp -= array[i][j] * result[j]; } } if (temp % array[i][i] != 0) { return -2; } result[i] = temp / array[i][i]; } return 0; } int main() { int i, type; int equnum, varnum; int array[MaxNum][MaxNum] = { {3,5,-4,0}, {7,2,6,-4}, {4,-1,5,-5} }; int result[MaxNum]; equnum = 3; varnum = 3; type = GaussFun(equnum, varnum, result,array);//调用高斯函数 if (type == -1) { cout << "该方程无解。" << endl; } else if (type == -2) { cout << "该方程又浮点数解没有整数解。" << endl; } else if (type > 0) { cout << "该方程有无穷多解!自由变量的数量为" << type << endl; for (i = 0; i < varnum; i++) { if (unuse_result[i]) { cout << i + 1 << "是不确定的" << endl; } else { cout << i + 1 << result[i] << endl; } } } else { cout << "该方程的解为:" << endl; for (i = 0; i < varnum; i++) { cout << i + 1 << result[i] << endl; } } return 0; }