/*
*copyright(c) 2016,烟台大学计算机学院
*All rights reserved
*文件名称:test.cpp
*作者:谭泽纯
*版本:v6.0
*
*问题描述:
(1)定义一个定义完整的类(是可以当作独立的产品发布,成为众多项目中的“基础工程”)。这样的类在(2)的基础上,扩展+、-、*、/运算符的功能,使之能与double型数据进行运算。设Complex c; double d; c+d和d+c的结果为“将d视为实部为d的复数同c相加”,其他-、*、/运算符类似。
<code class="hljs cpp has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">class</span> Complex { <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span>: Complex(){real=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>;imag=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>;} Complex(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">double</span> r,<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">double</span> i){real=r; imag=i;} Complex <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">operator</span>+(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">const</span> Complex &c2); Complex <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">operator</span>-(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">const</span> Complex &c2); Complex <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">operator</span>*(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">const</span> Complex &c2); Complex <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">operator</span>/(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">const</span> Complex &c2); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> display(); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private</span>: <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">double</span> real; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">double</span> imag; }; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//下面定义成员函数</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//下面定义用于测试的main()函数</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> main() { Complex c1(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4</span>),c2(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5</span>,-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10</span>),c3; <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span><<<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"c1="</span>; c1.display(); <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span><<<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"c2="</span>; c2.display(); c3=c1+c2; <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span><<<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"c1+c2="</span>; c3.display(); c3=c1-c2; <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span><<<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"c1-c2="</span>; c3.display(); c3=c1*c2; <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span><<<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"c1*c2="</span>; c3.display(); c3=c1/c2; <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span><<<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"c1/c2="</span>; c3.display(); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>; }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li><li style="box-sizing: border-box; padding: 0px 5px;">31</li><li style="box-sizing: border-box; padding: 0px 5px;">32</li><li style="box-sizing: border-box; padding: 0px 5px;">33</li><li style="box-sizing: border-box; padding: 0px 5px;">34</li><li style="box-sizing: border-box; padding: 0px 5px;">35</li><li style="box-sizing: border-box; padding: 0px 5px;">36</li><li style="box-sizing: border-box; padding: 0px 5px;">37</li><li style="box-sizing: border-box; padding: 0px 5px;">38</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li><li style="box-sizing: border-box; padding: 0px 5px;">31</li><li style="box-sizing: border-box; padding: 0px 5px;">32</li><li style="box-sizing: border-box; padding: 0px 5px;">33</li><li style="box-sizing: border-box; padding: 0px 5px;">34</li><li style="box-sizing: border-box; padding: 0px 5px;">35</li><li style="box-sizing: border-box; padding: 0px 5px;">36</li><li style="box-sizing: border-box; padding: 0px 5px;">37</li><li style="box-sizing: border-box; padding: 0px 5px;">38</li></ul>
*/
-
- //主函数参考老师
- #include <iostream>
- using namespace std;
- class Complex
- {
- public:
- Complex()
- {
- real=0;
- imag=0;
- }
- Complex(double r,double i)
- {
- real=r;
- imag=i;
- }
- friend Complex operator+(Complex &c1, Complex &c2);
- friend Complex operator+(double d1, Complex &c2);
- friend Complex operator+(Complex &c1, double d2);
- friend Complex operator-(Complex &c1, Complex &c2);
- friend Complex operator-(double d1, Complex &c2);
- friend Complex operator-(Complex &c1, double d2);
- friend Complex operator*(Complex &c1, Complex &c2);
- friend Complex operator*(double d1, Complex &c2);
- friend Complex operator*(Complex &c1, double d2);
- friend Complex operator/(Complex &c1, Complex &c2);
- friend Complex operator/(double d1, Complex &c2);
- friend Complex operator/(Complex &c1, double d2);
- void display();
- private:
- double real;
- double imag;
- };
-
-
- Complex operator+(Complex &c1, Complex &c2)
- {
-
- return Complex(c1.real+c2.real,c1.imag+c2.imag);
- }
- Complex operator+(double d1, Complex &c2)
- {
- return Complex(d1+c2.real,c2.imag);
- }
- Complex operator+(Complex &c1, double d2)
- { return Complex(d2+c1.real,c1.imag);
- }
- Complex operator-(Complex &c1, Complex &c2)
- {
- return Complex(c1.real-c2.real,c1.imag-c2.imag);
- }
- Complex operator-(double d1, Complex &c2)
- {
- return Complex(d1-c2.real,-c2.imag);
- }
- Complex operator-(Complex &c1, double d2)
- {
- return Complex(c1.real-d2,c1.imag);
- }
-
- Complex operator*(Complex &c1, Complex &c2)
- {
- return Complex(c1.real*c2.real-c1.imag*c2.imag,c1.imag*c2.real+c1.real*c2.imag);
- }
- Complex operator*(double d1, Complex &c2)
- {
- return Complex(c2.imag*d1,c2.real*d1);
- }
- Complex operator*(Complex &c1, double d2)
- {
-
- return Complex(c1.imag*d1,c1.real*d1);
- }
-
- Complex operator/(Complex &c1, Complex &c2)
- {
- return Complex((c1.real*c2.real+c1.imag*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag),(c1.imag*c2.real-c1.real*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag));
- }
- Complex operator/(double d1, Complex &c2)
- {
- return Complex (d1/c2.real,c2.imag);
- }
- Complex operator/(Complex &c1, double d2)
- {
- return Complex (c2.real/d2,c2.imag);
- }
-
- void Complex::display()
- {
- cout<<real<<" "<<imag<<endl;
- }
-
- int main()
- {
- Complex c1(3,4),c2(5,-10),c3;
- double d=11;
- cout<<"c1=";
- c1.display();
- cout<<"c2=";
- c2.display();
- cout<<"d="<<d<<endl<<endl;
- cout<<"下面是重载运算符的计算结果: "<<endl;
- c3=c1+c2;
- cout<<"c1+c2=";
- c3.display();
- cout<<"c1+d=";
- (c1+d).display();
- cout<<"d+c1=";
- (d+c1).display();
- c3=c1-c2;
- cout<<"c1-c2=";
- c3.display();
- cout<<"c1-d=";
- (c1-d).display();
- cout<<"d-c1=";
- (d-c1).display();
- c3=c1*c2;
- cout<<"c1*c2=";
- c3.display();
- cout<<"c1*d=";
- (c1*d).display();
- cout<<"d*c1=";
- (d*c1).display();
- c3=c1/c2;
- cout<<"c1/c2=";
- c3.display();
- cout<<"c1/d=";
- (c1/d).display();
- cout<<"d/c1=";
- (d/c1).display();
- return 0;
- }
- return Complex(c1.real+c2.real,c1.imag+c2.imag);