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/11/12 06:59] bmonsuez [Question n°2.2] |
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 317: | Line 314: | ||
} | } | ||
</code> | </code> | ||
- | |||
</hidden> | </hidden> | ||
Line 352: | Line 348: | ||
} | } | ||
</code> | </code> | ||
- | |||
</hidden> | </hidden> | ||
==== Question n°2.2==== | ==== Question n°2.2==== | ||
Line 366: | Line 361: | ||
<hidden Correction> | <hidden Correction> | ||
- | |||
<code cpp> | <code cpp> | ||
class Complex | class Complex | ||
Line 423: | Line 417: | ||
</code> | </code> | ||
+ | |||
</hidden> | </hidden> | ||
Line 432: | 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 451: | 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 458: | 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> |