练习:实现3*3矩阵类,支持下面操作符重载
+ - += -= -(负) 前++、-- 后++、-- <<(输出)
* *=
提示:
1 2 3 9 8 7 10 10 10
4 5 6 + 6 5 4 = 10 10 10
7 8 9 3 2 1 10 10 10
--------------------------------
1 2 3 9 8 7 -8 -6 -4
4 5 6 - 6 5 4 = 2 0 2
7 8 9 3 2 1 4 6 8
--------------------------------
1 2 3 9 8 7 30 24 18
4 5 6 * 6 5 4 = 84 69 54
7 8 9 3 2 1 138 114 90
#include <iostream>
using namespace std;
class M33{
public:
M33(void){
for(int i=0;i<3;i++)
for(int j = 0;j<3;j++)
m_a[i][j] = 0;
}
M33(int a[][3]){
for(int i=0;i<3;i++)
for(int j = 0;j<3;j++)
m_a[i][j] = a[i][j];
}
//重载输出操作符:<<
friend ostream &operator<<(ostream &os,const M33 &m){
for(int i=0;i<3;i++){
for(int j = 0;j<3;j++){
cout<<m.m_a[i][j]<<" ";
}
cout<<endl;
}
return os;
}
//重载双目计算操作符"+":
const M33 operator+(const M33& m)const{
M33 res;
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
res.m_a[i][j] = m_a[i][j] + m.m_a[i][j];
}
}
return res;
}
//重载双目计算操作符"-":
friend const M33 operator-(const M33& m1,const M33& m2){
M33 res;
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
res.m_a[i][j] =m1.m_a[i][j] - m2.m_a[i][j];
}
}
return res;
}
//重载双目赋值运算符“+=”:
M33& operator+=(const M33& m){
for(int i = 0;i<3;i++){
for(int j=0;j<3;j++){
m_a[i][j]= m_a[i][j] + m.m_a[i][j];
}
}
return *this;
}
//重载双目赋值运算符"-=":
friend M33& operator-=( M33& m1,const M33&m2){
for(int i = 0;i<3;i++){
for(int j=0;j<3;j++){
m1.m_a[i][j]= m1.m_a[i][j] - m2.m_a[i][j];
}
}
return m1;
}
//重载单目运算符“-”取反:
const M33 operator-(void)const{
M33 res;
for(int i = 0;i<3;i++){
for(int j=0;j<3;j++){
res.m_a[i][j]= -m_a[i][j];
}
}
return res;
}
//重载前++:
M33& operator++(void){
for(int i = 0;i<3;i++){
for(int j=0;j<3;j++){
m_a[i][j]= m_a[i][j]+1;
}
}
return *this;
}
//重载前--:
friend M33& operator--(M33& m){
for(int i = 0;i<3;i++){
for(int j=0;j<3;j++){
m.m_a[i][j]= m.m_a[i][j]-1;
}
}
return m;
}
//重载后++:
const M33 operator++(int){
M33 old;
for(int i = 0;i<3;i++){
for(int j=0;j<3;j++){
old.m_a[i][j]= m_a[i][j];
}
}
for(int i = 0;i<3;i++){
for(int j=0;j<3;j++){
m_a[i][j]= m_a[i][j]+1;
}
}
return old;
}
//重载后--
friend const M33 operator--(M33& m,int){
M33 old;
for(int i = 0;i<3;i++){
for(int j=0;j<3;j++){
old.m_a[i][j]= m.m_a[i][j];
}
}
for(int i = 0;i<3;i++){
for(int j=0;j<3;j++){
m.m_a[i][j]= m.m_a[i][j]-1;
}
}
return old;
}
//重载双目运算符“*”:
const M33 operator*(const M33&m)const{
M33 res;
for(int i=0;i<3;i++){
for(int j = 0;j<3;j++){
for(int k = 0;k<3;k++){
res.m_a[i][j]+= m_a[i][k]*m.m_a[k][j];
}
}
}
return res;
}
//重载双目赋值运算符"*=":
friend M33& operator*=(M33& m1,const M33& m2){
M33 old(m1);
for(int i=0;i<3;i++){
for(int j = 0;j<3;j++){
m1.m_a[i][j]=0;
for(int k = 0;k<3;k++){
m1.m_a[i][j] += old.m_a[i][k] * m2.m_a[k][j];
}
}
}
return m1;
}
private:
int m_a[3][3];
};
int main(void){
int arr1[3][3] = {1,2,3,4,5,6,7,8,9};
int arr2[3][3] = {9,8,7,6,5,4,3,2,1};
M33 m1(arr1);
M33 m2(arr2);
M33 m3 = m1+m2;
cout << m1 <<endl;
cout << m2 <<endl;
cout<<"m1+m2"<<endl;
cout << m3 <<endl;
M33 m4 = m2 - m1;
cout<<"m2-m1"<<endl;
cout<<m4<<endl;
M33 m5;
cout <<"m5"<<endl;
cout <<m5<<endl;
m5 += m1;
cout<<"m5+=m1"<<endl;
cout<<m5<<endl;
m5 -=m1;
cout<<"m5-=m1"<<endl;
cout<<m5<<endl;
M33 m6 = -m1;
cout<<"-m1"<<endl;
cout<<m6<<endl;
cout<<"++m5"<<endl;
cout<<++m5<<endl;
cout<<"--m5"<<endl;
cout<<--m5<<endl;
cout<<"m5++"<<endl;
cout<<m5++<<endl;
cout<<"m5"<<endl;
cout<<m5<<endl;
cout<<"m5--"<<endl;
cout<<m5--<<endl;
cout<<"m5"<<endl;
cout<<m5<<endl;
M33 m7 = m1*m2;
cout<<"m1*m2"<<endl;
cout <<m7<<endl;
cout <<"m1*=m2"<<endl;
cout <<(m1*=m2)<<endl;
return 0;
}