main.py 5.8 KB

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