computorv1.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. neg = re.findall('X\^-', str)
  33. if neg: wrong_degree();
  34. i, s = 0, ""
  35. while (i < len(str) - 2):
  36. if cond_X0(str, i) and str[i + 1] == " " and str[i + 2] != "*":
  37. a = str[:i + 1]; b = str[i + 1:]; s = "";
  38. seq = a, " * X^0", b; str = s.join(seq)
  39. i = i + 1
  40. if cond_X0(str, len(str) - 1):
  41. seq = str, " * X^0"; str = s.join(seq)
  42. if str[0] == "X": seq = "1 * ", str; str = s.join(seq)
  43. if str[len(str) - 3] == "=" and str[len(str) - 1] == "X":
  44. a = str[:len(str) - 1]; seq = a, "1 * X^1"; str = s.join(seq)
  45. if (str[len(str) - 3] == "*" or str[len(str) - 3] == "-" or str[len(str) - 3] == "+") and str[len(str) - 1] == "X":
  46. a = str[:len(str) - 1]; seq = a, "X^1"; str = s.join(seq)
  47. str = str.replace("X ", "X^1 "); str = str.replace("+ X^", "+ 1 * X^")
  48. str = str.replace("- X^", "- 1 * X^"); str = str.replace("= X^", "= 1 * X^")
  49. eq1, eq2 = str.split("=")
  50. eq1 = re.split('(\-*\s*[\.0-9]*)\s\*\sX\^(\-*[0-9\.]*)', eq1)
  51. eq2 = re.split('(\-*\s*[\.0-9]*)\s\*\sX\^(\-*[0-9\.]*)', eq2)
  52. eq = re.split('(\-*\s*[\.0-9]*)\s\*\sX\^(\-*[0-9\.]*)', str)
  53. strp = str
  54. return (strp, eq, eq1, eq2)
  55. def get_coeff_degree(eq1, eq2):
  56. li = [[], []]
  57. for i in range(1, len(eq1), 3):
  58. t = eq1[i]; li[0].append(float(t.replace(" ", "")));
  59. li[1].append(float(eq1[i + 1]))
  60. for i in range(1, len(eq2), 3):
  61. t = eq2[i]; li[0].append(-float(t.replace(" ", "")));
  62. li[1].append(float(eq2[i + 1]))
  63. var, deg, i = 42, 0, 0
  64. while i < len(li[0]):
  65. if li[0][i] == 0: del li[0][i], li[1][i]; i -= 1
  66. i += 1
  67. while var == 42:
  68. var, i = 0, 0
  69. while i < len(li[1]):
  70. t, j = li[1][i], 0
  71. while j < len(li[1]) and 1 < len(li[1]):
  72. if i != j and t == li[1][j]:
  73. var = 42
  74. li[0][i] += li[0][j]; del li[0][j], li[1][j]; j -= 1
  75. if i < len(li[0]) and li[0][i] == 0: del li[0][i], li[1][i]
  76. j += 1;
  77. i += 1
  78. for i in range(0, len(li[1])):
  79. if li[1][i] > deg:
  80. deg = li[1][i]
  81. if (deg != 0 and deg != 1 and deg != 2): wrong_degree()
  82. a, b, c = 0, 0, 0
  83. for i in range (0, len(li[1])):
  84. if li[1][i] == 2: a = li[0][i]
  85. if li[1][i] == 1: b = li[0][i]
  86. if li[1][i] == 0: c = li[0][i]
  87. return (deg, a, b, c)
  88. def display(str, a, b, c, degree):
  89. print("Équation entrée :", str)
  90. print ("\nÉquation réduite :")
  91. if degree == 0: print(c, "= 0\nÉquation fausse et sans inconnue qui n’est pas résolvable."); exit()
  92. if degree == 1:
  93. if b == 1 or b == -1: sign(b, 1, 0); print ("X", end = " ");
  94. else: print (b, "* X", end = " ");
  95. if c < 0: sign(c, 1, 0); print(-c, end = " ")
  96. if c > 0: sign(c, 0, 1); print(c, end = " ")
  97. print("= 0\naX + b = 0")
  98. if degree == 2:
  99. if a == 1 or a == -1: sign(a, 1, 0); print ("X²", end = " ");
  100. else: print(a, "* X²", end = " ");
  101. if b == 1 or b == -1: sign(b, 1, 1); print("X", end = " ")
  102. elif b != 0: sign(b, 0, 1); print(b, "X", end = " ");
  103. if c != 0: sign(c, 0, 1); print (c, end = " ")
  104. print("= 0\naX² + bX + c = 0")
  105. print ("Équation de degré polynomial", end = " "); print(int(degree), "\n")
  106. def solve_degree_2(a, b, c):
  107. delta = b**2 - 4 * a * c
  108. print ("Calcul du discriminant :\nΔ = b² - 4ac\nΔ =", end = " ")
  109. print ("{}² - 4 × {} × {}\nΔ = {}".format(b, a, c, round(delta, 3)))
  110. alpha = -b / (2 * a)
  111. if delta > 0:
  112. beta = ft_sqrt(delta) / (2 * a)
  113. print ("Dicriminant scritement positif.\n\nCalcul des solutions :")
  114. print (" -b + √Δ {} + √{}\nX1 = ——————— = ——————————————— =".format(-b, round(delta, 3)), end = " ");
  115. print_d(alpha + beta); print("\n 2 × a 2 ×", a)
  116. print("\n -b - √Δ {} - √{}\nX2 = ——————— = ——————————————— =".format(-b, round(delta, 3)), end = " ")
  117. print_d(alpha - beta); print("\n 2 × a 2 ×", a)
  118. if delta == 0:
  119. print ("Discriminant nul.\n\nCalcul de la solution :")
  120. print (" -b {}\nX = ——————— = ——————— = {}\n 2 × a 2 × {}".format(-b, round(alpha, 3), a))
  121. if delta < 0:
  122. print ("Discriminant négatif.\n\nCalcul des solutions :")
  123. print (" -b + i × √-Δ {} + i × √{}".format(-b, round(round(-delta, 3), 3)))
  124. xi = ft_sqrt(-delta) / (2 * a)
  125. print("X1 = ————————————— = ——————————————————— =", end = " ");
  126. print_d(alpha); sign(a, 0, 1); print_d (xi);
  127. print("× i\n 2 × a 2 ×", a)
  128. print ("\n -b - i × √-Δ {} - i × √{}".format(-b, round(-delta, 3)))
  129. print("X2 = ————————————— = ——————————————————— =", end = " "); print_d(alpha);
  130. if a <= 0: print("+", end = " ");
  131. print_d (-xi); print ("× i\n 2 × a 2 ×", a)
  132. if __name__ == '__main__':
  133. if len(sys.argv) != 2: print("Entrez l’équation à résoudre."); exit()
  134. str = sys.argv[1]
  135. strp, eq, eq1, eq2 = parse(str)
  136. degree, a, b, c = get_coeff_degree(eq1, eq2)
  137. display(str, a, b, c, degree)
  138. if (degree == 1):
  139. print ("Solution :\n -b ", -c, " \nX = ——— = ——————— =", end = " ");
  140. print_d(-c / b); print("\n a ", b)
  141. if (degree == 2): solve_degree_2(a, b, c)