• 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 }
  • 相关阅读:
    hdu 2492 树状数组 Ping pong
    HDU 1532 基础EK Drainage Ditches
    EK算法模板
    Codeforces Round #538 (Div. 2) (A-E题解)
    Codeforces Global Round 1 (A-E题解)
    Educational Codeforces Round 59 (Rated for Div. 2) DE题解
    Codeforces Round #535 (Div. 3) 题解
    Codeforces Round #534 (Div. 2) D. Game with modulo(取余性质+二分)
    POJ2253:Frogger(改造Dijkstra)
    POJ1797:Heavy Transportation(改造Dijkstra)
  • 原文地址:https://www.cnblogs.com/wzzdeblog/p/10722775.html
Copyright © 2020-2023  润新知