-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d520214
commit 583d8a4
Showing
4 changed files
with
92 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
""" | ||
""" | ||
from math import sqrt | ||
|
||
|
||
class Complexe: | ||
|
||
# Le constructeur | ||
def __init__(self, a=0, b=0): | ||
self.__a = a | ||
self.__b = b | ||
|
||
# Les accesseurs | ||
def getA(self): | ||
return self.__a | ||
|
||
def getB(self): | ||
return self.__b | ||
|
||
def setA(self, a): | ||
self.__a = a | ||
|
||
def setB(self, b): | ||
self.__b = b | ||
|
||
# La méthode qui permet de faire afficher un nombre complexe | ||
def __str__(self): | ||
a = self.getA() | ||
b = self.getB() | ||
if not(a != 0 or (a == 0 and b == 0)): | ||
a = "" | ||
return f"{a}{"+" if b > 0 else ""}{b if b not in {-1, 0, 1} else ""}{"i" if b != 0 else ""}" | ||
|
||
# La méthode qui retourne la somme de deux nombre complexes | ||
def ajouter(self, complex): | ||
return Complexe(self.getA() + complex.getA(), self.getB() + complex.getB()) | ||
|
||
# La méthode qui retourne le produit de deux nombres complexes | ||
def produit(self, complex): | ||
a = self.getA()*complex.getA() - self.getB()*complex.getB() | ||
b = self.getA()*complex.getB() + self.getB()*complex.getA() | ||
return Complexe(a, b) | ||
|
||
# La méthode qui retourne le conjugué d'un nombre complexe | ||
def conjugue(self): | ||
return Complexe(self.getA(), - self.getB()) | ||
|
||
# La méthode qui calcule le module d'un nombre complexe | ||
def module(self): | ||
return f"{sqrt(self.getA()**2 + self.getB()**2):.2f}" | ||
|
||
# La méthode qui retourne le carré d'un nombre complexe | ||
def carre(self): | ||
a = self.getA()**2 - self.getB()**2 | ||
b = 2 * self.getA() * self.getB() | ||
return Complexe(a, b) | ||
|
||
# La méthode qui compare deux nombre complexes | ||
def comparer(self, complex): | ||
return True if self.getA() == complex.getA() and self.getB() == complex.getB() else False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from Complexe import Complexe | ||
|
||
|
||
# Application de l'exercice 1 | ||
nb1 = Complexe(1, -1) | ||
nb2 = Complexe(2, 4) | ||
|
||
somme = nb1.ajouter(nb1) | ||
produit = nb1.produit(nb2) | ||
conjugue = nb1.conjugue() | ||
module = nb1.module() | ||
carre = nb1.carre() | ||
|
||
print(somme) | ||
print(produit) | ||
print(conjugue) | ||
print(module) | ||
print(carre) | ||
print(nb1.comparer(nb2)) | ||
print(Complexe()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters