Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cmath>
- #include <iostream>
- using namespace std;
- class Complex {
- public:
- double real, imag;
- Complex (double r) {
- real = r;
- imag = 0;
- }
- Complex (double r, double i) {
- real = r;
- imag = i;
- }
- Complex operator + (Complex& c) {
- return Complex (real + c.real, imag + c.imag);
- }
- Complex operator - (Complex& c) {
- return Complex (real - c.real, imag - c.imag);
- }
- Complex operator * (Complex& c) {
- return Complex (real * c.real - imag * c.imag, imag * c.real + real * c.imag);
- }
- Complex operator / (Complex& c) {
- return Complex ((real * c.real + imag * c.imag) / (c.real * c.real + c.imag * c.imag),
- (imag * c.real - real * c.imag) / (c.real * c.real + c.imag * c.imag));
- }
- Complex operator + () {
- return Complex (abs(real), abs(imag));
- }
- Complex operator - () {
- return Complex (abs(real) * (-1), abs(imag) * (-1));
- }
- double Re() {
- return real;
- };
- double Im() {
- return imag;
- }
- bool operator == (Complex& c) {
- return (real == c.real && imag == c.imag);
- }
- bool operator != (Complex& c) {
- return (real != c.real || imag != c.imag);
- }
- ostream& operator << (ostream& out)
- {
- out << "(" << real << ", " << imag << ")";
- return out;
- }
- };
- double abs (Complex& c) {
- return sqrt (c.real * c.real + c.imag * c.imag);
- }
- int main() {
- Complex a (1, 1), b (2, 2);
- cout << a;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement