最近没什么心情整理零散的知识点,就整理一下第四章的课后习题答案。
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 }
小结:
熟练掌握复数运算,重载运算符(+,-,*,/,<<,>>)函数的使用方法,矩阵的输入及输出函数封装,矩阵的加、减和乘法的运算。