1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| def addition(self, nat):
taille = max(self.taille(), nat.taille())
#normaliser les listes
diffTaille = abs(self.taille() - nat.taille())
if self.taille() > nat.taille():
for i in range(diffTaille):
nat.chiffre.append(0)
elif self.taille() < nat.taille():
for i in range(diffTaille):
self.chiffre.append(0)
#une fois on a les listes avec la meme taille, on execute l'addition
temp = []
carry = 0
for i in range(taille):
carry = carry + self.chiffre[i] + nat.chiffre[i]
temp.append(carry % 10)
carry = int(carry / 10)
if carry != 0:
temp.append(carry)
res = Naturel()
res.chiffre = []
for i in range(len(temp)):
res.chiffre.append(temp[i])
return res
def soustraction(self, nat):
intSelf = nat2Int(self.chiffre)
intNat = nat2Int(nat.chiffre)
intRes = intSelf - intNat
return int2Nat(intRes) |
Partager