Cái này do mình tự viết về các phép toán cộng , nhân 2 ma trận.Các dạng biến đổi sơ cấp trên dòng và rút gọn ma trận về ma trận bậc thang. Mình đang suy nghĩ thuật toán về tính định thức ma trận. Các bạn xem rùi cho mình ý kiến nha. :P
Code:
#include <stdio.h>
#include <conio.h>
#include <iostream.h>
#include <windows.h>
#define SIZE 10
class matran
{
private:
	int sd,sc;
	float **p;
public:
	void nhap();
	void nhap(int a,int b);
	void nhan(const matran &a,const matran &b);
	void cong(const matran &a,const matran &b);
	void BDSCTD1(int sd,float a);
	void BDSCTD2(int sd1,int sd2);
	void BDSCTD3(int sd1,int sd2,float q);
	void sapxep();
	void sapxep1();
	void phanbu(const matran &a,int n,int m);
	int dong();
	int cot();
	void xuat();
	~matran();
};
void matran::nhap()
{
	cout<<"Nhap so dong va so cot"<<endl<<"Gioi han cua dong va cot la khong duoc qua 10"<<endl;
	cout<<"So dong = ";cin>>this->sd;cout<<"So cot = ";cin>>this->sc;
	while(this->sd<=0||this->sd>SIZE||this->sc<=0||this->sc>SIZE)
	{
		cout<<"Du lieu khong hop le, nhap lai"<<endl;
		cin>>sd>>sc;
	}
	p=new float*[sd];
	for(int t=0;t<sd;t++)
		p[t]=new float[sc];
	for(int i=0;i<this->sd;i++)
		for(int j=0;j<sc;j++)
		{
			cout<<"Phan tu dong "<<i+1<<" cot "<<j+1<<" la ";cin>>p[i][j];
		}
}
void matran::nhap(int a,int b)
{
	sd=a;
	sc=b;
	p=new float*[sd];
	for(int t=0;t<sd;t++)
		p[t]=new float[sc];
}
void matran::nhan(const matran &a,const matran &b)
{
	int i,j,t;
	float ret;
	int lk=a.sc;
	for(i=0;i<a.sd;i++)
	{
		for(j=0;j<b.sc;j++)
		{
			ret=0;
			for(t=0;t<lk;t++)
				ret=ret+(a.p[i][t]*b.p[t][j]);
			this->p[i][j]=ret;
		}
	}
}
void matran::cong(const matran &a,const matran &b)
{
	int i,j;
	for(i=0;i<a.sd;i++)
		for(j=0;j<a.sc;j++)
		{
			this->p[i][j]=a.p[i][j]+b.p[i][j];
		}
}
void matran::BDSCTD1(int sd,float a)
{
	for(int i=0;i<this->sc;i++)
		this->p[sd][i]=this->p[sd][i]*a;
}
void matran::BDSCTD2(int sd1,int sd2)
{
	float *o;
	o=new float[this->sc];
	for(int i=0;i<this->sc;i++)
	{
		o[i]=this->p[sd1][i];
		this->p[sd1][i]=this->p[sd2][i];
		this->p[sd2][i]=o[i];
	}
	delete []o;
	o=NULL;
}
void matran::BDSCTD3(int sd1,int sd2,float q)
{
	for(int i=0;i<this->sc;i++)
	{
		this->p[sd1][i]=this->p[sd1][i]+q*this->p[sd2][i];
	}
}
void matran::sapxep()
{
	int i=0,t=0;
	int k;
	k=i;
	while(i<this->sd-1)
	{
		while(this->p[i][t]!=0)
			{
				i++;
				k=i;
				break;
			}
		for(int j=k;j<this->sd;j++)
		{
			if(this->p[j][t]!=0)
			{
				this->BDSCTD2(j,i);
				i++;
			}
		}
		k=i;t++;
	}
}
int matran::dong()
{
	return this->sd;
}
int matran::cot()
{
	return this->sc;
}
void matran::sapxep1()
{
	int t,i=0,j=0;
	do
	{
		while(this->p[i][j]==0)
		{
			cout<<"hehe"<<endl;
			j++;
		}t=i;
		for(int m=t+1;m<this->sd;m++)
		{
			if(this->p[m][j]!=0)
			{
				this->BDSCTD3(m,t,-this->p[m][j]/this->p[t][j]);
				this->BDSCTD1(m,this->p[t][j]);
			}
		}i++;
	}while(i<this->sd);

}
void matran::phanbu(const matran &a,int n,int m)
{

}
void matran::xuat()
{
	for(int i=0;i<this->sd;i++)
	{
		for(int j=0;j<this->sc;j++)
		{
			cout<<this->p[i][j]<<"   ";
		}
		cout<<endl;
	}
}
matran::~matran()
{
	cout<<"DELETE"<<endl;
	for(int i=0;i<this->sd;i++)
		delete []p[i];
	delete []p;
	p=NULL;
}
void main()
{
	char traloi;
	int ma;
	cout<<"**********DU AN MA TRAN**********"<<endl<<"Huong dan : Chuong trinh se tu liet ke cac phep toan trong MA TRAN"<<endl<<"Chon so thu tu cua phep toan"<<endl;
	cout<<"An mot phim bat ky de tiep tuc"<<endl;
	getch();
	do{
		system("cls");
		cout<<"**********DANH SACH CAC PHEP TOAN**********"<<endl;
		cout<<"1.Phep toan nhan hai ma tran"<<endl<<"2.Phep toan cong hai ma tran"<<endl;
		cout<<"3.Cac phep bien doi so cap tren dong"<<endl;
		cout<<"4.Rut gon mot ma tran"<<endl;
		cout<<"5.Dinh thuc"<<endl;
		cout<<"Chon mot phep toan"<<endl;
		cin>>ma;
	while(ma==1)
	{
	do{
		system("cls");
		cout<<"*****PHEP NHAN MA TRAN*****"<<endl;
		int sda,scb;
		matran a;
		matran b;
		cout<<"Nhap ma tran A"<<endl;
		a.nhap();
		cout<<"Nhap ma tran B"<<endl;
		b.nhap();
		system("cls");
		cout<<"*****PHEP NHAN MA TRAN*****"<<endl;
		cout<<"Ma tran A"<<endl<<endl;
		a.xuat();cout<<endl;
		cout<<"Ma tran B"<<endl<<endl;
		b.xuat();cout<<endl;
		sda=a.dong();
		scb=b.cot();
		if(sda==scb)
		{
			matran c;
			c.nhap(sda,scb);
			c.nhan(a,b);
			cout<<"Tich 2 ma tran A va B"<<endl<<endl;
			c.xuat();
		}
		else
		{
			cout<<"Khong ton tai phep nhan hai ma tran a va b"<<endl<<"(Luu y : so dong va so cot cua hai ma tran)"<<endl;
		}
		cout<<"Ban co muon thuc hien lai phep toan Y/N"<<endl;
		fflush(stdin);
		traloi=getch();
	}while(traloi=='y'||traloi=='Y');
	break;
	}
	while(ma==2)
	{
		do
		{
			system("cls");
			cout<<"*****PHEP CONG MA TRAN*****"<<endl;
			int sda,sdb,sca,scb;
			matran a;
			matran b;
			cout<<"Nhap ma tran A"<<endl;
			a.nhap();
			cout<<"Nhap ma tran b"<<endl;
			b.nhap();
			system("cls");
			cout<<"*****PHEP CONG MA TRAN*****"<<endl;
			cout<<"Ma tran A"<<endl<<endl;
			a.xuat();cout<<endl;
			cout<<"Ma tran B"<<endl<<endl;
			b.xuat();cout<<endl;
			sda=a.dong();sca=a.cot();sdb=b.dong();scb=b.cot();
			if(sda==sdb&&sca==scb)
			{
				matran c;
				c.nhap(sda,sca);
				c.cong(a,b);
				cout<<"Tong 2 ma tran A va B"<<endl<<endl;
				c.cong(a,b);cout<<endl;
				c.xuat();cout<<endl;
			}
			else
			{
				cout<<"Khong ton tai hai phep cong ma tran"<<endl<<"(Luu y: so dong va so cot cua hai ma tran)";
			}
			cout<<"Ban co muon thuc hien lai phep toan Y/N"<<endl;
			fflush(stdin);
			traloi=getch();
		}while(traloi=='y'||traloi=='Y');
		break;
	}
	while(ma==3)
	{
		do
		{
			system("cls");
			cout<<"*****PHEP BIEN DOI SO CAP TREN DONG*****"<<endl;
			int sd0;
			matran a;
			int cohieu;
			cout<<"Nhap ma tran A"<<endl;
			a.nhap();
			sd0=a.dong();
			system("cls");
			cout<<"Ma tran A"<<endl<<endl;a.xuat();cout<<endl;
			do{
			cout<<"1.Nhan mot so cho mot dong"<<endl;
			cout<<"2.Doi 2 dong cho nhau"<<endl;
			cout<<"3.Cong vao mot boi cua dong khac"<<endl;
			cin>>cohieu;
			while(cohieu==1)
			{
				int sd;float k;
				cout<<"Nhap so dong muon thao tac"<<endl;
				cin>>sd;
				while(sd<=0)
				{
					cout<<"So dong khong ton tai , nhap lai so dong"<<endl;
					cin>>sd;
				}
				cout<<"Nhap so muon nhan vao so dong"<<endl;
				cin>>k;cout<<endl;
				a.BDSCTD1(sd-1,k);cout<<"Ma tran A sau khi BDSCTD"<<endl;
				a.xuat();
				break;
			}
			while(cohieu==2)
			{
				int sd1,sd2;
				cout<<"Nhap 2 dong muon hoan doi vi tri cho nhau"<<endl;
				cout<<"Dong 1 la ";cin>>sd1;
				while(sd1<=0||sd1>sd0)
				{
					cout<<"So dong khong ton tai , nhap lai"<<endl;
					cout<<"Dong 1 la ";cin>>sd1;
				}
				cout<<"Dong 2 la ";cin>>sd2;
				while(sd2<=0||sd2>sd0)
				{
					cout<<"So dong khong ton tai , nhap lai"<<endl;
					cout<<"Dong 2 la ";cin>>sd2;
				}
				a.BDSCTD2(sd1-1,sd2-1);cout<<"Ma tran A sau khi BDSCTD"<<endl;
				a.xuat();
				break;
			}
			while(cohieu==3)
			{
				int sd1,sd2;
				float q;
				cout<<"Nhap vao dong muon thao tac : ";cin>>sd1;
				while(sd1<=0||sd1>sd0)
				{
					cout<<"So dong khong ton tai, nhap lai ";
					cin>>sd1;
				}
				cout<<"Nhap vao so dong muon nhan : ";cin>>sd2;
				while(sd2<=0||sd2>sd0)
				{
					cout<<"So dong khong ton tai, nhap lai ";
					cin>>sd2;
				}
				cout<<"Nhap vao boi so : ";cin>>q;
				a.BDSCTD3(sd1-1,sd2-1,q);
				cout<<"Ma tran A sau khi BDSCTD"<<endl;
				a.xuat();break;
			}
			cout<<"Tiep tuc phep bien doi so sap tren dong Y/N"<<endl;fflush(stdin);
			traloi=getch();
			}while(traloi=='y'||traloi=='Y');
			cout<<"Ban muon thuc hien lai phep toan Y/N"<<endl;
			traloi=getch();
		}while(traloi=='y'||traloi=='Y');
		break;
	}
	while(ma==4)
	{
		do{
			system("cls");
		cout<<"*****RUT GON THANH MA TRAN BAC THANG*****"<<endl;
		matran a;
		a.nhap();
		a.sapxep();
		a.xuat();
		a.sapxep1();
		system("cls");
		cout<<"*****RUT GON THANH MA TRAN BAC THANG*****"<<endl;
		cout<<"ma tran bac thang cua ma tran A co dang"<<endl;
		a.xuat();
		cout<<"Ban co muon thuc hien lai chuong trinh Y/N"<<endl;
		fflush(stdin);
		traloi=getch();
		}while(traloi=='y'||traloi=='Y');
		break;
	}
	while(ma==5)
	{
		do
		{
			system("cls");
			cout<<"*****DINH THUC*****"<<endl;
			cout<<"Ban co muon thuc hien lai chuong trinh Y/N"<<endl;
			fflush(stdin);
			traloi=getch();
		}while(traloi=='y'||traloi=='Y');
		break;
	}
	cout<<"Ban muon thuc hien lai chuong trinh Y/N"<<endl;
	traloi=getch();
	}while(traloi=='y'||traloi=='Y');

}