Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cmath>
- #include <iostream>
- class Complex {
- private:
- double re, im;
- public:
- Complex() {
- }
- Complex(double r) {
- re = r;
- im = 0;
- }
- Complex(double r, double i) {
- re = r;
- im = i;
- }
- Complex(const Complex &c) {
- re = c.re;
- im = c.im;
- }
- ~Complex() {
- }
- Complex& operator = (Complex &c) {
- re = c.re;
- im = c.im;
- return (*this);
- }
- double Re() const {
- return re;
- }
- double& Re() {
- return re;
- }
- double Im() const {
- return im;
- }
- double& Im() {
- return im;
- }
- Complex operator -() {
- return {-re, -im};
- }
- Complex operator +() {
- return {re, im};
- }
- };
- double abs(Complex& a) {
- return sqrt(a.Re() * a.Re() + a.Im() * a.Im());
- }
- Complex operator + (const Complex& a, const Complex &c) {
- return Complex(a.Re() + c.Re(), a.Im() + c.Im());
- }
- Complex operator - (const Complex& a, const Complex &c) {
- return Complex(a.Re() - c.Re(), a.Im() - c.Im());
- }
- Complex operator * (const Complex& a, const Complex &c) {
- return Complex(a.Re() * c.Re() - a.Im() * c.Im(), a.Re() * c.Im() + a.Im() * c.Re());
- }
- Complex operator / (const Complex& a, const Complex &c) {
- Complex temp;
- double r = c.Re() * c.Re() + c.Im() * c.Im();
- temp.Re() = (a.Re() * c.Re() + a.Im() * c.Im()) / r;
- temp.Im() = (a.Im() * c.Re() - a.Re() * c.Im()) / r;
- return temp;
- }
- bool operator == (const Complex& a, const Complex& c) {
- return (a.Re() == c.Re() && a.Im() == c.Im());
- }
- bool operator != (const Complex& a, const Complex& c) {
- return (a.Re() != c.Re() || a.Im() != c.Im());
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement