第九周实验报告一

/* (程序头部注释开始)
* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生 
* All rights reserved.
* 文件名称:                              
* 作    者: 鲍增凯                          
* 完成日期:     2012    年   5  月    7   日

* 版 本 号:

01.#include <iostream>     
02.using namespace std;    
03.class Complex    
04.{    
05.public:    
06.    Complex(){real=0;imag=0;}   
07.  
08.    Complex(double r,double i){real=r;imag=i;}  
09.  
10.  
11.    friend Complex operator+(Complex &c1,Complex &c2);                   
12.  
13.    friend Complex operator-(Complex &c1,Complex &c2);   
14.  
15.    friend Complex operator*(Complex &c1,Complex &c2);   
16.  
17.    friend Complex operator/(Complex &c1,Complex &c2);   
18.  
19.  
20.    friend Complex operator+(Complex &c1,double d);   
21.  
22.    friend Complex operator+(double d,Complex &c1);    
23.  
24.    friend Complex operator-(Complex &c1,double d);   
25.  
26.    friend Complex operator-(double d,Complex &c1);    
27.  
28.    friend Complex operator*(Complex &c1,double d);    
29.  
30.    friend Complex operator*(double d,Complex &c1);    
31.  
32.    friend Complex operator/(Complex &c1,double d);    
33.  
34.    friend Complex operator/(double d,Complex &c1);    
35.  
36.  
37.    Complex operator-();   
38.  
39.    void display();    
40.  
41.    friend ostream & operator <<(ostream &,Complex &);              //友元函数声明输出流   
42.   
43.    friend istream & operator >>(istream &,Complex &);              //友元函数声明输入流   
44.private:    
45.    double real;    
46.    double imag;    
47.};    
48.//下面定义成员函数     
49.Complex operator+(Complex &c1,Complex &c2)    
50.{    
51.    return Complex(c1.real+c2.real,c1.imag+c2.imag);    
52.}    
53.    
54.Complex operator-(Complex &c1,Complex &c2)    
55.{    
56.    return Complex(c1.real-c2.real,c1.imag-c2.imag);    
57.}    
58.    
59.Complex operator*(Complex &c1,Complex &c2)    
60.{    
61.    Complex c;    
62.    c.real=c1.real*c2.real-c1.imag*c2.imag;    
63.    c.imag=c1.real*c2.imag-c1.imag*c2.real;    
64.    return c;    
65.}    
66.    
67.Complex operator/(Complex &c1,Complex &c2)    
68.{     
69.    Complex c;    
70.    c.real=(c1.real*c2.real+c1.imag*c2.imag)/(c2.imag*c2.imag+c2.real*c2.real);    
71.    c.imag=(c1.imag*c2.real-c1.real*c2.imag)/(c2.imag*c2.imag+c2.real*c2.real);    
72.    return c;    
73.}    
74.    
75.Complex operator+(Complex &c1,double d)    
76.{    
77.    return Complex(c1.real+d,c1.imag);    
78.}    
79.    
80.Complex operator+(double d,Complex &c1)    
81.{    
82.    return Complex(c1.real+d,c1.imag);    
83.}    
84.    
85. Complex operator-(Complex &c1,double d)    
86. {    
87.     return Complex(c1.real-d,c1.imag);    
88. }    
89.    
90. Complex operator-(double d,Complex &c1)    
91.{    
92.     return Complex(c1.real-d,c1.imag);    
93. }    
94.    
95. Complex operator*(Complex &c1,double d)    
96. {    
97.     return Complex(c1.real*d,c1.imag*d);    
98. }    
99.    
100. Complex operator*(double d,Complex &c1)    
101. {    
102.     return Complex(c1.real*d,c1.imag*d);    
103. }    
104.    
105. Complex operator/(Complex &c1,double d)    
106. {    
107.     return Complex(c1.real/d,c1.imag/d);    
108. }    
109.    
110. Complex operator/(double d,Complex &c1)    
111. {    
112.     return Complex(c1.real/d,c1.imag/d);    
113. }    
114.    
115. Complex Complex:: operator-()    
116. {    
117.     return Complex(-real,-imag);    
118. }    
119.    
120.   
121.   
122. ostream & operator <<(ostream & output,Complex &c)  
123. {  
124.     output<<"("<<c.real;  
125.     if(c.imag>=0)output<<"+";  
126.     output<<c.imag<<"i)"<<endl;  
127.     return output;  
128. }  
129.  
130. istream & operator >>(istream & input,Complex &c)  
131. {  
132.     cout<<"请输入:"<<endl;  
133.     input>>c.real>>c.imag;  
134.  
135.     return input;  
136. }  
137.  
138.int main()    
139.{    
140.    Complex c1,c2,c3;    
141.    double d=3;   
142.    cin>>c1;  
143.    cout<<"c1="<<c1<<endl;    
144.    
145.    cin>>c2;  
146.    cout<<"c2="<<c2<<endl;      
147.    
148.    c3=c1+c2;    
149.    cout<<"c1+c2="<<c3<<endl;      
150.    
151.    c3=c1-c2;    
152.    cout<<"c1-c2="<<c3<<endl;     
153.    
154.    c3=c1*c2;    
155.    cout<<"c1*c2="<<c3<<endl;    
156.    
157.    c3=c1/c2;    
158.    cout<<"c1/c2="<<c3<<endl;    
159.  
160.    c3=c1+d;    
161.    cout<<"c1+d="<<c3<<endl;      
162.    
163.    c3=d+c1;    
164.    cout<<"d+c1="<<c3<<endl;      
165.    
166.    c3=c1-d;    
167.    cout<<"c1-d="<<c3<<endl;     
168.    
169.    c3=d-c1;    
170.    cout<<"d-c1="<<c3<<endl;      
171.    
172.    c3=c1*d;    
173.    cout<<"c1*d="<<c3<<endl;    
174.           
175.    c3=d*c1;    
176.    cout<<"d*c1="<<c3<<endl;      
177.    
178.    c3=c1/d;    
179.    cout<<"c1/d="<<c3<<endl;    
180.    
181.    c3=d/c1;    
182.    cout<<"d/c1="<<c3<<endl;      
183.        
184.    c3=-c1;    
185.    cout<<"-c1="<<c3<<endl;      
186.    
187.    c3=-c2;    
188.    cout<<"-c2="<<c3<<endl;      
189.    
190.    system("pause");    
191.    return 0;    
192.}    


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值