Telechargé par hamid raja

CorrigeTD1TD2

publicité
Corrigés de TD 1 et TD2 de la programmation orientée objet
class Rectangle:
def __init__(self, Long=0.0,larg=0.0,nbC=0):
self.longueur=Long
self.largeur=larg
self.nombreCotes=nbC
def surface(self):
return self.longueur*self.largeur
def perimetre(self):
return 2*(self.longueur+self.largeur)
rect=Rectangle(5.0,2.6)
print(rect.surface(), rect.perimetre())
class Parallelepipede:
def __init__(self,longueur=0.0, largeur=0.0, hauteur=0.0):
self.longueur=longueur
self.largeur=largeur
self.hauteur= hauteur
self.nom= "Parallèlépipède"
def volume(self ):
return self.longueur*self.largeur*self.hauteur
def __str__(self):
return "Le parallèlépipède de côtés : "+str(self.longueur)+" , " +str(self.largeur)+" et
"+str(self.hauteur)+ " a un volume de : "+ str(self.volume())
para=Parallelepipede(12,8,10)
print(para)
class Fraction:
def __simpliFrac(self,a,b):
if a<0: a=-a
if b<0: b=-b
while b!=0:
r=a%b
a=b
b=r
return a
def __init__(self, num=1, den=1):
if den==0: raise(ValueError("valeur nulle interdite"))
self.num=int(num/self.__simpliFrac(num,den))
self.den=int(den/self.__simpliFrac(num,den))
self.signe=1
if self.num*self.den<0: self.signe=-1
def __str__(self):
return str(self.num) + "/" + str(self.den)
def __neg__(self):# retourne la Fraction opposée ( - f )
a=Fraction(-self.num,self.den)
1
a.signe=-self.signe
return a
def __add__(self,other): #retourne la Fraction somme ( f + g )
d=self.__simpliFrac(self.den,other.den)
som=Fraction(self.num*(other.den/d)+other.num*(self.den/d), (self.den*other.den)/d)
som.signe=1
if som.num*som.den<0: som.signe=-1
return som
def __sub__(self,other): #retourne la Fraction différence ( f - g )
d=self.__simpliFrac(self.den,other.den)
sub=Fraction(self.num*(other.den/d)-other.num*(self.den/d), (self.den*other.den)/d)
sub.signe=1
if sub.num*sub.den<0: sub.signe=-1
return sub
def __mul__(self,other):#retourne la Fraction produit ( f * g )
return Fraction(self.num*other.num, self.den*other.den)
def __truediv__(self,other):# retourne la Fraction rapport ( f / g )
return Fraction(self.num*other.den, self.den*other.num)
def __floordiv__(self,other): #retourne la Fraction rapport ( f // g )
return Fraction(int((self.num*other.den)//(self.den*other.num)),1)
nbr1=Fraction(6,9)
nbr2=Fraction(7,5)
S=nbr1+nbr2
sub=nbr1-nbr2
multi=nbr1*nbr2
divi=nbr1/nbr2
divF=nbr1//nbr2
print(S)
print(sub)
print(multi)
print(divi)
print(divF)
class Robot:
def __init__(self, nom, x=0,y=0,direction="Est"):
self.nom=nom
self.x=x
self.y=y
self.direction=direction
def avance(self):
self.x+=1
return self
def droite(self):
if self.direction=="Nord":
self.x+=1; self.y-=1; self.direction="Est"
elif self.direction=="Est":
2
self.x-=1; self.y-=1; self.direction="Sud"
elif self.direction=="Sud":
self.x-=1;self.y+=1; self.direction="Ouest"
else:
self.x+=1; self.y+=1;self.direction="Nord"
return self
def __str__(self):
return "le robot: "+ str(self.nom)+ " est à la position: ("+str(self.x)+","+str(self.y)+") et la
direction :"+str(self.direction)
robot=Robot("kingi",5,2,"Nord")
print(robot)
print(robot.avance())
print(robot.droite())
class RobotNG:
turbo=True #un attribut de classe pour l'utiliser on utilise le nom de la classe : RobotNG.turbo
def __init__(self, nom, x=0,y=0,direction="Est"):
self.nom=nom
self.x=x
self.y=y
self.direction=direction
def avance(self,n):
self.x+=n
if RobotNG.turbo==True: self.x*=3
return self
def droite(self):
if self.direction=="Nord":
self.x+=1; self.y-=1; self.direction="Est"
elif self.direction=="Est":
self.x-=1; self.y-=1; self.direction="Sud"
elif self.direction=="Sud":
self.x-=1;self.y+=1; self.direction="Ouest"
else:
self.x+=1; self.y+=1;self.direction="Nord"
return self
def gauche(self):
if self.direction=="Nord":
self.x-=1; self.y-=1; self.direction="Ouest"
elif self.direction=="Est":
self.x-=1; self.y+=1; self.direction="Nord"
elif self.direction=="Sud":
self.x+=1;self.y+=1; self.direction="Est"
else:
self.x+=1; self.y-=1;self.direction="Sud"
return self
def demiTour(self):
return self.gauche().gauche()
3
def __str__(self):
return "le robot: "+ str(self.nom)+" est en mode Turbo, à la position: ("+str(self.x)+","+str(self.y)
+") et la direction :"+str(self.direction)
robot=RobotNG("kingi",5,2,"Est")
print(robot)
print(robot.avance(3))
print(robot.droite())
print(robot.gauche())
print(robot.demiTour())
class Dual:
def __init__(self,a=0.0,b=0.0):
self.a=a
self.b=b
def __repr__(self):
return str(self.a )+ " + "+str(self.b)+"*epsilon"
def __add__(self, other):
return Dual(self.a+other.a, self.b+other.b)
def __sub__(self,other):
return Dual(self.a-other.a, self.b-other.b)
def __mul__(self,other):# surcharge de l'opérateur * ;
return Dual(self.a*other.a, self.a*other.b+self.b*other.a)
def __truediv__ (self,other):# surcharge de l'opérateur / ;
ae=(self.a*other.a)/(other.a*other.a)
be=(self.b*other.a-self.a*other.b)/(other.a*other.a)
return Dual(ae,be)
def __floordiv__(self,other):# surcharge de l'opérateur // (division entière) ;
return Dual(int((self.a*other.a)/(other.a*other.a)))
def __mod__(self,other):# surcharge de l'opérateur % (modulo) ;
return (self.a*other.a)%(other.a*other.a)
def __pow__(self,n):# surcharge de l'opérateur ** (puissance)
p=self
for i in range(1,n,1):
p=p*self
return p
d1=Dual(3,5)
d2=Dual(2,7)
print(repr(d1))
S=d1+d2
print(repr(S))
su=d1-d2
print(repr(su))
m=d1*d2
print(repr(m))
q=d1/d2
print(repr(q))
en=d1//d2
4
print(repr(en))
r=d1%d2
print(repr(r))
p=d1**2
print(repr(p))
5
Téléchargement
Explore flashcards