final复数的比较

请定义一个复数类,实数和虚数是其私有数据成员。再定义一个>(大于号)的运算符重载函数,用于比较两个复数间模的大小。

 

 1 #include<iostream>
 2 using namespace std;
 3 
 4 class Complex       //定义Comlpex类
 5 {
 6 private:
 7     int i,j;
 8 public:
 9     friend bool operator > (Complex &C1,Complex &C2);   //友元函数 >
10     friend istream& operator >> (istream &input, Complex &C);   //友元函数 >> 输入流
11     friend ostream& operator << (ostream &output, Complex &C);  //友元函数 << 输出流
12     Complex(void){};    //默认构造函数
13     Complex(int ii,int jj):i(ii),j(jj){};       //构造函数
14     friend bool isEnd(Complex &C1,Complex &C2); //友元函数 判断是否停止输入
15 };
16 
17 bool operator > (Complex &C1,Complex &C2)
18 {
19     if(C1.i*C1.i+C1.j*C1.j>C2.i*C2.i+C2.j*C2.j) return true;    //计算是否大于
20     else return false;
21 }
22 
23 istream& operator >> (istream &input,Complex &C)    //输入时调用
24 {
25     input>>C.i>>C.j;
26     return input;
27 }
28 
29 ostream& operator << (ostream &output,Complex &C)   //输出时调用
30 {
31     output<<C.i;
32     if(C.j>=0) output<<"+";
33     output<<C.j<<"i";
34     return output;
35 }
36 
37 bool isEnd(Complex &C1,Complex &C2)     //判断是否停止输入
38 {
39     if(!C1.i&&!C1.j&&!C2.i&&!C2.j) return true;
40     else return false;
41 }
42 
43 int main()
44 {
45     Complex C1,C2;
46     cin>>C1>>C2;    //调用Complex的输入流函数
47     while(!isEnd(C1,C2))    //判断是否停止输入
48     {
49         if(C1>C2) cout<<"true"<<endl;
50         else cout<<"false"<<endl;
51         cin>>C1>>C2;
52     }
53     return 0;
54 }

 

posted @ 2019-04-17 12:32  菜鸟plus  阅读(665)  评论(0编辑  收藏  举报