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