📄 gauss-seidel.cpp
字号:
#include<iostream.h>
int jiefangchengzu(int,int);
int main()
{
int f,n=0;
cout<<"输入方程组的阶数:"<<endl;
cin>>f;
n=jiefangchengzu(n,f);
cout<<"迭代次数n="<<n<<endl;
return 0;
}
int jiefangchengzu(int n,int f)
{
long double **a,*b,*x;
long double c=1,d=0,e=0;
a=new long double*[f];
for(int l=0;l<f;l++)
a[l]=new long double[f];
b=new long double[f];
x=new long double[f];
cout<<"请输入矩阵系数a和b:"<<endl;
for(int t=0; t<f; t++)
{
for(int j=0; j<f; j++)
cin>>a[t][j];
cout<<"b["<<t<<"]=";
cin>>b[t];
x[t]=0;
}
while(c>0.0000000001)
{
c=0;
n++;
for(int p=0; p<f; p++)
{
d=0;
for(int q=0; q<f; q++)
{
if(p==q)
continue;
d=d+a[p][q]*x[q];
}
e=x[p];
x[p]=(b[p]-d)/a[p][p];
if((e-x[p])>=0.0000000001)
c=e-x[p];
if((e-x[p])<=-0.0000000001)
c=x[p]-e;
}
}
cout<<"解向量为:"<<endl;
for(int g=0; g<f;g++)
cout<<x[g]<<endl;
return n;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -