This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
in204:tds:sujets:td5:part1 [2019/10/22 19:37] bmonsuez [Question n°1.4] |
in204:tds:sujets:td5:part1 [2022/11/18 10:48] (current) |
||
---|---|---|---|
Line 30: | Line 30: | ||
<hidden Correction> | <hidden Correction> | ||
+ | <nodisp 2> | ||
Pour convertir un nombre en virgule flottante en un nombre complexe, il faut définir de [[in204:cpp:syntax:class:constructor|nouveaux constructeurs]] qui vont prendre un seul argument qui aura pour type le type d'un nombre à virgule flottante. | Pour convertir un nombre en virgule flottante en un nombre complexe, il faut définir de [[in204:cpp:syntax:class:constructor|nouveaux constructeurs]] qui vont prendre un seul argument qui aura pour type le type d'un nombre à virgule flottante. | ||
Line 63: | Line 64: | ||
}; | }; | ||
</code> | </code> | ||
- | |||
</hidden> | </hidden> | ||
==== Question n°1.2===== | ==== Question n°1.2===== | ||
Line 94: | Line 94: | ||
}; | }; | ||
</code> | </code> | ||
- | |||
</hidden> | </hidden> | ||
==== Question n°1.3===== | ==== Question n°1.3===== | ||
Line 187: | Line 186: | ||
Complex complexB(Polar(), 1.0, 0,52); | Complex complexB(Polar(), 1.0, 0,52); | ||
</code> | </code> | ||
- | |||
</hidden> | </hidden> | ||
Line 268: | Line 266: | ||
</code> | </code> | ||
- | |||
</hidden> | </hidden> | ||
Line 278: | Line 275: | ||
Complex complexValue = 3.3 + 5 * I; | Complex complexValue = 3.3 + 5 * I; | ||
</code> | </code> | ||
+ | |||
+ | <hidden Correction> | ||
+ | En fait, il est nécessaire de déclarer un opérateur de multiplication par une valeur numérique avant de pouvoir procéder à cette écriture. | ||
+ | |||
+ | Pour ce faire vous ajouter la déclaration suivante : | ||
+ | |||
+ | <code cpp> | ||
+ | |||
+ | class Complex | ||
+ | { | ||
+ | friend Complex operator * (int, const Complex&); | ||
+ | friend Complex operator * (double, const Complex&); | ||
+ | friend Complex operator * (const Complex&, int); | ||
+ | friend Complex operator * (const Complex&, double); | ||
+ | private: | ||
+ | ... | ||
+ | }; | ||
+ | |||
+ | inline Complex operator * (int theLeftValue, const Complex& theRightValue) | ||
+ | { | ||
+ | return Complex(theLeftValue * theRightValue.mRealPart, | ||
+ | theRightValue.mImaginaryPart); | ||
+ | } | ||
+ | inline Complex operator * (const Complex& theLeftValue, int theRightValue) | ||
+ | { | ||
+ | return Complex(theLeftValue.mRealPart * theRightValue, | ||
+ | theLeftValue.mImaginaryPart); | ||
+ | } | ||
+ | inline Complex operator * (double theLeftValue, const Complex& theRightValue) | ||
+ | { | ||
+ | return Complex(theLeftValue * theRightValue.mRealPart, | ||
+ | theRightValue.mImaginaryPart); | ||
+ | } | ||
+ | inline Complex operator * (const Complex& theLeftValue, double theRightValue) | ||
+ | { | ||
+ | return Complex(theLeftValue.mRealPart * theRightValue, | ||
+ | theLeftValue.mImaginaryPart); | ||
+ | } | ||
+ | </code> | ||
+ | |||
+ | </hidden> | ||
===== Question n°2===== | ===== Question n°2===== | ||
Line 292: | Line 330: | ||
</code> | </code> | ||
+ | <hidden Correction> | ||
+ | |||
+ | <code cpp> | ||
+ | Complex operator + (const Complex& aRightValue) const | ||
+ | { | ||
+ | Complex result(this); | ||
+ | result.mRealPart += aRightValue.mRealPart; | ||
+ | result.mImaginaryPart += aRightValue.mImaginaryPart; | ||
+ | return result; | ||
+ | } | ||
+ | Complex operator - (const Complex& aRightValue) const; | ||
+ | { | ||
+ | Complex result(this); | ||
+ | result.mRealPart -= aRightValue.mRealPart; | ||
+ | result.mImaginaryPart -= aRightValue.mImaginaryPart; | ||
+ | return result; | ||
+ | } | ||
+ | </code> | ||
+ | </hidden> | ||
==== Question n°2.2==== | ==== Question n°2.2==== | ||
Line 302: | Line 359: | ||
Expliquer la différence avec les opérations précédentes ? | Expliquer la différence avec les opérations précédentes ? | ||
+ | |||
+ | <hidden Correction> | ||
+ | <code cpp> | ||
+ | class Complex | ||
+ | { | ||
+ | ... | ||
+ | public: | ||
+ | Complex operator + (double aRightValue) const | ||
+ | { | ||
+ | Complex result(this); | ||
+ | result.mRealPart += aRightValue; | ||
+ | return result; | ||
+ | } | ||
+ | Complex operator - (double aRightValue) const; | ||
+ | { | ||
+ | Complex result(this); | ||
+ | result.mRealPart -= aRightValue; | ||
+ | return result; | ||
+ | } | ||
+ | </code> | ||
+ | |||
+ | Ces fonctions calculent l'addition d'un nombre à virgule flottante avec un nombre complexe, cependant, le nombre complexe est à gauche, le nombre à virgule flottante est à droite. Si nous voulons avoir un nombre à virgule flottante à gauche et un nombre complex à droite, il n'est pas possible de définir l'opérateur dans la classe mais en dehors de la classe. Les opérateurs définis dans la classe suppose que le premier argument de l'opéateur est l'objet lui-même. | ||
+ | |||
+ | <code cpp> | ||
+ | class Complex | ||
+ | { | ||
+ | friend Complex operator +(double, const Complex&); | ||
+ | friend Complex operator -(double, const Complex&); | ||
+ | ... | ||
+ | public: | ||
+ | Complex operator + (double aRightValue) const | ||
+ | { | ||
+ | Complex result(this); | ||
+ | result.mRealPart += aRightValue; | ||
+ | return result; | ||
+ | } | ||
+ | Complex operator - (double aRightValue) const; | ||
+ | { | ||
+ | Complex result(this); | ||
+ | result.mRealPart -= aRightValue; | ||
+ | return result; | ||
+ | } | ||
+ | ... | ||
+ | }; | ||
+ | |||
+ | Complex operator +(double aLeftValue, const Complex& aRightValue) | ||
+ | { | ||
+ | Complex result(aRightValue); | ||
+ | result.mRealPart += aRightValue; | ||
+ | return result; | ||
+ | } | ||
+ | Complex operator -(double aLeftValue, const Complex& aRightValue) | ||
+ | { | ||
+ | Complex leftValue(aLeftValue, 0.0); | ||
+ | return leftValue - aRightValue; | ||
+ | } | ||
+ | |||
+ | </code> | ||
+ | |||
+ | </hidden> | ||
+ | |||
==== Question n°2.3 ==== | ==== Question n°2.3 ==== | ||
Line 309: | Line 427: | ||
Complex complexValue = 3.3 + 5 * I; | Complex complexValue = 3.3 + 5 * I; | ||
</code> | </code> | ||
+ | |||
+ | <hidden Correction> | ||
+ | Nous pouvons désormais définir des types complexes et effectué des calculs sur ces types complexes. | ||
+ | |||
+ | Ainsi il sera possible de définir : | ||
+ | |||
+ | <code cpp> | ||
+ | Complex I(0, 1); | ||
+ | |||
+ | Complex complexValue = 3.3 + I; | ||
+ | |||
+ | </code> | ||
+ | |||
+ | Cependant, le code suivant est ambiguë : | ||
+ | |||
+ | <code cpp> | ||
+ | Complex complexValue = 3.3 + 5 * I; | ||
+ | </code> | ||
+ | En effet, il faudrait être mesure de faire une multiplication entre un nombre entier ou un nombre à virgule flottante et un nombre complexe. Nous verrons comment faire pour effectuer ce type conversion à la fin de l'exercice. | ||
+ | |||
+ | </hidden> | ||
+ | |||
==== Question n°2.4 ==== | ==== Question n°2.4 ==== | ||
Line 328: | Line 468: | ||
==== Question n°3.2 ==== | ==== Question n°3.2 ==== | ||
- | Proposer une surcharge des opérations + et –. Implanter ces dernières et tester. | + | Proposer une surcharge des opérations * et /. Implanter ces dernières et tester. |
<code cpp> | <code cpp> | ||
Line 335: | Line 475: | ||
</code> | </code> | ||
+ | <hidden Correction> | ||
+ | <code cpp> | ||
+ | Complex operator * (const Complex& aRightValue) const | ||
+ | { | ||
+ | return Complex( | ||
+ | mRealPart * aRightValue.mRealPart - mImaginaryPart * aRighValue.mImaginaryPart, | ||
+ | mRealPart * aRightValue.mImaginaryPart + mImaginaryPart * aRightValue.mRealPart); | ||
+ | } | ||
+ | Complex operator / (const Complex& aRightValue) const | ||
+ | { | ||
+ | double squareNorm = aRightValue.mRealPart * aRightValue.mRealPart | ||
+ | + aRightValue.mImaginaryPart * aRightValue.mImaginaryPart; | ||
+ | return *this * | ||
+ | Complex(aRighValue.mRealPart / squareNorm, | ||
+ | - aRightValue.mImaginaryPart / squareNorm); | ||
+ | } | ||
+ | |||
+ | </code> | ||
+ | </hidden> | ||
+ | |||
==== Question n°3.3 (optionnel) ==== | ==== Question n°3.3 (optionnel) ==== | ||
- | Proposer une surcharge des opérations + et –. Implanter ces dernières et tester. | + | Proposer une surcharge des opérations '*=' et '/='. Implanter ces dernières et tester. |
<code cpp> | <code cpp> |