#include<iostream>
#include<cmath>
#include<string>
#include<string.h>
#include<cstring>
using namespace std;
static float rec[3][4]={1,2,1,2,-2,-2,-1,-3,2,-3,-2,-1};
int abs(int a)
//绝对值函数的实现
{
if(a<0)
return -a;
return a;
}
//返回当前j列下的最大值的(限制条件为行)
int absmax(int i,int j,int row)
{
int choice=i;//选择的绝对值得最大位置
int max=-1111;
for(int c=i;c<row;c++)
{
if(max<abs(rec[c][j]))
{
choice=c;
max=abs(rec[c][j]);
}
}
return choice;
}
void swaprow(int row,int row1)
{
float b[4];
memcpy(b,rec[row],sizeof(b));
memcpy(rec[row],rec[row1],sizeof(b));
memcpy(rec[row1],b,sizeof(b));
}
int main(int argc,char *argv)
{
// float rec[3][4]={1,2,1,2;-2,-2,-1,-3;2,-3,-2,-1};
//创建了一个浮点型矩阵来实现过程的存储
//初始系数的存储
int row=3;
int choice;
for(int j=0;j<3;j++)//列做外层循环
{
choice=absmax(j,j,row);
// cout<<choice<<endl;
// cout<<"this is j"<<j<<endl;
if(choice!=j)
//如果choice不等于当前行的数值,也就是说当前行不为绝对值最大的,那么就进行行交换
swaprow(choice,j);
//然后进行相关的运算
// for(int i=0;i<row;i++)
// {
// for(int lie1=0;lie1<=row;lie1++)
// cout<<rec[i][lie1]<<" ";
// cout<<endl;
// }
//将选中的化为1,然后在这一行的每一列都除以当前的元素
float a=rec[j][j];
for(int lie=j;lie<=row;lie++)
{
rec[j][lie]=rec[j][lie]/a;
}
//for(int hang1=0;hang1<row;hang1++)
// {
// for(int lie1=0;lie1<=row;lie1++)
// cout<<rec[hang1][lie1]<<" ";
// cout<<endl;
// }
//将相关的行的数据根据rec[j][j]的数据进行相减
for(int hang=0;hang<row;hang++)
{float first=rec[hang][j];
for(int lie=0;lie<=row;lie++)
{ if(hang!=j)
rec[hang][lie]=rec[hang][lie]-rec[j][lie]*first;
}
}
// cout<<"****************"<<endl;
cout<<"这是第"<<j<<"次的相关结果"<<endl;
for(int hang2=0;hang2<row;hang2++)//得出最后的相关结果
{
for(int lie2=0;lie2<=row;lie2++)
cout<<rec[hang2][lie2]<<" ";
cout<<endl;
}
cout<<"****************"<<endl;
}
int lastrow=0;
cout<<"那么最后的相关结果为[x1,x2,x3]="<<rec[0][row]<<","<<rec[1][row]<<","<<rec[2][row]<<endl;
return 0;
}