main.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #!/usr/bin/env python3
  2. # **************************************************************************** #
  3. # #
  4. # ::: :::::::: #
  5. # main.py :+: :+: :+: #
  6. # +:+ +:+ +:+ #
  7. # By: mazimi <mazimi@student.42.fr> +#+ +:+ +#+ #
  8. # +#+#+#+#+#+ +#+ #
  9. # Created: 2015/04/22 13:13:05 by mazimi #+# #+# #
  10. # Updated: 2015/09/29 13:03:13 by mazimi ### ########.fr #
  11. # #
  12. # **************************************************************************** #
  13. import sys
  14. import re
  15. def ft_sqrt (n):
  16. a = 1
  17. for i in range(0, 10):
  18. a = 0.5 * (a + n / a)
  19. return (a)
  20. def sign(a, less, more):
  21. if a >= 0 and more == 1: print("+", end = " ")
  22. if a < 0 and less == 1: print("-", end = " ")
  23. def print_d(n):
  24. print("{0:.3f}".format(round(n, 3)), end = " ")
  25. def wrong_degree():
  26. print ("Je ne sait pas résoudre les équations de degré polynomial différent de un et deux.")
  27. exit()
  28. def cond_X0(eq, i):
  29. if eq[i - 1] != "^" and "0" <= eq[i] and eq[i] <= "9":
  30. return (1)
  31. def parse (str):
  32. i, s = 0, ""
  33. while (i < len(str) - 2):
  34. if cond_X0(str, i) and str[i + 1] == " " and str[i + 2] != "*":
  35. a = str[:i + 1]; b = str[i + 1:]; s = "";
  36. seq = a, " * X^0", b; str = s.join(seq)
  37. i = i + 1
  38. if cond_X0(str, len(str) - 1):
  39. seq = str, " * X^0"; str = s.join(seq)
  40. if str[0] == "X": seq = "1 * ", str; str = s.join(seq)
  41. if str[len(str) - 3] == "=" and str[len(str) - 1] == "X":
  42. a = str[:len(str) - 1]; seq = a, "1 * X^1"; str = s.join(seq)
  43. str = str.replace("X ", "X^1 "); str = str.replace("+ X^", "+ 1 * X^")
  44. str = str.replace("- X^", "- 1 * X^"); str = str.replace("= X^", "= 1 * X^")
  45. eq1, eq2 = str.split("=")
  46. eq1 = re.split('(\-*\s*[\.0-9]*)\s\*\sX\^(\-*[0-9\.]*)', eq1)
  47. eq2 = re.split('(\-*\s*[\.0-9]*)\s\*\sX\^(\-*[0-9\.]*)', eq2)
  48. eq = re.split('(\-*\s*[\.0-9]*)\s\*\sX\^(\-*[0-9\.]*)', str)
  49. strp = str
  50. return (strp, eq, eq1, eq2)
  51. def get_coeff_degree(eq1, eq2):
  52. li = [[], []]
  53. for i in range(1, len(eq1), 3):
  54. t = eq1[i]; li[0].append(float(t.replace(" ", "")));
  55. li[1].append(float(eq1[i + 1]))
  56. for i in range(1, len(eq2), 3):
  57. t = eq2[i]; li[0].append(-float(t.replace(" ", "")));
  58. li[1].append(float(eq2[i + 1]))
  59. var, deg = 42, 0
  60. while var == 42:
  61. var, i = 0, 0
  62. while i < len(li[1]):
  63. t, j = li[1][i], 0
  64. while j < len(li[1]):
  65. if i != j and t == li[1][j]:
  66. var = 42
  67. li[0][i] += li[0][j]; del li[0][j], li[1][j]; j -= 1
  68. if li[0][i] == 0: del li[0][i], li[1][i]
  69. j += 1;
  70. i += 1
  71. for i in range(0, len(li[1])):
  72. if li[1][i] > deg:
  73. deg = li[1][i]
  74. if (deg != 0 and deg != 1 and deg != 2): wrong_degree()
  75. a, b, c = 0, 0, 0
  76. for i in range (0, len(li[1])):
  77. if li[1][i] == 2: a = li[0][i]
  78. if li[1][i] == 1: b = li[0][i]
  79. if li[1][i] == 0: c = li[0][i]
  80. return (deg, a, b, c)
  81. def display(str, a, b, c, degree):
  82. print("Équation entrée :", str)
  83. print ("\nÉquation réduite :")
  84. if degree == 0: print(c, "= 0\nÉquation fausse et sans inconnue qui n’est pas résolvable."); exit()
  85. if degree == 1:
  86. a = -2; b = 1
  87. if a == 1 or a == -1: sign(a, 1, 0); print ("X", end = " ");
  88. else: print (a, "* X", end = " ");
  89. if b < 0: sign(b, 1, 0); print(-b, end = " ")
  90. if b > 0: sign(b, 0, 1); print(b, end = " ")
  91. print("= 0\naX + b = 0")
  92. if degree == 2:
  93. if a == 1 or a == -1: sign(a, 1, 0); print ("X²", end = " ");
  94. else: print(a, "* X²", end = " ");
  95. if b == 1 or b == -1: sign(b, 1, 1); print("X", end = " ")
  96. elif b != 0: sign(b, 0, 1); print(b, "X", end = " ");
  97. if c != 0: sign(c, 0, 1); print (c, end = " ")
  98. print("= 0\naX² + bX + c = 0")
  99. print ("Équation de degré polynomial", end = " "); print(int(degree), "\n")
  100. def solve_degree_2(a, b, c):
  101. delta = b**2 - 4 * a * c
  102. print ("Calcul du discriminant :\nΔ = b² - 4ac\nΔ =", end = " ")
  103. print ("{}² - 4 × {} × {}\nΔ = {}".format(b, a, c, round(delta, 3)))
  104. alpha = -b / (2 * a)
  105. if delta > 0:
  106. beta = ft_sqrt(delta) / (2 * a)
  107. print ("Dicriminant scritement positif.\n\nCalcul des solutions :")
  108. print (" -b + √Δ {} + √{}\nX1 = ——————— = ——————————————— =".format(-b, round(delta, 3)), end = " ");
  109. print_d(alpha + beta); print("\n 2 × a 2 ×", a)
  110. print("\n -b - √Δ {} - √{}\nX2 = ——————— = ——————————————— =".format(-b, round(delta, 3)), end = " ")
  111. print_d(alpha - beta); print("\n 2 × a 2 ×", a)
  112. if delta == 0:
  113. print ("Discriminant nul.\n\nCalcul de la solution :")
  114. print (" -b {}\nX = ——————— = ——————— = {}\n 2 × a 2 × {}".format(-b, round(alpha, 3), a))
  115. if delta < 0:
  116. print ("Discriminant négatif.\n\nCalcul des solutions :")
  117. print (" -b + i × √-Δ {} + i × √{}".format(-b, round(round(-delta, 3), 3)))
  118. xi = ft_sqrt(-delta) / (2 * a)
  119. print("X1 = ————————————— = ——————————————————— =", end = " ");
  120. print_d(alpha); sign(a, 0, 1); print_d (xi);
  121. print("× i\n 2 × a 2 ×", a)
  122. print ("\n -b - i × √-Δ {} - i × √{}".format(-b, round(-delta, 3)))
  123. print("X2 = ————————————— = ——————————————————— =", end = " "); print_d(alpha);
  124. if a <= 0: print("+", end = " ");
  125. print_d (-xi); print ("× i\n 2 × a 2 ×", a)
  126. if __name__ == '__main__':
  127. if len(sys.argv) != 2: print("Entrez l’équation à résoudre."); exit()
  128. str = sys.argv[1]
  129. strp, eq, eq1, eq2 = parse(str)
  130. degree, a, b, c = get_coeff_degree(eq1, eq2)
  131. display(str, a, b, c, degree)
  132. if (degree == 1):
  133. print ("Solution :\n -b ", -c, " \nX = ——— = ——————— =", end = " ");
  134. print_d(-c / b); print("\n a ", b)
  135. if (degree == 2): solve_degree_2(a, b, c)