zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    对NETIF_F_GSO的一些理解
    关于ptype_all和pypte_base中的pt_prev的说明[转]
    linux网络收包过程
    linux的pci驱动模型
    linux内核的冷热页分配器
    linux的bootmem内存管理
    GitHub 下载代码命令并且导入到IDEA环境
    配置Log4j(非常具体)
    sudo:有效用户 ID 不是 0,sudo 属于 root 并设置了 setuid 位吗?
    RMP和YUM软件安装
  • 原文地址:https://www.cnblogs.com/wzzdeblog/p/10722775.html
Copyright © 2011-2022 走看看