Untitled

 avatar
unknown
python
10 months ago
1.8 kB
8
Indexable
import math


def unstable_solution():
    a = float(input("Podaj a: "))

    if a == 0:
        print("To nie jest równanie kwadratowe.")
        return

    b = float(input("Podaj b: "))
    c = float(input("Podaj c: "))

    delta = b ** 2 - 4 * a * c
    if delta < 0:
        print("Brak rozwiązań równania")
    elif delta == 0:
        x0 = -b / (2 * a)
        print(f"Rówanienie ma jedno rozwiązanie:\nx0 = {x0}")
    else:
        x1 = (-b - math.sqrt(delta)) / (2 * a)
        x2 = (-b + math.sqrt(delta)) / (2 * a)
        print("Rówananie ma dwa rozwiązania:")
        print(f"x1 = {x1}\nx2 = {x2}")


def stable_solution():
    a = float(input("Podaj a: "))

    if a == 0:
        print("To nie jest równanie kwadratowe.")
        return

    b = float(input("Podaj b: "))
    c = float(input("Podaj c: "))
    delta = b ** 2 - 4 * a * c

    if delta < 0:
        print("Brak rozwiązań równania")
    elif delta == 0:
        x0 = -b / (2 * a)
        print(f"Rówanienie ma jedno rozwiązanie:\nx0 = {x0}")
    else:
        temp = c / a
        if b > 0:
            x1 = (-b - math.sqrt(delta)) / (2 * a)
            x2 = temp / x1
            print("Rówananie ma dwa rozwiązania:")
            print(f"x1 = {x1}\nx2 = {x2}")
        else:
            x2 = (-b + math.sqrt(delta)) / (2 * a)
            x1 = temp / x2
            print("Rówananie ma dwa rozwiązania:")
            print(f"x1 = {x1}\nx2 = {x2}")


if __name__ == '__main__':
    # unstable_solution()

    stable_solution()

    # Podaj
    # a: 1
    # Podaj
    # b: 19
    # Podaj
    # c: 2
    # Rówananie
    # ma
    # dwa
    # rozwiązania:
    # x1 = -18.894147114027966
    # x2 = -0.10585288597203224
Editor is loading...
Leave a Comment