0% found this document useful (0 votes)
59 views

Corrig e TD5: Lelong J Er Ome 3 Janvier 2006

The document contains C++ code defining a Complex class with methods for operations like addition, multiplication and conjugation of complex numbers. It includes a Complex.h header file defining the Complex class interface and Complex.cpp defining the method implementations. It also includes test_Complex.cpp which tests the Complex class by printing outputs of operations on sample complex number objects.

Uploaded by

Tarek Hammouda
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views

Corrig e TD5: Lelong J Er Ome 3 Janvier 2006

The document contains C++ code defining a Complex class with methods for operations like addition, multiplication and conjugation of complex numbers. It includes a Complex.h header file defining the Complex class interface and Complex.cpp defining the method implementations. It also includes test_Complex.cpp which tests the Complex class by printing outputs of operations on sample complex number objects.

Uploaded by

Tarek Hammouda
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Corrigé TD5

LELONG Jérôme

3 janvier 2006

/* beginning of file Complex.cpp */

#include<iostream>
#include <cmath>

using namespace std;

#include "Complex.H"

Complex::Complex(double x, double y)
{
real = x;
img = y;
}

Complex::Complex(const Complex &c)


{
real = c.real;
img = c.img;
}

double Complex::module() const


{
return( sqrt(real*real + img*img) );
}

Complex Complex::conj() const


{
return( Complex(real, -img) );
}

double Complex::arg() const


{
if(img==0)
return 0;
if(img >0)
return acos(real/this->module());
else
return -acos(real/this->module());
}

1
Complex Complex::inverse() const
{
return( real/this->module() );
}

Complex& Complex::operator=( const Complex &c)


{
real = c.real;
img = c.img;

return *this;
}

Complex operator+(const Complex &c1, const Complex &c2)


{
return( Complex(c1.real + c2.real, c1.img + c2.img) );
}

Complex operator+(const Complex &c1, double x)


{
return( Complex(c1.real + x, c1.img ) );
}

Complex operator+(double x, const Complex &c2)


{
return( Complex(x+ c2.real, c2.img) );
}

Complex operator-(const Complex &c1, const Complex &c2)


{
return( Complex(c1.real - c2.real, c1.img - c2.img) );
}

Complex operator-(const Complex &c1, double x)


{
return( Complex(c1.real - x, c1.img ) );
}

Complex operator-(double x, const Complex &c2)


{
return( Complex(x- c2.real, c2.img) );
}

Complex operator*(const Complex &c1, const Complex &c2)


{
return( Complex(c1.real * c2.real - c1.img * c2.img, c1.real *...
... c2.img + c1.img*c2.real) );
}

2
Complex operator*(const Complex &c1, double x)
{
return( Complex(c1.real * x, c1.img * x ) );
}

Complex operator*(double x, const Complex &c2)


{
return( Complex(x * c2.real, x * c2.img) );
}

Complex operator/(const Complex &c1, const Complex &c2)


{
return( Complex(c1.real * c2.real - c1.img * c2.img, c1.real *...
... c2.img + c1.img*c2.real) / c2.module());
}

Complex operator/(const Complex &c1, double x)


{
if(x==0){
perror("division by zero");exit(1);
}
return( Complex(c1.real / x, c1.img / x ) );
}

bool operator==(const Complex &c1, const Complex &c2)


{
if(c1.real == c2.real && c1.img == c2.real)
return true;
return false;
}

ostream &operator<<(ostream &o, const Complex &c)


{
o << c.real << " + " << c.img << " I";
return o;
}

/* end of file Complex.cpp */

/* beginning of file Complex.H */

#ifndef __COMPLEX_H
#define __COMPLEX_H

#include <iostream>

class Complex{
double real;

3
double img;

public:
Complex(double=0, double=0);
Complex(const Complex &c);
double module() const;
double arg() const;
Complex conj() const;
double Real() const {return real;}
double Img() const {return img;}
double& Real() {return real;}
double& Img() {return img;}
Complex inverse() const;
Complex& operator=(const Complex&);

// fonctions amies
friend bool operator==(const Complex&, const Complex&);

friend Complex operator+(const Complex&, const Complex&);


friend Complex operator+(double, const Complex&);
friend Complex operator+(const Complex&, double);

friend Complex operator*(const Complex&, const Complex&);


friend Complex operator*(double, const Complex&);
friend Complex operator*(const Complex&, double);

friend Complex operator-(const Complex&, const Complex&);


friend Complex operator-(double, const Complex&);
friend Complex operator-(const Complex&, double);

friend Complex operator/(const Complex&, const Complex&);


friend Complex operator/(const Complex&, double );
friend std::ostream& operator<<(std::ostream&, const Complex &);
};

#endif

/* end of file Complex.H */

/* beginning of file test_Complex.cpp */

#include<iostream>
#include <cmath>
#include "Complex.H"

using namespace std;

int main()

4
{
Complex Z1 = Complex(1);
Complex Z2 = Complex(0,1);
Complex Z3 = Complex(1/2,sin(M_PI/3));
Complex Z4 = Complex(cos(M_PI/4),sin(M_PI/4));

cout << Z1 << endl;


cout << Z2 << endl;
cout << Z3 << endl;
cout << Z1+Z2 << endl;
cout << Z1+Z3 << endl;

cout << (Z1+Z2).arg() << endl;


cout << (Z1+Z2).module() << endl;

Complex Z = Complex((Z1+Z2)*sqrt(2));
if( Z == Z4)
cout << "true" << endl;
else
cout << "false" << endl;

cout << M_PI/4 << endl;


cout << Z4.arg() << endl;
cout << Z4.module() << endl;

Complex Z5;
cout << Z5 << endl;
Z5.Img() = 3;
cout << Z5 << endl;
exit(0);
}

/* end of file test_Complex.cpp */

You might also like