#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
const int n=4;
const double w=1.15; //w不能定义为float类型
int main()
{
float a[n][n],b[n];
int i,j;
void gauss(float a[][n],float b[n]); //声明
cout<<"please input a[i][j]:"<<endl;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
cin>>a[i][j];
cout<<endl;
cout<<"please input b[i]:"<<endl;
for(i=0;i<n;i++)
cin>>b[i];
cout<<'/n';
gauss(a,b); //调用
return 0;
}
void gauss(float a[][n],float b[n]) //求解
{
int i,k=0;
float max_cha(float x1[n],float x2[n]);
float qiu_he1(float a[][n],float x1[n],int i);
float qiu_he2(float a[][n],float x2[n],int i);
float cha,he1,he2;
float x1[n],x2[n];
cout<<setiosflags(ios::right);
for(i=0;i<=n;i++)
{
if(i==0)
cout<<setw(10)<<"k";
else
cout<<setw(14)<<"x"<<i;
}
cout<<'/n'<<setw(10)<<k;
for(i=0;i<n;i++)
{
x1[i]=0;
cout<<setiosflags(ios::right);
cout<<setw(15)<<x1[i];
}
cout<<endl;
do
{k++;
cout<<setw(10)<<k;
for(i=0;i<n;i++)
{
he1=qiu_he1(a,x1,i);
he2=qiu_he2(a,x2,i);
x2[i]=x1[i]+w/a[i][i]*(b[i]-he2-he1);
cout<<setw(15)<<x2[i];
}
cout<<endl;
cha=max_cha(x1,x2);
for(i=0;i<n;i++)
x1[i]=x2[i];
}
while(fabs(cha)>0.00001);
cout<<endl;
}
float qiu_he1(float a[][n],float x1[n],int i)
{
float sum=0;
int j;
for(j=i;j<n;j++)
sum+=a[i][j]*x1[j];
return (sum);
}
float qiu_he2(float a[][n],float x2[n],int i)
{
float sum=0;
int j;
for(j=0;j<i;j++)
sum+=a[i][j]*x2[j];
return (sum);
}
float max_cha(float x1[n],float x2[n])
{
int j;
float max=fabs(x1[0]-x2[0]);
float cha;
for(j=0;j<n;j++)
{
cha=fabs(x1[j]-x2[j]);
if(max<cha)
max=cha;
}
return (max);
}