c++实现4阶矩阵或行列式的计算

来源:互联网 发布:知乎 校园招聘 文案 编辑:程序博客网 时间:2024/04/30 10:57

           由于测试数目太少,会有各种bug,不一定每个矩阵都能计算出来,以后我会陆续优化,各位见谅

//mat.cpp  文件二#include"mat.h"#include<iostream>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:smallint 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;}//判断0int 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<iostream>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"<<endl;a.judgef(q,w);                     //化负数a.show();//cout << "第一列  2"<<endl;} while (a.judge(q, w) != 1);q++, w++;      //第一列结束,q=w=1do{e = q;     //保证q值不变for (e; e < 4; e++){                //排序for (int j = 3; j>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);  //比较,找出1int swap(int,int,int);     //交换,最小值(1)到最上面int swap1(int, int, int);int operation(int,int);    //计算,缩小元素int judge(int,int);        //检测0void judgef(int,int);     //检测负数,化为正数void sort();  void opera();int zero();void show();               //显示private:double mat[4][5] ;};


0 0
原创粉丝点击