Thầy giáo ra bài tập lớn lập trình các phép toán trên ma trận theo kiểu lập trình hướng đối tượng, e làm như này không biết đã đúng kiểu lập trình hướng đối tượng chưa ạ? và tối ưu nữa?
Các a cho e xin nhận xét với.
E cảm ơn ạ.
#include <iomanip>
#include <complex>
#include <iostream>
using namespace std;
class MATRAN
{
private:
int soHang=10,soCot=10;
float det=0;
float **mt=new float*[soHang];
public:
MATRAN(int m, int n):soHang(m),soCot(n){
for(int i=0;i<this->soHang;i++)
this->mt[i]=new float[this->soCot];
cout<<fixed;
cout<<setiosflags(ios::showpoint)<<setprecision(1);
}
void thaoTac(int tt);
void inMaTran();
void nhapMaTran();
void chuyenVi();
bool dinhThuc();
float phuHop(int hangXoa, int cotXoa);
void nghichDao();
int hang();
void xoaHang();
void xoaCot();
void congMaTran();
void truMaTran();
void nhanMT_So();
void nhanMT_MT();
~MATRAN(){
for(int i=0;i<this->soHang;i++) delete[] this->mt[i];
delete[] this->mt;
this->soHang=0; this->det=0; this->soCot=0;
}
};
void MATRAN::thaoTac(int tt){
if(tt==0){
cout<<"\n+ Danh sach thao tac:\n";
cout<<"\tChuyen vi:\t1|";
cout<<"\tDinh thuc:\t2|";
cout<<"\tNghich dao:\t3|";
cout<<"\n\tHang:\t\t4|";
cout<<"\tXoa hang:\t5|";
cout<<"\tXoa cot:\t6|";
cout<<"\n\tCong ma tran:\t7|";
cout<<"\tTru ma tran:\t8|";
cout<<"\n\tMa tran x So:\t9|";
cout<<"\tMa tran x Ma tran:\t10|";
cout<<"\n\tKet thuc phien lam viec:\t0";
cout<<"\n\n+ Chon thao tac ma ban muon thuc hien: ";
cin>>tt;
cout<<endl;
}
switch(tt){
case 1: chuyenVi(); break;
case 2: if(this->dinhThuc()) cout<<"\tDinh thuc cua ma tran A la: det(A) = "<<this->det<<endl;
else cout<<"\tMa tran khong hop le!\n";
thaoTac(0); break;
case 3: nghichDao(); break;
case 4: cout<<"\tHang cua ma tran A la: "<<this->hang()<<endl;
thaoTac(0); break;
case 5: xoaHang(); break;
case 6: xoaCot(); break;
case 7: congMaTran(); break;
case 8: truMaTran(); break;
case 9: nhanMT_So(); break;
case 10: nhanMT_MT(); break;
default: break;
}
}
void MATRAN::inMaTran(){
for(int i=0;i<this->soHang;i++){
cout<<"\n\t";
for(int j=0;j<this->soCot;j++) cout<<setw(5)<<this->mt[i][j]<<" ";
}
cout<<endl;
}
void MATRAN::nhapMaTran(){
for(int i=0;i<this->soHang;i++)
for(int j=0;j<this->soCot;j++){
cout<<"\tPhan tu ["<<i+1<<","<<j+1<<"] = ";
cin>>this->mt[i][j];
}
cout<<"Ma tran ban vua nhap la:";
this->inMaTran();
}
void MATRAN::chuyenVi(){
MATRAN cv(this->soCot,this->soHang);
for(int i=0;i<cv.soHang;i++)
for(int j=0;j<cv.soCot;j++)
cv.mt[i][j]=this->mt[j][i];
cout<<"Ma tran chuyen vi cua ma tran A la:";
cv.inMaTran();
thaoTac(0);
}
bool MATRAN::dinhThuc(){
if(this->soHang==this->soCot){
int i=0,d=0;
float dt=1.0;
//Tao ma tran B lay gia tri cua ma tran A
MATRAN b(this->soHang,this->soCot);
for(int i=0;i<b.soHang;i++)
for(int j=0;j<b.soCot;j++)
b.mt[i][j]=this->mt[j][i];
//Dua ma tran ve ma tran tam giac tren
for(i=0;i<b.soHang-1;i++){
if(b.mt[i][i]==0)
for(int k=i+1;k<b.soHang;k++){
bool c=0;
if(b.mt[k][i]!=0){
for(int j=0;j<b.soCot;j++){
float t=b.mt[i][j];
b.mt[i][j]=b.mt[k][j];
b.mt[k][j]=t;
}
d++;
c=1;
break;
}
if(c) break;
}
for(int k=i+1;k<b.soHang;k++){
float tl=b.mt[k][i]/b.mt[i][i];
for(int j=0;j<soCot;j++)
b.mt[k][j]-=b.mt[i][j]*tl;
}
}
//Tinh dinh thuc bang tich cac phan tu tren duong cheo chinh
for(i=0;i<b.soCot;i++) dt*=b.mt[i][i];
if(dt!=0) dt*=pow(-1,d);
this->det=dt;
return 1;
} else{
return 0;
}
}
float MATRAN::phuHop(int hangXoa, int cotXoa){
MATRAN ph(this->soHang-1,this->soCot-1);
for(int i=0;i<hangXoa;i++){
for(int j=0;j<cotXoa;j++)
ph.mt[i][j]=this->mt[i][j];
for(int j=cotXoa;j<ph.soCot;j++)
ph.mt[i][j]=this->mt[i][j+1];
}
for(int i=hangXoa;i<ph.soHang;i++){
for(int j=0;j<cotXoa;j++)
ph.mt[i][j]=this->mt[i+1][j];
for(int j=cotXoa;j<ph.soCot;j++)
ph.mt[i][j]=this->mt[i+1][j+1];
}
ph.dinhThuc();
float pTu=(float) ph.det/this->det;
if(pTu!=0)
pTu*=pow(-1,hangXoa+cotXoa);
return pTu;
}
void MATRAN::nghichDao(){
if(this->dinhThuc()==0 || this->det==0) cout<<"\tMa Tran A khong kha nghich\n";
else{
//Tao ma tran chuyen vi cua ma tran A
MATRAN cv(this->soCot,this->soHang);
for(int i=0;i<cv.soHang;i++)
for(int j=0;j<cv.soCot;j++)
cv.mt[i][j]=this->mt[j][i];
//Tao ma tran B co cac phan tu la ma tran phu hop cua ma tran chuyen vi
MATRAN b(this->soHang,this->soCot);
cv.det=this->det;
for(int i=0;i<b.soHang;i++)
for(int j=0;j<b.soCot;j++)
b.mt[i][j]=cv.phuHop(i,j);
cout<<"Ma tran nghich dao cua ma tran A la:";
b.inMaTran();
}
thaoTac(0);
}
int MATRAN::hang(){
//Tao ma tran B lay gia tri cua ma tran A
MATRAN b(this->soHang,this->soCot);
for(int i=0;i<b.soHang;i++)
for(int j=0;j<b.soCot;j++)
b.mt[i][j]=this->mt[i][j];
//Dua ma tran ve ma tran tam giac tren
for(int i=0;i<b.soHang-1;i++){
for(int j=i;j<b.soCot;j++){
if(b.mt[i][j]==0)
for(int k=i+1;k<b.soHang;k++)
if(b.mt[k][j]!=0)
for(int u=0;u<b.soCot;u++){
float t=b.mt[i][u];
b.mt[i][u]=b.mt[k][u];
b.mt[k][u]=t;
}
if(b.mt[i][j]!=0){
for(int k=i+1;k<b.soHang;k++){
float tl=b.mt[k][i]/b.mt[i][j];
for(int u=0;u<b.soCot;u++)
b.mt[k][u]-=b.mt[i][u]*tl;
}
break;
}
}
}
//Tim hang
int r=0;
for(int i=b.soHang-1;i>=0;i--){
for(int j=0;j<b.soCot;j++)
if(b.mt[i][j]!=0){
r=i+1;
break;
}
if(r>0) break;
}
return r;
}
void MATRAN::xoaHang(){
int x=0;
do{
cout<<"Nhap vao hang thu x (0<x<"<<this->soHang+1<<") ban muon xoa: ";
cin>>x;
} while(x<1 || x>this->soHang);
MATRAN b(this->soHang-1,this->soCot);
x--;
for(int j=0;j<b.soCot;j++){
for(int i=0;i<x;i++)
b.mt[i][j]=this->mt[i][j];
for(int i=x;i<b.soHang;i++)
b.mt[i][j]=this->mt[i+1][j];
}
cout<<"Ma tran A sau khi xoa hang thu "<<x+1<<" la:";
b.inMaTran();
thaoTac(0);
}
void MATRAN::xoaCot(){
int x=0;
do{
cout<<"Nhap vao cot thu x (0<x<"<<this->soCot+1<<") ban muon xoa: ";
cin>>x;
} while(x<1 || x>this->soCot);
MATRAN b(this->soHang,this->soCot-1);
x--;
for(int i=0;i<b.soHang;i++){
for(int j=0;j<x;j++)
b.mt[i][j]=this->mt[i][j];
for(int j=x;j<b.soCot;j++)
b.mt[i][j]=this->mt[i][j+1];
}
cout<<"Ma tran A sau khi xoa cot thu "<<x+1<<" la:";
b.inMaTran();
thaoTac(0);
}
void MATRAN::congMaTran(){
MATRAN b(this->soHang,this->soCot);
cout<<"Nhap ma tran B:\n";
b.nhapMaTran();
for(int i=0;i<b.soHang;i++)
for(int j=0;j<b.soCot;j++){
b.mt[i][j]+=this->mt[i][j];
}
cout<<"Tong cua 2 ma tran A va B la:";
b.inMaTran();
thaoTac(0);
}
void MATRAN::truMaTran(){
MATRAN b(this->soHang,this->soCot);
cout<<"Nhap ma tran B:\n";
b.nhapMaTran();
for(int i=0;i<b.soHang;i++)
for(int j=0;j<b.soCot;j++){
b.mt[i][j]=this->mt[i][j]-b.mt[i][j];
}
cout<<"Hieu cua 2 ma tran A va B la:";
b.inMaTran();
thaoTac(0);
}
void MATRAN::nhanMT_So(){
float x=1;
MATRAN b(this->soHang,this->soCot);
cout<<"Nhap vao so x: ";
cin>>x;
for(int i=0;i<b.soHang;i++)
for(int j=0;j<b.soCot;j++)
b.mt[i][j]=x*this->mt[i][j];
cout<<"Tich cua "<<x<<" voi ma tran A la:";
b.inMaTran();
thaoTac(0);
}
void MATRAN::nhanMT_MT(){
int cotB=10;
cout<<"Nhap so cot cua ma tran B: ";
cin>>cotB;
MATRAN b(this->soCot,cotB);
cout<<"\nNhap ma tran B:\n";
b.nhapMaTran();
MATRAN c(this->soHang,b.soCot);
for(int i=0;i<c.soHang;i++)
for(int j=0;j<c.soCot;j++){
c.mt[i][j]=0;
for(int k=0;k<this->soCot;k++)
c.mt[i][j]+=this->mt[i][k]*b.mt[k][j];
}
cout<<"Tich cua ma tran A va ma tran B la:";
c.inMaTran();
thaoTac(0);
}
int main(){
int m,n;
cout<<"Nhap so hang cua ma tran A: ";
cin>>m;
cout<<"Nhap so cot cua ma tran A: ";
cin>>n;
MATRAN a(m,n);
cout<<"\nNhap ma tran A:\n";
a.nhapMaTran();
a.thaoTac(0);
return 0;
}