Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import re, math
- class SquareEquationException(Exception):
- pass
- def solve_square_equation(eq):
- eq = eq.lower().lstrip().rstrip().replace(' ', '')
- eqreg = re.findall('(?:[-+]{0,1}[0-9]{0,100}x[\^]2|[-+]{1}[0-9]{0,100}x|[-+]{0,1}[0-9]{1,100})', eq)
- try:
- for i in eqreg:
- if '^' in i:
- if (i[0] == '+' or i[0] == '-') and i[1] == 'x':
- a = int('%s1' % i[0])
- elif i[0] == 'x':
- a = 1
- else:
- a = int(i.split('x')[0])
- elif 'x' in i:
- b = int(i.split('x')[0])
- else:
- c = int(i)
- D = b * b - 4 * a * c
- print('a = %d\nb = %d\nc = %d\nD = %d'% (a,b,c,D))
- if D < 0:
- raise SquareEquationException
- if D > 0:
- x1 = int((-b + math.sqrt(D)) / 2 * a)
- x2 = int((-b - math.sqrt(D)) / 2 * a)
- roots = [x1, x2]
- roots.sort()
- print('x1, x2 =',roots)
- if D == 0:
- x = int((-b + math.sqrt(D)) / 2 * a)
- print('x = [',x,']')
- except Exception as ERR:
- print(ERR)
- raise SquareEquationException
- eq = input()
- print(eq,'= 0')
- solve_square_equation(eq)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement