C++面向程序設計(第二版)課后習題答案解析


最近沒什么心情整理零散的知識點,就整理一下第四章的課后習題答案。

1.定義一個復數類Complex,重載運算符“+”,使之能用於復數的加法運算。將運算符函數重載為非成員函數,非友元的普通函數。編程序,求兩個復數之和。

源代碼:

 1 #include <iostream>
 2 #include<stdlib.h>
 3 using namespace std;
 4 class Complex
 5 {public:
 6     Complex(){real=0;imag=0;}
 7     Complex(double r,double i){real=r;imag=i;}
 8     double get_real();//獲取實部函數
 9     double get_imag();//獲取虛部函數
10     void display();//顯示函數
11 private:
12     double real;
13     double imag;
14 };
15 
16 //實現具體的函數
17 double Complex::get_real()
18 {
19     return real;
20 }
21 double Complex::get_imag()
22 {
23     return imag;
24 }
25 void Complex::display()
26 {
27     cout<<"("<<real<<","<<imag<<"i)"<<endl;
28 }
29 //重載運算符“+”
30 Complex operator + (Complex &c1,Complex &c2)
31 {
32     return Complex(c1.get_real()+c2.get_real(),c1.get_imag()+c2.get_imag());
33 }
34 
35 
36 int main()
37 {
38     Complex c1(3,4),c2(5,-10),c3;
39     c3=c1+c2;
40     cout<<"c3=";
41     c3.display();
42     system("pause");
43     return 0;
44 }

2.定義一個復數類Complex,重載運算符“+”,“-”,“*”,“/”,使之能用於復數的加,減,乘,除。運算符重載函數作為Complex類的成員函數。編程序,分別求兩個復數之和、差、積和商。

源程序:

 1 #include<iostream>
 2 #include<stdlib.h>
 3 using namespace std;
 4 class Complex
 5 {
 6 public:
 7     Complex(){real=0;imag=0;}
 8     Complex(double r,double i){real=r;imag=i;}
 9     //重載函數:加、減、乘、除
10     Complex operator+(Complex &c2);
11     Complex operator-(Complex &c2);
12     Complex operator*(Complex &c2);
13     Complex operator/(Complex &c2);
14     void display();
15 private:
16     double real;
17     double imag;
18 };
19 Complex Complex::operator+(Complex &c2)
20 {
21     Complex c; 
22     c.real=real+c2.real;
23     c.imag=imag+c2.imag;
24     return c;
25 }
26 Complex Complex::operator-(Complex &c2)
27 {
28     Complex c;
29     c.real=real-c2.real;
30     c.imag=imag-c2.imag;
31     return c;
32 }
33 Complex Complex::operator* (Complex &c2)
34 {
35     Complex c;
36     c.real=real*c2.real-imag* c2.imag;
37     c.imag=imag* c2.real+real*c2.imag;
38     return c;
39 }
40 Complex Complex::operator/(Complex &c2)
41 {
42     Complex c; 
43     c.real=(real*c2.real+imag* c2.imag)/(c2.real*c2.real+c2.imag* c2.imag);
44     c.imag=(imag* c2.real-real*c2.imag)/(c2.real*c2.real+c2.imag* c2.imag);
45     return c;
46 }
47 void Complex::display()
48 {
49     cout<<"("<<real<<","<<imag<<"i)"<<endl;
50 }
51 
52  int main()
53 {
54     Complex c1(3,4),c2(5,-10),c3;
55     c3=c1+c2;
56     cout<<"c1+c2=";
57     c3.display();
58     c3=c1-c2;
59     cout<<"c1-c2=";
60     c3.display();
61     c3=c1*c2;
62     cout<<"c1*c2=";
63     c3.display();
64     c3=c1/c2;
65     cout<<"c1/c2=";
66     c3.display();
67     system("pause");
68     return 0;
69  }

3.定義一個復數類函數Complex,重載運算符“+”,使之能用於復數的加法運算。參加運算的兩個運算量都可以是類對象,也可以其中有一個是整數,順序任意。如c1+c2,i+c1,c1+i均合法(設i為整數,c1,c2為復數)。編程序,分別求兩個復數之和、整數和復數之和。

源代碼:

 1 #include <iostream>
 2 #include<stdlib.h>
 3 using namespace std;
 4 class Complex
 5 {
 6 public:
 7     Complex(){real=0;imag=0;}
 8     Complex(double r,double i){real=r;imag=i;}
 9     //定義重載復數加法
10     Complex operator+(Complex &c2);
11     //定義重載整數加法
12     Complex operator+(int &i);
13     //定義友元類重載復數、整數加法函數
14     friend Complex operator+(int&,Complex &);
15     void display();
16 private:
17     double real;
18     double imag;
19 };
20 
21  Complex Complex::operator+(Complex &c)
22 {
23     return Complex(real+c.real,imag+c.imag);
24  }
25 Complex Complex::operator+(int &i)
26 {
27     return Complex(real+i,imag);
28 }
29 void Complex::display()
30 {
31     cout<<"("<<real<<","<<imag<<"i)"<<endl;
32 }
33 Complex operator+(int &i,Complex &c)
34 {
35     return Complex(i+c.real,c.imag);
36 }
37 
38  int main()
39 {
40     Complex c1(3,4),c2(5,-10),c3;
41     int i=5;
42     c3=c1+c2;
43     cout<<"c1+c2=";
44     c3.display();
45     c3=i+c1;
46     cout<<"i+c1=";
47     c3.display();
48     c3=c1+i;
49     cout<<"c1+i=";
50     c3.display();
51     system("pause");
52     return 0;
53 }

4.有兩個矩陣a和b,均為2行3列。求兩個矩陣之和,重載運算符“+”,使之能用於矩陣相加。如c=a+b。

源代碼:

 1 #include <iostream>
 2 #include<stdlib.h>
 3 using namespace std;
 4 class Matrix
 5 {
 6 public:
 7     Matrix();
 8     //定義友元類加法重載函數
 9     friend Matrix operator+(Matrix &,Matrix &);
10     //定義矩陣輸入函數
11     void input();
12     //定義矩陣輸出函數
13     void display();
14 private:
15 //定義兩行三列的矩陣
16     int mat[2][3];
17 };
18 Matrix::Matrix()
19 {
20     for(int i=0;i<2;i++)
21     {
22         for(int j=0;j<3;j++)
23         {
24             mat[i][j]=0;
25         }
26     
27     }
28     
29 }
30 //矩陣加法運算函數
31 Matrix operator+(Matrix &a,Matrix &b)
32 {
33     Matrix c;
34     for(int i=0;i<2;i++)
35     {
36         for(int j=0;j<3;j++)
37         {
38         c.mat[i][j]=a.mat[i][j]+b.mat[i][j];
39         }
40     }
41     
42     return c;
43 }
44 //矩陣控制台輸入具體數據函數
45 void Matrix::input()
46 {
47     cout<<"input value of matrix:"<<endl;
48     for(int i=0;i<2;i++)
49     {
50         for(int j=0;j<3;j++)
51         {
52             cin>>mat[i][j];
53         }
54     
55     }
56     
57 }
58 //顯示矩陣數據函數
59  void Matrix::display()
60 {
61     for (int i=0;i<2;i++)
62     {
63         for(int j=0;j<3;j++)
64         {
65             cout<<mat[i][j]<<" ";
66         }
67         cout<<endl;
68     }
69 }
70 int main()
71 {    
72     Matrix a,b,c;
73     a.input();
74     b.input();
75     cout<<endl<<"Matrix a:"<<endl;
76     a.display();
77     cout<<endl<<"Matrix b:"<<endl;
78     b.display();
79     c=a+b; 
80     cout<<endl<<"Matrix c = Matrix a + Matrix b :"<<endl;
81     c.display();
82     system("pause");
83     return 0;
84 }

5.在第四題的基礎上,重載流插入運算符“<<”和流提取運算符“>>”,使之能用於矩陣的輸入和輸出。

源程序:

 1 #include <iostream>
 2 #include<stdlib.h>
 3 using namespace std;
 4 class Matrix
 5 {
 6 public:
 7     Matrix();
 8     //定義重載運算符“+”函數
 9     friend Matrix operator+(Matrix &,Matrix &);
10     //定義重載流插入運算符“<<”函數
11     friend ostream& operator<<(ostream&,Matrix&);
12     //定義重載流提取運算符“>>”函數
13     friend istream& operator>>(istream&,Matrix&);
14 private:
15 //定義2行3列矩陣
16     int mat[2][3];
17 };
18 //初始化矩陣
19 Matrix::Matrix()
20 {
21     for(int i=0;i<2;i++)
22     {
23         for(int j=0;j<3;j++)
24         {
25             mat[i][j]=0;
26         }
27         
28     }
29 }
30 //重載運算符“+”函數
31 Matrix operator+(Matrix &a,Matrix &b)
32 {
33     Matrix c;
34     for(int i=0;i<2;i++)
35     {
36         for(int j=0;j<3;j++)
37         {
38             c.mat[i][j]=a.mat[i][j]+b.mat[i][j];
39     
40         }
41     }
42     
43     return c; 
44 }
45 
46 //重載流提取運算符“>>”函數,輸入矩陣數據
47 istream& operator>>(istream &in,Matrix &m)
48 {
49     cout<<"input value of matrix:"<<endl;
50     for(int i=0;i<2;i++)
51     {
52         for(int j=0;j<3;j++)
53         {
54              in>>m.mat[i][j];
55         }
56        
57     }
58     
59     return in;
60 }
61 //重載流插入運算符“<<”函數,輸出矩陣數據
62 ostream& operator<<(ostream &out,Matrix &m)
63 {
64     for (int i=0;i<2;i++)
65     {for(int j=0;j<3;j++)
66     {
67         out<<m.mat[i][j]<<" ";
68     }
69     out<<endl;
70     }
71     return out;
72 }
73 int main()
74 {
75     Matrix a,b,c;
76     cin>>a;
77     cin>>b;
78     cout<<endl<<"Matrix a:"<<endl<<a<<endl;
79     cout<<endl<<"Matrix b:"<<endl<<b<<endl;
80     c=a+b; 
81     cout<<endl<<"Matrix C = Matrix a + Matrix b :"<<endl<<c<<endl;
82     system("pause");
83     return 0;
84 }

6.請編寫程序,處理一個復數與一個double數相加的運算,結果存放在一個double型的變量dl中,輸出dl的值,再以復數的形式輸出此值。定義Complex(復數)類,在成員函數中包含重載類型轉換運算符:

operator double() {return real;}

源代碼:

 1 #include <iostream>
 2 #include<stdlib.h>
 3 using namespace std;
 4 class Complex
 5 {
 6 public:
 7     Complex(){real=0;imag=0;}
 8     Complex(double r){real=r;imag=0;}
 9     Complex(double r,double i){real=r;imag=i;}
10     //重載類型轉換運算符函數
11     operator double(){return real;}
12     void display();
13 private:
14     double real;
15     double imag;
16 };
17 void Complex::display()
18 {
19     cout<<"("<<real<<", "<<imag<<")"<<endl;
20 }
21 int main()
22 {
23     Complex c1(3,4),c2;
24     double d1;
25     d1=2.5+c1;
26     cout<<"d1="<<d1<<endl;
27     c2=Complex(d1);
28     cout<<"c2=";
29     c2.display();
30     system("pause");
31     return 0;
32 }

7.擴展:有兩個3行3列的矩陣,求兩個矩陣的和、差、積,重載運算符“+”,“-”,“*”。

源代碼:

  1 #include <iostream>
  2 #include<stdlib.h>
  3 using namespace std;
  4 class Matrix
  5 {
  6 public:
  7     Matrix();
  8     //定義重載加法運算函數
  9     friend Matrix operator+(Matrix &,Matrix &);
 10     //定義重載減法運算函數
 11     friend Matrix operator-(Matrix &,Matrix &);
 12     //定義重載乘法運算函數
 13     friend Matrix operator*(Matrix &,Matrix &);
 14     void input();
 15     void display();
 16 private:
 17     //定義3行3列矩陣
 18     int mat[3][3];
 19 };
 20 //初始化矩陣
 21 Matrix::Matrix()
 22 {
 23     for(int i=0;i<3;i++)
 24     {
 25         for(int j=0;j<3;j++)
 26         {
 27             mat[i][j]=0;
 28         }
 29     
 30     }
 31     
 32 }
 33 //重載加法運算函數,
 34 Matrix operator+(Matrix &a,Matrix &b)
 35 {
 36     Matrix c;
 37     for(int i=0;i<3;i++)
 38     {
 39         for(int j=0;j<3;j++)
 40         {
 41             //矩陣加法為行列號相同的元素相加為該位置的數據
 42             c.mat[i][j]=a.mat[i][j]+b.mat[i][j];
 43         }
 44     }
 45     
 46     return c;
 47 }
 48 //重載減法運算函數,矩陣減法為行列號相同的元素相減為該位置的數據
 49 Matrix operator-(Matrix &a,Matrix &b)
 50 {
 51     Matrix c;
 52     for(int i=0;i<3;i++)
 53     {
 54         for(int j=0;j<3;j++)
 55         {
 56         c.mat[i][j]=a.mat[i][j]-b.mat[i][j];
 57         }
 58     }
 59     
 60     return c;
 61 }
 62 //重載乘法運算函數,第一個矩陣的第i行的第一個元素,逐一與第二個矩陣的i行的j列上的所有元素之積的和,為該i行j列的數據
 63 Matrix operator*(Matrix &a,Matrix &b)
 64 {
 65     //int mat;
 66     Matrix c;
 67     for(int i=0;i<3;i++)
 68     {
 69         for(int j=0;j<3;j++)
 70         {
 71             //矩陣乘法運算的核心步驟,k為控制列乘的數據
 72             for(int k=0;k<3;k++)
 73             c.mat[i][j]=c.mat[i][j]+a.mat[i][k]*b.mat[k][j];
 74         }
 75     }
 76     
 77     return c;
 78 }
 79 //矩陣輸入函數
 80 void Matrix::input()
 81 {
 82     cout<<"input value of matrix:"<<endl;
 83     for(int i=0;i<3;i++)
 84     {
 85         for(int j=0;j<3;j++)
 86         {
 87             cin>>mat[i][j];
 88         }
 89     
 90     }
 91     
 92 }
 93 //矩陣輸出函數
 94  void Matrix::display()
 95 {
 96     for (int i=0;i<3;i++)
 97     {
 98         for(int j=0;j<3;j++)
 99         {
100             cout<<mat[i][j]<<" ";
101         }
102         cout<<endl;
103     }
104 }
105 int main()
106 {    
107     Matrix a,b,c;
108     a.input();
109     b.input();
110     cout<<endl<<"Matrix a:"<<endl;
111     a.display();
112     cout<<endl<<"Matrix b:"<<endl;
113     b.display();
114     c=a+b; 
115     cout<<endl<<"Matrix c = Matrix a + Matrix b :"<<endl;
116     c.display();
117     c=a-b; 
118     cout<<endl<<"Matrix c = Matrix a - Matrix b :"<<endl;
119     c.display();
120     c=a*b; 
121     cout<<endl<<"Matrix c = Matrix a * Matrix b :"<<endl;
122     c.display();
123     system("pause");
124     return 0;
125 }

小結:

熟練掌握復數運算,重載運算符(+,-,*,/,<<,>>)函數的使用方法,矩陣的輸入及輸出函數封裝,矩陣的加、減和乘法的運算。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM