Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <iostream>
- #include <stdio.h>
- #include <math.h>
- #include <stdlib.h>
- using namespace std;
- struct Complex {
- double re, im;
- Complex(double re, double im)
- {
- this->re = re;
- this->im = im;
- }
- double modulo()
- {
- return sqrt(this->re*this->re + this->im*this->im);
- }
- Complex conj()
- {
- return Complex(re, -im);
- }
- void print()
- {
- printf("[%lf %lf]", re, im);
- }
- Complex* addr()
- {
- return this;
- }
- };
- Complex operator+(Complex x, Complex y)
- {
- return Complex(x.re +y.re, x.im + y.im);
- }
- Complex operator-(Complex x, Complex y)
- {
- return Complex(x.re - y.re, x.im - y.im);
- }
- Complex operator*(Complex x, Complex y)
- {
- return Complex(x.re*y.re - x.im*y.im, x.re*y.im + x.im*y.re);
- }
- Complex operator/(Complex x, Complex y)
- {
- double znam = y.re*y.re + y.im*y.im;
- return Complex((x.re*y.re + x.im*y.im)/znam, (x.re*y.im - x.im*y.re)/znam);
- }
- void print(Complex x)
- {
- cout << "(" << x.re << " " << x.im << ")";
- }
- ostream& operator<<(ostream& o, Complex c)
- {
- o << "[" << c.re << " " << c.im << "]";
- return o;
- }
- class NewComplex : public Complex
- {
- public:
- NewComplex() : Complex ()
- {}
- NewComplex(re, im) : Complex (re, im)
- {}
- Complex opposit_vector()
- {
- return Complex(-im, re);
- }
- };
- int _tmain(int argc, _TCHAR* argv[])
- {
- Complex x = Complex(3, -5),
- y = Complex(1, -2);
- Complex res = (x + y)/(x - y);
- cout << " x = " << x << endl;
- cout << " y = " << y << endl; cout << endl;
- cout << " x + y = " << x + y << endl;
- cout << " x - y = " << x - y << endl;
- cout << " x * y = " << x * y << endl;
- cout << " x / y = " << x / y << endl;
- cout << "(x + y)/(x - y) = (" << x << " + " << y <<")/(" << x << " - " << y << ") = " << res << endl;
- cout << "|((x + y)/(x - y)| = |" << res << "| = " << res.modulo() << endl;
- cout << " res = " << res << endl;
- cout << "!res = " << res.conj() << endl;
- NewComplex nc(1, 2);
- cout << nc << nc + 10 << " " << nc.opposit_vector() << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement