资源简介
高斯消去法源代码,能实现线性方程Ax=b的求解,内有注释,简单易懂
代码片段和文件信息
#include “iostream“
#include “fstream“
#include “string“
#include “stdlib.h“
#include “gauss.h“
using namespace std;
int CountLines() //统计系数阵阶数
{
int n = 0;
string temp;
fstream f;
f.open(“equation.txt“ ios::in);
if (f.fail())
{
cout << “打开文件失败未找到文件\n“;
}
else
{
while (getline(f temp))
{
if (temp[0] == ‘#‘)
break;
n++;
}
}
f.close();
return n;
}
void equation::input()
{
int t i = 0 j = 0;
string temp;
fstream f;
f.open(“equation.txt“ ios::in);
while (getline(f temp ‘ ‘)) //从文件读入系数矩阵到A中
{
t = temp.find(‘\n‘);
if (t > -1)
temp.erase(t sizeof(‘\n‘));
if (temp[0] == ‘#‘)
break;
else
{
if (j == n - 1)
{
A[i][j] = atof(temp.c_str());
j = 0;
i++;
}
else
{
A[i][j] = atof(temp.c_str());
j++;
}
}
}
i = 0;
while (getline(f temp ‘ ‘) && !f.eof()) //从文件读入常数项到b中
{
t = temp.find(‘\n‘);
if (t > -1)
temp.erase(t sizeof(‘\n‘));
b[i] = atof(temp.c_str());
i++;
}
// f.close(); //这里加这东西居然会报错
}
void equation::DisplayEquation() //显示系数矩阵与常数向量
{
cout << “系数矩阵为:\n“;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
{
if (j == n - 1)
cout << A[i][j] << “\t\n“;
else
cout << A[i][j] << “\t“;
}
cout << “\n常数项为:\n“;
for (int i = 0; i < n; i++)
cout << b[i] << “\t“;
cout< }
void equation::DisplayEquation(int a) //显示系数矩阵与常数向量
{
cout << “高斯消元后系数矩阵为:\n“;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
{
if (j == n - 1)
cout << A[i][j] << “\t\n“;
else
cout << A[i][j] << “\t“;
}
cout << “\n常数项为:\n“;
for (int i = 0; i < n; i++)
cout << b[i] << “\t“;
cout< }
void equation::solve() //求解方程,将解存入x中
{
double c;
int q=0;
//================================forward elimination==================================
for (int k = 0; k < n; k++)
{
for (int i = k + 1; i < n; i++)
{
c = A[i][k] / A[k][k];
for (int j = k; j < n; j++)
A[i][j] = A[i][j] - A[k][j] * c;
b[i] = b[i] - b[k] * c;
}
}
if(A[n-1][n-1]<10e-8)
{
cout<<“方程无解\n“;
cout<<“按任意键退出“< getchar();
exit(1);
}
//==========================back substitution==========================================
double sum;
x[n - 1] = b[n - 1] / A[n - 1][n - 1];
for (int i = n - 2; i >= 0; i--)
{
sum = 0;
for (int j = n - 1; j >= i + 1; j--)
sum = sum + A[i][j] * x[j];
x[i] = (b[i] - sum) / A[i][i];
}
}
void equation::DisplayResult()
{
cout << “方程的解为:\n“;
for (int i = 0; i < n; i++)
cout << x[i] << “ “;
cout << endl;
}
void equation::output()
{
fstream r;
r.open(“result.txt“ ios::out);
if (r.fail())
{
cout << “打开文件失败\n“<<“按任意键退出“< getchar();
exit(1);
}
for (int i = 0; i < n; i++)
{
r << “X“ << i << “=“ << x[i] << “\n“;
}
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 64 2017-10-20 20:53 gauss\bin\Debug\equation.txt
文件 1078599 2017-10-20 20:08 gauss\bin\Debug\gauss-有显示.exe
文件 1079111 2017-10-20 20:33 gauss\bin\Debug\gauss.exe
文件 27 2017-10-20 20:53 gauss\bin\Debug\result.txt
文件 561664 2017-10-20 19:29 gauss\bin\Release\gauss.exe
文件 40 2017-10-20 16:37 gauss\equation.txt
文件 1095 2017-10-20 19:10 gauss\gauss.cbp
文件 185 2017-10-20 20:40 gauss\gauss.depend
文件 436 2017-10-20 19:48 gauss\gauss.h
文件 542 2017-11-01 16:12 gauss\gauss.layout
文件 3512 2017-10-20 20:33 gauss\main.cpp
文件 38716 2017-10-20 20:33 gauss\obj\Debug\main.o
文件 13229 2017-10-20 19:29 gauss\obj\Release\main.o
文件 24 2017-10-20 20:53 gauss\result.txt
目录 0 2017-10-20 20:33 gauss\bin\Debug
目录 0 2017-10-20 19:29 gauss\bin\Release
目录 0 2017-10-20 20:33 gauss\obj\Debug
目录 0 2017-10-20 19:29 gauss\obj\Release
目录 0 2017-10-20 19:25 gauss\bin
目录 0 2017-10-20 19:25 gauss\obj
目录 0 2017-11-01 16:12 gauss
----------- --------- ---------- ----- ----
2777244 21
评论
共有 条评论