编写C++程序用高斯消去法中的列主元法求解4次以上的线性方程具体要求为:1线性方程的系数可以由文本文件读入也可以手工录入;录入的系数可以保存到文本文件。2能够显示每次消元的结果3清晰显示方程的解如果方程没有解也给出无解的结论。143验收要求1431功能验收要求1程序控制用户可以在提示下输入或导入要求解的线性方程系数并求解。一次求解执行完成后可以提示用户再次进行另一个线性方程的求解。由用户输入特殊字
下面是一个满足要求的C++程序示例:
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
// 打印矩阵
void printMatrix(const vector<vector<double>>& matrix) {
for (const auto& row : matrix) {
for (const auto& num : row) {
cout << num << " ";
}
cout << endl;
}
}
// 高斯消元法求解线性方程组
vector<double> solveLinearEquations(vector<vector<double>>& matrix) {
int n = matrix.size();
vector<double> result(n);
// 消元过程
for (int i = 0; i < n - 1; i++) {
// 选取主元
int maxRow = i;
double maxVal = matrix[i][i];
for (int j = i + 1; j < n; j++) {
if (abs(matrix[j][i]) > abs(maxVal)) {
maxRow = j;
maxVal = matrix[j][i];
}
}
// 交换行
if (maxRow != i) {
swap(matrix[maxRow], matrix[i]);
}
// 消元
for (int j = i + 1; j < n; j++) {
double factor = matrix[j][i] / matrix[i][i];
for (int k = i + 1; k < n + 1; k++) {
matrix[j][k] -= factor * matrix[i][k];
}
}
}
// 回代求解
for (int i = n - 1; i >= 0; i--) {
double sum = 0.0;
for (int j = i + 1; j < n; j++) {
sum += matrix[i][j] * result[j];
}
result[i] = (matrix[i][n] - sum) / matrix[i][i];
}
return result;
}
int main() {
char choice;
do {
int n;
cout << "请输入线性方程的阶数:";
cin >> n;
vector<vector<double>> matrix(n, vector<double>(n + 1));
// 选择输入方式
cout << "请选择输入方式(1-手工输入,2-从文件导入):";
int inputChoice;
cin >> inputChoice;
if (inputChoice == 1) {
// 手工输入系数
cout << "请按行输入线性方程组的系数矩阵(每行" << n + 1 << "个数,用空格分隔):" << endl;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n + 1; j++) {
cin >> matrix[i][j];
}
}
} else if (inputChoice == 2) {
// 从文件导入系数
string filename;
cout << "请输入包含系数的文件名:";
cin >> filename;
ifstream file(filename);
if (!file) {
cerr << "无法打开文件" << filename << endl;
continue;
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n + 1; j++) {
file >> matrix[i][j];
}
}
file.close();
} else {
cout << "无效的选择" << endl;
continue;
}
// 求解线性方程组
vector<double> result = solveLinearEquations(matrix);
// 打印消元过程和结果
cout << "消元过程:" << endl;
printMatrix(matrix);
cout << "方程的解:" << endl;
for (int i = 0; i < n; i++) {
cout << "x" << i + 1 << " = " << result[i] << endl;
}
// 选择输出方式
cout << "请选择输出方式(1-打印到控制台,2-保存到文件):";
int outputChoice;
cin >> outputChoice;
if (outputChoice == 1) {
// 打印到控制台
cout << "将结果打印到控制台:" << endl;
for (int i = 0; i < n; i++) {
cout << "x" << i + 1 << " = " << result[i] << endl;
}
} else if (outputChoice == 2) {
// 保存到文件
string filename;
cout << "请输入保存结果的文件名:";
cin >> filename;
ofstream file(filename);
if (!file) {
cerr << "无法打开文件" << filename << endl;
continue;
}
file << "方程的解:" << endl;
for (int i = 0; i < n; i++) {
file << "x" << i + 1 << " = " << result[i] << endl;
}
file.close();
} else {
cout << "无效的选择" << endl;
continue;
}
// 继续求解
cout << "是否继续求解(Y/N)?";
cin >> choice;
} while (choice == 'Y' || choice == 'y');
return 0;
}
使用该程序,用户可以选择手工输入线性方程的系数,或从文件导入系数。然后程序将使用列主元法进行高斯消元,打印消元过程和方程的解。最后,用户可以选择将结果打印到控制台或保存到文件。程序还支持多次求解不同的线性方程,并在用户输入特殊字符时结束程序运行
原文地址: https://www.cveoy.top/t/topic/iLjw 著作权归作者所有。请勿转载和采集!