acphuc Binh nhì
Bài gửi : 3 Tổng điêm: : 7 Được cảm ơn: : 0 Tham gia: : 07/12/2010
| Tiêu đề: Mảng hai chiều .bài hoàn chỉnh Wed 15 Dec 2010 - 23:16 | |
| #include #include #include #include #include class mt{ private: int m,n; int a[20][20]; public: friend mt operator+(const mt &,const mt &); friend ostream& operator<<(ostream& os,const mt& d); friend istream& operator>>(istream& is,mt& d); friend mt operator-(const mt &,const mt &); friend mt operator*(const mt &,const mt &); }; ostream& operator<<(ostream& os,const mt& d) {os< for(int i=1;i<=d.m;++i) {os<<"\n"; for(int j=1;j<=d.n;++j) os< } os<<"\n"; return os;} istream& operator>>(istream& is,mt& d) { cout<<"\n --Cap ma tran--\n"; cout<<"\n so cot:"; cin>>d.n; cout<<"\n so hang:"; cin>>d.m; for(int i=1;i<=d.m;++i) for(int j=1;j<=d.n;++j) { cout<<" hang "< is>>d.a[i][j];} return is;} mt operator+(const mt &x1,const mt &x2) { if(x1.m!=x2.m||x1.n!=x2.n) {cout<<"\n Hai ma tran khong cung chieu"; getch();} else { mt x; int i,j; x.n=x1.n; x.m=x1.m; for(i=1;i<=x.m;i++) for(j=1;j<=x.n;j++) x.a[i][j]=x2.a[i][j]+x1.a[i][j]; return x;} } mt operator-(const mt &x1,const mt &x2) { if(x1.m!=x2.m||x1.n!=x2.n) {cout<<"\n Hai ma tran khong cung chieu"; getch();} else { mt x; int i,j; x.n=x1.n; x.m=x1.m; for(i=1;i<=x.m;i++) for(j=1;j<=x.n;j++) x.a[i][j]=x2.a[i][j]-x1.a[i][j]; return x;} } mt operator*(const mt &x1,const mt &x2) {mt x; if(x1.n!=x2.m) {cout<<"\n hai ma tran khong cung cap"; getch();} else { int i,j,n,m; n=x.n=x1.n; m=x.m=x1.m; for(i=1;i<=m;++i) for(j=1;j<=n;++j) { x.a[i][j]=0.0; for(int k=1;k<=n;++k) x.a[i][j]+=x1.a[i][k]*x2.a[k][j];} return x;}} void main() { mt p,q,u,o,w; char c,r; int l; cin>>p; cout<<"\n Ma Tran P"; cout< cin>>q; cout<<"\n Ma Tran Q"; cout< cin>>u; cout<<"\n Ma Tran U"; cout< cin>>o; cout<<"\n Ma Tran O"; cout< cout<<"\n Gia tri cua bieu thuc (Q+P)*(U-O)"; cout<<"\n tong cua hai ma tran "<<(Q+P); cout<<"\n Hieu cua hai ma tran "<<(U-O); cout<<"\n Tich cua hai ma tran "< do{cin>>c;}while(c=='y'||c=='Y'); } | |
|