Mình viết chương trình sử dụng các toán tử +,-,*,/ 2 ma trận. Lỗi xảy ra ở toán tử nhân 2 ma trận, khi nhân 2 ma trận từ cấp 3 trở lên là bị lỗi. Còn cấp 1, 2 vẫn bình thường. Các bạn xem giúp.
Code:
class MT { int m, n; int **a; public: MT() { m=0; n=0; a=NULL;} // hàm tạo mặc định MT( int m1, int n1, int x=0); // hàm tạo có tham số MT( const MT &); // hàm tạo chép const MT & operator = (const MT &); // toán tử gán friend MT operator + ( MT &, MT &); friend MT operator * ( MT &, MT &); friend int operator == (MT &, MT &); friend istream & operator >> ( istream &, MT &); friend ostream & operator << ( ostream &, MT &); void xoabonho(); ~MT(); }; MT::MT( int m1, int n1, int x) { m= m1; n= n1; a= new int *[m]; for( int i=0; i< m; i++) a[i]= new int [n]; for( int i=0; i<m; i++) for( int j=0; j< n; j++) a[i][j]=x; } MT::MT( const MT &A) { m= A.m; n= A.n; a= new int *[m]; if( a==NULL) cout<<"\nKhong du bo nho."; for( int i=0; i< m; i++) a[i]= new int [n]; for ( int i=0; i< m; i++) for( int j=0; j< n; j++) a[i][j]= A.a[i][j]; } void MT::xoabonho() { if(a) { for( int i=0; i< m; i++) delete []a[i]; delete []a; } } MT::~MT() { xoabonho(); } const MT & MT::operator = ( const MT &B) { if( m != B.m || n != B.n) { xoabonho(); m= B.m; n=B.n; a= new int *[m]; if( a==NULL) cout<<"\nKhong du bo nho."; for( int i=0; i< m; i++) a[i]= new int [n]; } for( int i=0; i< m; i++) for( int j=0; j< n; j++) a[i][j]= B.a[i][j]; return B; } MT operator + ( MT &B1, MT &B2) { if( B1.m != B2.m || B1.n != B2.n) { cout<<"\nKhong cong duoc."; //exit(1); } MT A( B1.m, B1.n, 0); A.a = new int *[B1.m]; for( int i=0; i< A.m; i++) A.a[i]= new int [B1.n]; for( int i=0; i< A.m; i++) for( int j=0; j< A.n; j++) A.a[i][j]= B1.a[i][j] + B2.a[i][j]; return A; } MT operator * ( MT &B1, MT &B2) { if( B1.n != B2.m) { cout<<"\nKhong nhan duoc."; //exit(1); } MT A( B1.m, B2.n, 0); A.a= new int *[B1.m]; for( int i=0; i< A.m; i++) A.a[i]= new int [B2.n]; for( int i=0; i< A.m; i++) for( int j=0; j< A.n; j++) { A.a[i][j] = 0; for( int k=0; k< A.n; k++) A.a[i][j] += B1.a[i][k] * B2.a[k][j]; } return A; } int operator == ( MT &B1, MT &B2) { int OK=1; if( B1.m != B2.m || B1.n != B2.n) { OK=0; } else { for( int i=0; i< B1.m; i++) { for( int j=0; j< B1.n; j++) if( B1.a[i][j] != B2.a[i][j]) OK=0; } } return OK; } istream & operator >> ( istream & nhap, MT &A) { do { cout<<"\nSo hang: "; cin>> A.m;} while( A.m < 1 || A.m > 10); do { cout<<"So cot: "; cin>> A.n;} while( A.n < 1 || A.n > 10); A.a= new int *[A.m]; for( int i=0; i< A.m; i++) A.a[i]= new int [A.n]; for( int i=0; i< A.m; i++) for( int j=0; j< A.n; j++) { cout<<"Nhap phan tu a["<<i<<"]["<<j<<"] : "; cin>>A.a[i][j]; } return nhap; } ostream & operator << ( ostream & xuat, MT &A) { for( int i=0; i< A.m; i++) { for( int j=0; j< A.n; j++) cout<<" "<<setw(4)<<A.a[i][j]; cout<<"\n"; } return xuat; } void menu() { cout<<"\t\t MENU"; cout<<"\n\t1. Nhap, xuat ma tran"; cout<<"\n\t2. Cong 2 ma tran"; cout<<"\n\t3. So sanh 2 ma tran"; cout<<"\n\t4. Nhan 2 ma tran"; cout<<"\n\t5. Gan ma tran"; cout<<"\n\t0. Thoat"; } void main() { int chon; MT A,B,C; while(1) { do { menu(); cout<<"\nChon chuc nang: "; cin>>chon; } while( chon < 0 || chon > 5); switch(chon) { case 1: { cout<<"\nNhap ma tran:"; cin>>A; cout<<"\nXuat ma tran:\n"; cout<<A; break; } case 2: { cout<<"\nNhap ma tran thu 2:"; cin>>B; cout<<"Ma tran tong: \n"; cout<<A+B; break; } case 3: { cout<<"\nSo sanh 2 ma tran: "; if( A==B) cout<<"2 ma tran bang nhau.\n"; else cout<<"2 ma tran ko bang nhau.\n"; break; } case 4: { cout<<"\nNhap ma tran thu 2:"; cin>>B; cout<<"\nNhan 2 ma tran. Ma tran tich la: \n"; cout<<A*B; break; } case 5: { cout<<"\nGan ma tran A cho C. Ma tran C:\n"; C=A; cout<<C; break; } default: { exit(1); break;} } getch(); } }