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

Aritmetika: vylepšená verze 2025

parent 364d7f42
Branches
No related tags found
No related merge requests found
......@@ -2,12 +2,15 @@
# 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 z_retezce(s):
return [int(x) for x in reversed(s)]
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
......@@ -29,6 +32,42 @@ def secti(a, b):
return c
def secti2(a, b):
n = max(len(a), len(b))
a += [0] * (n - len(a))
b += [0] * (n - len(b))
c = []
prenos = 0
for i in range(n):
s = a[i] + b[i] + prenos
c.append(s % 10)
prenos = s // 10
if prenos > 0:
c.append(prenos)
return c
def odecti(a, b):
n = max(len(a), len(b))
a += [0] * (n - len(a))
b += [0] * (n - len(b))
c = []
prenos = 0
for i in range(n):
s = a[i] - b[i] + prenos
c.append(s % 10)
prenos = s // 10
return c
def vynasob_cislici(a, cislice):
c = []
prenos = 0
......@@ -43,6 +82,7 @@ def vynasob_cislici(a, cislice):
return c
def vynasob(a, b):
c = []
......@@ -52,8 +92,10 @@ def vynasob(a, b):
return c
# Příklad použití:
a = nacti()
b = nacti()
print("Součet:", na_retezec(secti(a, b)))
a = z_retezce(input("a="))
b = z_retezce(input("b="))
print("Součet:", na_retezec(secti2(a, b)))
print("Rozdíl:", na_retezec(odecti(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