Skip to content
Snippets Groups Projects
Commit d8c75e01 authored by Martin Mareš's avatar Martin Mareš
Browse files

Základní aritmetické algoritmy

parents
Branches
No related tags found
No related merge requests found
#!/usr/bin/python3
# Aritmetické algoritmy pracující po číslicích
# Číslo je reprezentované seznamem číslic počínaje _nejnižším_ řádem
def nacti():
return [ int(x) for x in reversed(input()) ]
def na_retezec(a):
return "".join([ str(x) for x in reversed(a) ])
def secti(a, b):
if len(a) < len(b):
a, b = b, a
c = []
prenos = 0
for i in range(len(a)):
if i < len(b):
bi = b[i]
else:
bi = 0
s = a[i] + bi + prenos
c.append(s % 10)
prenos = s // 10
if prenos > 0:
c.append(prenos)
return c
def vynasob_cislici(a, cislice):
c = []
prenos = 0
for i in range(len(a)):
s = a[i]*cislice + prenos
c.append(s % 10)
prenos = s // 10
if prenos > 0:
c.append(prenos)
return c
def vynasob(a, b):
c = []
for i in range(len(b)):
mezivysl = [0]*i + vynasob_cislici(a, b[i])
c = secti(c, mezivysl)
return c
# Příklad použití:
a = nacti()
b = nacti()
print("Součet:", na_retezec(secti(a, b)))
print("Součin:", na_retezec(vynasob(a, b)))
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment