由于测试数目太少,会有各种bug,不一定每个矩阵都能计算出来,以后我会陆续优化,各位见谅
//mat.cpp 文件二
#include"mat.h"
#include
using namespace std;
//测试
void Mat::asd(){
double a1[4][5] = { { 2, -1, -1, 1, 2 }, { 1, 1, -2, 1, 4 },//
{ 4, -6, 2, -2, 4 }, { 3, 6, -9, 7, 9 } };//
for (int i = 0; i < 4; i++){//
for (int j = 0; j < 4; j++){
Mat::mat[i][j]= a1[i][j];//测试,删掉
}}}//删掉
//输入
void Mat::assignment() {
for (int i = 0; i < 4; i++){
for (int j = 0; j < 4; j++){
cin >> mat[i][j];
}
}
}
//输出
void Mat::show(){
for (int i = 0; i < 4; i++){
for (int j = 0; j < 4; j++){
cout << mat[i][j]<<" ";
}
cout << endl;
}
}
//比较 i:max j:small
int Mat::compare(int i,int j,int k){
if (mat[i][k]>mat[j][k]){
Mat::swap(i,j,k);
}
if (mat[i][k] == 0){
Mat::swap1(i, j, k);
}
return 0;
}
//交换升序
int Mat::swap(int i, int j,int k){
double save[5];
for (k; k < 4; k++){
save[k] = mat[i][k];
mat[i][k] = mat[j][k];
mat[j][k] = save[k];
}
return 0;
}
//降序
int Mat::swap1(int i, int j, int k){
double save[5];
for (k; k < 4; k++){
save[k] = mat[j][k];
mat[j][k] = mat[i][k];
mat[i][k] = save[k];
}
return 0;
}
//化简
int Mat::operation(int a, int b){
if (mat[a][b] == 1){
int i,j;
i = a + 1,j=b;
for (; i < 4; i++){
for (; j < 4; j++){
mat[i][j] = mat[i][j] - mat[i][b] * mat[a][j];
}
mat[i][b] = 0;//mat[i][b] - mat[i][b] * mat[a][b];
}
}
else{
int y, z;
y = a + 1, z = b ;
for (; y < 4; y++){
while(mat[y][b]>=mat[a][b] && (mat[a][b]>0||(mat[y][b]<0&&mat[a][b]<0))){
for (; z < 4; z++){
mat[y][z] = mat[y][z] - mat[a][z];
}
z = b ;
}
}
}
return 0;
}
//判断0
int Mat::judge(int i,int j){
if (i == 0){
if (Mat::mat[i + 1][j] == 0 && Mat::mat[i + 2][j] == 0 && Mat::mat[i + 3][j] == 0){
return 1;}
}
else if (i == 1){
if (Mat::mat[i + 1][j] == 0 && Mat::mat[i + 2][j] == 0){
return 1;}
}
else if(i==2){
if (Mat::mat[i + 1][j]==0){
return 1;}
}
}
//检测负数,化为正数
void Mat::judgef(int i,int j){
int a;
a = j;
for (; i < 4;i++){
if (Mat::mat[i][j] < 0){
for (; j < 4;j++){
Mat::mat[i][j] = -Mat::mat[i][j];
if (mat[i][j] == 0){
mat[i][j] = -mat[i][j];
}
}
j = a;
}
}
}
//最后两行排序
void Mat::sort(){
double a;
if (mat[2][2] > mat[2][3]){
for (int j = 2; j < 4; j++){
a = mat[2][j];
mat[2][j] = mat[3][j];
mat[3][j] = a;
}
}
}
void Mat::opera(){
int a,b;
a = mat[3][2], b = mat[2][2];
if (a%b== 0){
mat[3][4] = mat[3][4] / mat[3][3];
mat[3][3] = 1;
}
else if (b%a == 0){
mat[3][3] = mat[3][3] / mat[3][4];
mat[3][3] = 1;
}
}
int Mat::zero(){
if (mat[2][2] == 0 && mat[2][3] >= 0 && mat[3][2] > 0 && mat[3][3]){
double b;
b = mat[2][2];
mat[3][2] = mat[2][2];
mat[2][2] = b;
b = mat[2][3];
mat[3][3] = mat[2][3];
mat[2][3] = b;
}
return 0;
}
//源.cpp 文件三
#include"mat.h"
#include
using namespace std;
int main(){
int q = 0, w = 0,e,r;
Mat a;
cout << "Please enter 4*4 matix:" << endl;
//a.asd();//测试,删掉
a.assignment(); //输入
cout << endl;
cout << "Enter matrix is";
a.show();//
cout << endl;
do{
for (int i = 0; i < 4; i++){ //冒泡排序
for (int j = 3; j > i; j--){
a.compare(i, j, 0);
}
}
a.show();
cout << endl;
a.operation(q, w); //化简
a.show();//
cout <<"第一列 1"<e; j--){
a.compare(e, j, 1);
}
}
a.show();//
cout <<"第二列 1"<< endl;
a.operation(q, w); //化简
a.show();//
cout <<"第二列 2"<< endl;
a.judgef(q,w);
a.show();//
cout << "第二列 3" << endl;
} while (a.judge(q,w)!=1);
q++, w++; //第二列结束 q=w=2 即3行
do{
e = q; //保证q值不变
for (e; e < 4; e++){ //排序
for (int j = 3; j>e; j--){
a.compare(e, j, 2);
}
}
a.show();//
cout << "第三列 1" << endl;
a.operation(q, w); //化简
a.show();//
cout << "第三列 2" << endl;
a.judgef(q,w);
a.show();//
cout << "第三列 3" << endl;
} while (a.judge(q, w) != 1);
//第三列结束
//q++, w++; q=w=3
//do{
e = q; //保证q值不变
for (e; e < 4; e++){ //排序
for (int j = 3; j>e; j--){
a.compare(e, j, 2);
}
}
a.show();//
cout << "第四列 1" << endl;
a.operation(q, w); //化简
a.show();//
cout << "第四列 2" << endl;
a.judgef(q,w);
a.show();//
cout << "第四列 3" << endl;
//} while (a.judge(q, w) != 1);
//a.sort(); a.show(); cout << endl;
//a.operation(q, w);
//a.judgef(q,w);
/*q++; w++;
a.opera();
a.judgef(q, w);
a.operation(q,w);*/
a.zero();
a.show();
system("pause");
return 0;
}
//mat.h 文件一
class Mat{
public:
void asd();//测试
void assignment(); //输入数组
int compare(int,int,int); //比较,找出1
int swap(int,int,int); //交换,最小值(1)到最上面
int swap1(int, int, int);
int operation(int,int); //计算,缩小元素
int judge(int,int); //检测0
void judgef(int,int); //检测负数,化为正数
void sort();
void opera();
int zero();
void show(); //显示
private:
double mat[4][5] ;
};