首页 > 其他 > 详细

实验——运算符重载(方阵和单位阵的混合运算)

时间:2021-05-14 10:02:01      阅读:21      评论:0      收藏:0      [点我收藏+]
题目:
 假设square_matrix是n阶整型方阵,请实现下列运算:

(1)cin>> square_matrix

(2)cout<< square_matrix

(3)IntMatrix(n)生成一个n阶单位矩阵

(4)square_matrix+IntMatrix(n)

(5)square_matrix_A=square_matrix_B


源代码:

1
//matrix.h 2 #include<iostream> 3 using namespace std; 4 5 class Square_matrix; 6 7 class IntMatrix{ 8 public: 9 IntMatrix(int inputn) 10 { 11 n=inputn; 12 int i,j; 13 q=new int *[n]; 14 for(i=0;i<n;i++) 15 q[i]=new int[n]; 16 for(i=0;i<n;i++) 17 { 18 for(j=0;j<n;j++) 19 { 20 if(i==j) 21 q[i][j]=1; 22 else 23 q[i][j]=0; 24 } 25 } 26 //cout<<"define a "<<n<<" order int matrix."<<endl; 27 } 28 friend class Square_matrix; 29 friend Square_matrix operator+(const Square_matrix &,const IntMatrix &); 30 ~IntMatrix() 31 { 32 int i,j; 33 for(i=0;i<n;i++) 34 delete []q[i]; 35 delete []q; 36 cout<<"the intmatrix is deleted!"<<endl; 37 } 38 private: 39 int n; 40 int **q; 41 }; 42 43 class Square_matrix{ 44 public: 45 Square_matrix(int inputn) 46 { 47 n=inputn; 48 p=new int *[n]; 49 int i,j; 50 for(i=0;i<n;i++) 51 p[i]=new int[n]; 52 //cout<<"you have define a "<<n<<" order matrix."<<endl; 53 } 54 friend ostream &operator<<(ostream &,const Square_matrix &); 55 friend istream &operator>>(istream &,Square_matrix &); 56 Square_matrix & operator=(const Square_matrix & m) 57 { 58 if(this==&m) return *this; 59 int i,j; 60 delete p; 61 for(i=0;i<n;i++) 62 p[i]=new int[m.n]; 63 n=m.n; 64 for(i=0;i<n;i++) 65 for(j=0;j<n;j++) 66 p[i][j]=m.p[i][j]; 67 return *this; 68 } 69 friend Square_matrix operator+(const Square_matrix &,const IntMatrix &); 70 ~Square_matrix() 71 { 72 int i,j; 73 for(i=0;i<n;i++) 74 delete []p[i]; 75 delete []p; 76 cout<<"the matrix is deleted!"<<endl; 77 } 78 private: 79 int **p; 80 int n; 81 }; 82 83 istream &operator>> (istream & input,Square_matrix &m) 84 { 85 int i,j; 86 int mn=m.n; 87 //cout<<"the order of the matrix is:"<<mn<<endl; 88 for(i=0;i<mn;i++) 89 { 90 for(j=0;j<mn;j++) 91 { 92 input>>m.p[i][j]; 93 } 94 } 95 return input; 96 } 97 98 ostream &operator<< (ostream & output,const Square_matrix &m) 99 { 100 int i,j; 101 int mn=m.n; 102 for(i=0;i<mn;i++) 103 { 104 for(j=0;j<mn;j++) 105 output<<m.p[i][j]<<" "; 106 output<<endl; 107 } 108 return output; 109 } 110 111 Square_matrix operator+(const Square_matrix &m1,const IntMatrix &m2) 112 { 113 int n=m1.n; 114 Square_matrix mt(n); 115 int i,j; 116 for(i=0;i<n;i++) 117 for(j=0;j<n;j++) 118 mt.p[i][j]=m1.p[i][j]+m2.q[i][j]; 119 return mt; 120 };
 1 //main.cpp
 2 #include<iostream>
 3 #include "matrix.h"
 4 
 5 using namespace std;
 6 
 7 int main()
 8 {
 9 //    const int n=2;
10     int n;
11     cout<<"the order of the Square Matrix:";
12     cin>>n;
13     Square_matrix m0(n);
14     cout<<"Input the elements:"<<endl;
15     cin>>m0;
16     cout<<"m0:"<<endl;
17     cout<<m0;
18     Square_matrix m1(n);
19     cout<<"m1=m0"<<endl<<"m1:"<<endl;
20     m1=m0;
21     cout<<m1;
22     Square_matrix m3(n);
23     m3=m0+IntMatrix(n);
24     cout<<"m3=m0+Intmatrix("<<n<<")"<<endl<<"m3:"<<endl;
25     cout<<m3;
26     return 0;
27 }

技术分享图片

实验——运算符重载(方阵和单位阵的混合运算)

原文:https://www.cnblogs.com/satellite2021/p/14766787.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!