Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <math.h>
- using namespace std;
- class Complex {
- private:
- double re;
- double im;
- public:
- Complex(double _re, double _im = 0) : re(_re), im(_im) {}
- double Re() const {
- return re;
- }
- double Im() const {
- return im;
- }
- const Complex& operator+ () const {
- return *this;
- }
- Complex operator- () const {
- return Complex(-re, -im);
- }
- friend Complex operator+(const Complex& a, const Complex& b);
- friend Complex operator-(const Complex& a, const Complex& b);
- friend Complex operator*(const Complex& a, const Complex& b);
- friend Complex operator/(const Complex& a, const Complex& b);
- friend bool operator==(const Complex& a, const Complex& b);
- friend bool operator!=(const Complex& a, const Complex& b);
- };
- Complex operator+(const Complex& a, const Complex& b) {
- return Complex(a.re + b.re, a.im + b.im);
- }
- Complex operator-(const Complex& a, const Complex& b) {
- return Complex(a.re - b.re, a.im - b.im);
- }
- Complex operator*(const Complex& a, const Complex& b) {
- return Complex(a.re * b.re - a.im * b.im, a.re * b.im + a.im * b.re);
- }
- Complex operator/(const Complex& a, const Complex& b) {
- double re = (a.re * b.re + a.im * b.im) / (b.re * b.re + b.im * b.im);
- double im = (b.re * a.im - b.im * a.re) / (b.re * b.re + b.im * b.im);
- return Complex(re, im);
- }
- bool operator==(const Complex& a, const Complex& b) {
- if (a.re == b.re && a.im == b.im) {
- return true;
- }
- else {
- return false;
- }
- }
- bool operator!=(const Complex& a, const Complex& b) {
- return !(a == b);
- }
- double abs(const Complex& a) {
- return sqrt(a.Re() * a.Re() + a.Im() * a.Im());
- }
Advertisement
Add Comment
Please, Sign In to add comment