利用二維指針開辟空間形成二維數組;
原題為設計一個Matrix類,實現基本的矩陣運算;
初次設計為HL[10][10]數組,存放矩陣元素,后改為二維指針;
主要問題存在於二維指針理解的不透徹,無法理解其開辟空間的方法;
HL = new double *[row];
for(i = 0;i < row;i++) HL[i] = new double [list];
其次對於不同類型矩陣相加沒有找到合適的處理方式,只能手動控制不使不同類型矩陣相加或相減;
其中 row 為行,list為列,HL為存放矩陣的二維指針;
附上一個new運算符的用法;
https://www.cnblogs.com/2015-16/p/11782595.html
1 #include<iostream> 2 using namespace std; 3 4 class Matrix 5 { 6 private: 7 int row,list; 8 double **HL; 9 public: 10 Matrix(int r_ = 0, int l_ = 0); 11 Matrix(int r_ , int l_ , double **newone); 12 Matrix(const Matrix & rhs); 13 ~Matrix(); 14 Matrix operator + (const Matrix & rhs); 15 Matrix operator - (const Matrix & rhs); 16 Matrix operator = (const Matrix & rhs); 17 friend ostream & operator << (ostream & os , const Matrix & rhs); 18 }; 19 20 int i,j; 21 22 Matrix::Matrix(int r_ , int l_):row(r_),list(l_) //構造函數 23 { 24 HL = new double *[row]; 25 for(i = 0;i < row;i++) 26 HL[i] = new double [list]; 27 cout<<"please enter Matrix :"<<endl; 28 for(i = 0;i < row;i++) 29 for(j = 0;j < list;j++) 30 cin>>HL[i][j]; 31 } 32 33 Matrix::Matrix(int r_ , int l_ , double **newone ):row(r_),list(l_) //構造函數重載,主要用於加法減法中的return使用 34 { 35 HL = new double *[row]; 36 for(i = 0;i < row;i++) 37 HL[i] = new double [list]; 38 for(i = 0;i < row;i++) 39 for(j = 0;j < list;j++) 40 HL[i][j] = newone[i][j]; 41 } 42 43 Matrix::Matrix(const Matrix & rhs) 44 { 45 if(this != & rhs) 46 { 47 this->row = rhs.row; 48 this->list = rhs.list; 49 HL = new double *[row]; 50 for(i = 0;i < row;i++) 51 HL[i] = new double [list]; 52 for(i = 0;i < row;i++) 53 for(j = 0;j < list;j++) 54 this->HL[i][j] = rhs.HL[i][j]; 55 } 56 } 57 58 Matrix::~Matrix() // 析構函數,刪除開辟的空間 59 { 60 cout<<"~ Matrix : row ="<<row<<" , list = "<<list<<endl<<endl; 61 for(i = 0;i < row;i++) 62 delete [] HL[i]; 63 delete [] HL; 64 } 65 66 Matrix Matrix::operator + (const Matrix & rhs) 67 { 68 if( (this->row == rhs.row)&&(this->list == rhs.list) ) 69 { 70 double **newone; 71 int r_,l_; 72 r_ = row;l_ = list; 73 newone = new double *[row]; 74 for(i = 0;i < row;i++) 75 newone[i] = new double [list]; 76 for(i = 0;i < row;i++) 77 for(j = 0;j < list;j++) 78 newone[i][j] = HL[i][j] + rhs.HL[i][j]; 79 return Matrix(r_,l_,newone); 80 } 81 // else 82 // cout<<"error ——矩陣類型不符 "<<endl; 83 } 84 85 Matrix Matrix::operator - (const Matrix & rhs) 86 { 87 if( (this->row == rhs.row)&&(this->list == rhs.list) ) 88 { 89 double **newone; 90 int r_,l_; 91 r_ = row;l_ = list; 92 newone = new double *[row]; 93 for(i = 0;i < row;i++) 94 newone[i] = new double [list]; 95 for(i = 0;i < row;i++) 96 for(j = 0;j < list;j++) 97 newone[i][j] = HL[i][j] - rhs.HL[i][j]; 98 return Matrix(r_,l_,newone); 99 } 100 // else 101 // cout<<"error ——矩陣類型不符 "<<endl; 102 } 103 104 Matrix Matrix::operator = (const Matrix & rhs) 105 { 106 if((this->row == rhs.row)&&(this->list == rhs.list)) 107 { 108 for(i = 0;i < row;i++) 109 for(j = 0;j < list;j++) 110 this->HL[i][j] = rhs.HL[i][j]; 111 return (*this); 112 } 113 // else 114 // cout<<"error ——矩陣類型不符 "<<endl; 115 } 116 117 ostream & operator << (ostream & os,const Matrix & rhs) 118 { 119 os<<"Matrix : row ="<<rhs.row<<" , list = "<<rhs.list<<endl; 120 for(i = 0;i < rhs.row;i++) 121 { 122 for(j = 0;j < rhs.list;j++) 123 os<<rhs.HL[i][j]<<" "; 124 os<<endl; 125 } 126 return os; 127 } 128 129 int main() 130 { 131 int m,n,x,y; 132 cin>>n>>m>>x>>y; 133 Matrix aa(n,m),bb(n,m),cc(n,m),dd(x,y); 134 cout<<endl<<aa<<endl<<bb<<endl<<cc<<endl<<dd<<endl; 135 cout<<(aa+bb+cc)<<endl<<(cc-bb)<<endl; 136 return 0; 137 }
2019-11-02 15:34:51