You are on page 1of 5

//~~~~~~~BAI TAP LON~~~~~~~~~~~~~~~~

#include <conio.h>
#include <math.h>
#include <iostream>
using namespace std;
#include <iomanip>

//
//
//
//
//

Dng mn hnh hin th


Th vin ton hc
Th vin vo
ra chun
nh dng thng tin hin th

//~~~~~~~LOP MA TRAN~~~~~~~~~~~~~~~~
class MAT{
public:
int u,v;
float **r;
MAT();
MAT(int u1,int u2);
friend int Rank(const MAT & x);// tnh c hng ca A :Rank(A)
friend float Det(const MAT & x);// tnh c Det(A)
friend MAT operator + (const MAT& x1,const MAT& x2);// vit c A+B
friend MAT operator - (const MAT& x1,const MAT& x2);// vit c A-B
friend MAT operator * (const MAT& x1,const MAT& x2);// vit c B*A
friend MAT operator ^ (const MAT& x,const int & n);// vit c A^n
friend istream & operator >> (istream & In,MAT & x);// vit c
cin>>A
friend ostream & operator << (ostream & Out,const MAT & x);// vit
c cout<<A
friend MAT kalman(const MAT & a,const MAT & b);
friend MAT operator * (const MAT & x,const float & n); // vit c
A*2
friend MAT operator / (const MAT & x,const float & n); // vit c A/2
friend MAT operator * (const float & n, const MAT & x); // vit c
2*A
};
//~~~~~~~~~~DINH NGHIA HAM BAN+HAM THANH PHAN CUA LOP~~~~~~~~~~~~
//***************************************************************
MAT::MAT() {
this->u=0;this->v=0;this->r=NULL;
}
//**********************************
MAT::MAT(int u1,int v1) {
this->u=u1;this->v=v1;
this->r = new float* [u1+1];
for(int i=1;i<=u1+1;i++){
this->r[i]= new float [v1+1];
for(int j=1;j<v1+1;j++)
this->r[i][j]=0;
}
}
//***********************************
int Rank(const MAT & x){
int Rank,i,j,m,n;
float tg;
Rank=x.u;
for(i=1;i<=x.u;i++)

for(j=1;j<=x.v;j++){
if(x.r[i][j]==0&&i==j){
for(m=i+1;m<=x.u;m++)
if(x.r[m][j]!=0){
for(n=1;n<=x.u;n++){
tg=x.r[m][n];
x.r[m][n]=x.r[i][n];
x.r[i][n]=tg;
}
break;
}
}
else break;
}
for(i=1;i<=x.u;i++)
for(j=1;j<=x.v;j++){
if(x.r[i][j]!=0&&i==j) m=i+1;
while(m<=x.u){
float k=x.r[m][j]/x.r[i][j];
for(n=1;n<=x.v;n++)
x.r[m][n]=x.r[m][n]-k*x.r[i][n];
m++;
}
}
for(i=1;i<=x.u;i++){
for(j=1;j<=x.v;j++)
if(x.r[i][j]!=0)
break;
else
if(j==x.v)
Rank--;
}
return Rank;
}
//***********************************
MAT operator + (const MAT & x1,const MAT & x2){
if(x1.u!=x2.u||x1.v!=x2.v){
cout << "\n Do 2 ma tran khong cung cap! Hay nhap lai!";
getch();
}
else {
MAT d(x1.u,x1.v);
for(int i=1;i<x1.u+1;i++)
for(int j=1;j<x1.v+1;j++)
d.r[i][j]=x1.r[i][j]+x2.r[i][j];
return d;
}
}//***********************************
MAT operator - (const MAT & x1,const MAT & x2){
if(x1.u!=x2.u||x1.v!=x2.v){
cout << "\n Do 2 ma tran khong cung cap! Hay nhap lai!";
getch();
}
else {
MAT d(x1.u,x1.v);
for(int i=1;i<x1.u+1;i++)
for(int j=1;j<x1.v+1;j++)

d.r[i][j]=x1.r[i][j]-x2.r[i][j];
return d;
}
}
//***********************************
MAT operator * (const MAT & x1,const MAT & x2){
if(x1.v!=x2.u){
cout << "\n Khong thuc hien duoc do kich thuoc 2 ma tran ko hop
le! Hay nhap lai!";
getch();
}
else
{
MAT d(x1.u,x2.v);
for(int i=1;i<x1.u+1;i++)
for(int j=1;j<x2.v+1;j++){
for(int k=1;k<x1.v+1;k++)
d.r[i][j]+=x1.r[i][k]*x2.r[k][j];
}
return d;
}
}
//***********************************
MAT operator * (const MAT & x,const float & n){
MAT d(x.u,x.v);
for(int i=1;i<x.u+1;i++)
for(int j=1;j<x.v+1;j++)
d.r[i][j]=x.r[i][j]*n;
return d;
}
//***********************************
MAT operator * (const float & n, const MAT & x){
MAT d(x.u,x.v);
for(int i=1;i<x.u+1;i++)
for(int j=1;j<x.v+1;j++)
d.r[i][j]=x.r[i][j]*n;
return d;
}
//***********************************
MAT operator / (const MAT & x,const float & n){
MAT d(x.u,x.v);
for(int i=1;i<x.u+1;i++)
for(int j=1;j<x.v+1;j++)
d.r[i][j]=x.r[i][j]/n;
return d;
}
//***********************************
MAT operator ^ (const MAT & x,const int & n) {
MAT d(x.u,x.v);
for (int i=1; i <x.u+1; i++)
for (int j=1;j<x.v+1;j++)
d.r[i][j]=x.r[i][j];
for (int k=2; k <=n; k++)
d=d*d;
return d;
}
//***********************************

ostream& operator << (ostream & Out,const MAT & x) {


for (int i=1 ; i<= x.u ; ++i) {
Out << "\n" ;
for (int j=1; j<=x.v; ++j)
Out << setw(9) << x.r[i][j] ;
}
Out << "\n" ;
return Out;
}
//***********************************
istream & operator >> (istream & In,MAT & x){
cout << "\n So hang:";
In >> x.u;
cout << "\n So cot:";
In >> x.v;
x.r=new float*[x.u+1];
for(int i=1;i<x.u+1;i++){
x.r[i]=new float[x.v+1];
for(int j=1;j<x.v+1;j++){
cout << "Phan tu ["<<i<<"]"<<"["<<j<<"]"<<"=";
In >> x.r[i][j];
}
}
return In;
}
//***********************************
MAT kalman (const MAT & a,const MAT & b){
MAT d(b.u,b.u);
for(int i=1;i<b.u+1;i++)
d.r[i][1]=b.r[i][1];
for(int j=2;j<b.u+1;j++)
for(int k=1;k<=b.u+1;k++){
MAT c=a^(j-1);
d.r[k][j]=(c*b).r[k][1];
}
return d;
}
//***********************************
void main(){
char t;
do{
system("cls");
system("color 0c");
//***********************************
MAT a,b;
cout << "\n
cout << "\n
cin >> a;
cout << "\n
cin >> b;
cout << "\n
cout << "\n
cout << "\n
cout << "\n
cout << "\n

Nhap 2 ma tran cung cap!"<<endl;


Nhap vao ma tran a: "<<endl;
Nhap vao ma tran b: "<<endl;
Ma
Ma
Ma
Ma
Ma

tran
tran
tran
tran
tran

a+b
a-b
a*b
a^2
a/2

=";
=";
=";
=";
=";

cout << (a+b);//


cout << (a-b);//
cout << (a*b);//
cout << (a^2);
cout << (a/2);

Nhap 2 ma tran
a va b
cung cap

cout << "\n Ma tran a*2 =";


cout << "\n Ma tran 2*a =";

cout << (a*2);


cout << (2*a);

//***********************************
cout << "\n Do you want continue? Y/N
cin >> t;
}while (t=='y'||t=='Y');
}
/*
cout << "\n Nhap bac cua he " <<"\n";
cin >> n;
u=kalman(a,b);
cout << "\n Ma tran Kalman: " <<"\n" <<u;
if (Rank(u)!=n)
cout << "\n He KHONG Dieu Khien Duoc !";
else
cout << "\n He Dieu Khien Duoc!";
*/

\n";

You might also like