computorv1.py 5.6 KB

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