Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #! usr/bin/env python
- # -*- coding: utf-8 -*-
- import sys
- import math
- import cmath
- def solve(_coefficients):
- x = [None, None, None]
- if (_coefficients[0] == 0) and (_coefficients[1] == 0):
- if _coefficients[2] == 0:
- x[2] = 3
- return x
- d = _coefficients[1] ** 2 - 4 * _coefficients[0] * _coefficients[2]
- if d > 0:
- d = math.sqrt(d)
- x[0] = -(_coefficients[1] - d) / (2 * _coefficients[0])
- x[1] = -(_coefficients[1] + d) / (2 * _coefficients[0])
- x[2] = 2
- elif d == 0:
- x[0] = -(_coefficients[1]) / (2 * _coefficients[0])
- x[2] = 1
- else:
- d = cmath.sqrt(d)
- x[0] = -(_coefficients[1] - d) / (2 * _coefficients[0])
- x[1] = -(_coefficients[1] + d) / (2 * _coefficients[0])
- x[2] = 2
- return x
- if len(sys.argv) > 4:
- print "Слишком много аргументов"
- elif len(sys.argv) < 4:
- print "Слишком мало аргументов"
- else:
- coeffs=sys.argv[1:]
- try:
- coeffs[0] = float(sys.argv[1])
- coeffs[1] = float(sys.argv[2])
- coeffs[2] = float(sys.argv[3])
- except ValueError:
- print "Один или несколько из введенных аргументов не являются числами"
- else:
- print "Квадратное уравнение : {0}*x^2+{1}*x+{2}=0".format(coeffs[0], coeffs[1], coeffs[2])
- solution = solve(coeffs)
- if solution[2] == 2:
- print "Решение 1: {0}".format(solution[0])
- print "Решение 2: {0}".format(solution[1])
- elif solution[2] == 1:
- print "Решение : {0}".format(solution[0])
- elif solution[2] == 3:
- print "Решение уравнения может быть любое число"
- else:
- print ("Решений нет")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement