"""  Fractions (exercice 2.29)     """

def long(a,b): # nécessaire à l'affichage
    """ fonction déterminant la dimension correcte du trait de fraction
        en entrée : a et b, les numérateurs et dénominateurs
        en sortie : A et B (idem mais avec les espaces qui conviennent
                    et C : le trait de fraction (dimensionné correctement) """
    A,B,C=str(a),str(b),"-"
    m=max(len(A),len(B))
    while len(A)<m : A=" "+A
    while len(B)<m : B=" "+B
    while len(C)<m : C="-"+C
    return A,B,C
    
def affichage(a1,b1,op,a2,b2): # question 3 (utilise la fonction long)
    a3,b3=operation(a1,b1,op,a2,b2)
    A1,B1,C1=long(a1,b1)
    A2,B2,C2=long(a2,b2)
    A3,B3,C3=long(a3,b3)
    print(A1+"   "+A2+"   "+A3)
    print(C1+" "+op+" "+C2+" = "+C3)
    print(B1+"   "+B2+"   "+B3)

def operation(a1,b1,op,a2,b2): # question 2
    a,b=0,0
    if op=="+":
        a=a1*b2+a2*b1
        b=b1*b2
    if op=="-":
        a=a1*b2-a2*b1
        b=b1*b2
    if op=="*":
        a=a1*a2
        b=b1*b2
    if op=="/":
        a=a1*b2
        b=b1*a2
    return irreductible(a,b)
    
def irreductible(a,b): # question 1
    assert type(a)==int and type(b)==int and b>0
    num,denom=abs(a),b # recherche du pgcd
    if a==0 : return a,b
    while num!=denom :
        if num>denom :num-=denom
        else : denom-=num
    return a//num,b//num
"""
print(operation(1,2,"+",1,6))
print(operation(1,12,"+",15,6))
print(operation(-1,123,"+",15,456))
print(operation(-1,123,"/",15,456))
print(operation(112,123,"*",-125,456))
"""
affichage(1,2,"+",1,6)
affichage(1,12,"+",15,6)
affichage(-1,123,"+",15,456)
affichage(-1,123,"/",15,456)
affichage(112,123,"*",-125,456)


