ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2021-05-14 02:04:57  阅读:173  来源: 互联网

标签:Square matrix int 运算符 IntMatrix square 重载 方阵 cout


题目:
 假设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 }

标签:Square,matrix,int,运算符,IntMatrix,square,重载,方阵,cout
来源: https://www.cnblogs.com/satellite2021/p/14766787.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有