比较复数的大小(用C++的类实现)
2017-01-02 17:43
1221 查看
注:1.复数的大小是通过复数的模来比较的;2.对于复数类大小的比较,可以用对“>”运算符的重载来实现。
#include<iostream> #include<cmath> using namespace std; class Complex { public: Complex(double real,char sign ,double imge) :_real(real) , _sign(sign) , _imge(imge) {} bool operator>(const Complex& c) { double square1 = pow(_real, 2) + pow(_imge, 2); double square1_root1 = sqrt(square1); double square2 = pow(c._real, 2) + pow(c._imge, 2); double square2_root2 = sqrt(square2); if (square1_root1 > square2_root2) { return true; } else { return false; } } void Display() { cout << _real << _sign << _imge << "i"; } private: double _real; //实部 double _imge; //虚部 char _sign; //虚部的正负 }; int main() { Complex c1(1.1, '+', 2.1); Complex c2(3.1, '-', 5.6); int ret = c1 > c2; cout << "比较结果:"<<endl; if (ret) { c1.Display(); cout << "大于"; c2.Display(); } else { c1.Display(); cout << "不大于"; c2.Display(); } cout << endl; return 0; }
相关文章推荐
- 【C++】用函数模板实现不同类型的两个数比较大小
- c++一个实现分数大小比较和各种运算的类
- c++实现字符串strcmp()大小比较
- C/C++程序——递归和循环实现阶乘,并比较N!与(2N N)的大小
- js实现比较日期大小
- java与C++多态实现比较
- c++怎么比较5个字符串的大小并将其按从小到大输出
- 一个无聊男人的疯狂《数据结构与算法分析-C++描述》学习笔记 习题2.8 随机数组的三种生成算法(补) 将bash的实现翻译成比较纯正的bash风格
- c++中比较3个数值的大小
- 多态在 Java 和 C++ 编程语言中的实现比较
- 每日一题:编写一个函数,不使用算术运算符,实现比较两个数的大小
- c++中复数的递增与递减操作实现
- 循环识别的C++/Java/Go/Scala实现比较
- C和C++的面向对象实现机制比较
- Comparable接口实现与大小比较时的误区
- C++ 字符串比较 汇编代码实现
- C++实现数组大小运算符dimensionof()
- C++实现数组大小运算符dimensionof()
- [原创]详述IComparer,IComparable接口,实现自定义方法比较对象大小并排序(C#)
- 循环识别的C++/Java/Go/Scala实现比较