Alex9090

lab2 python

Oct 16th, 2018
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.84 KB | None | 0 0
  1. from math import sqrt as rad
  2. def sign(x):
  3.         if x > 0: return 1.
  4.         if x < 0: return -1.
  5.         if x == 0: return 0.
  6. def getnr(name):
  7.         r = input(name + "=")
  8.         t = str(type(r))
  9.         if t == "<type 'int'>" or t == "<type 'float'>":
  10.                 return float(r)
  11.         while t != "<type 'int'>" and t != "<type 'float'>":
  12.                 print("singur int sau float");
  13.                 r = input (name + "=");
  14.                 t = str(type(r));
  15.         if t == "<type 'int'>" or t == "<type 'float'>":
  16.                 return float(r)
  17. def getComplex(name):
  18.         r = input(name + "=")
  19.         t = str(type(r))
  20.         if t == "<type 'int'>" or t == "<type 'float'>" or t == "<type 'complex'>":
  21.                 return complex(r)
  22.         while t != "<type 'int'>" and t != "<type 'int'>" and t != "<type 'complex'>"
  23.                 print "complex"
  24.                 r = input(name + "=")
  25.                 t = str(type(r))
  26.                 if t == "<type 'int'>" or t == "<type 'float'>" or t == "<type 'complex'>":
  27.                         return complex(r)
  28.  
  29. def ecgrad2():
  30.         print "Ecuatia de gradul 2"
  31.         a = getnr('a')
  32.         b = getnr('b')
  33.         c = getnr('c')
  34.         delta = b * b - 4 * a * c
  35.           if a == 0:
  36.                 print "Ecuatie de gradul 1"
  37.                 return
  38.         if delta == 0:
  39.                 print str(-b/(2*a))
  40.                 return
  41.         if delta > 0:
  42.                 x1 = -b + rad(delta) / 2 * a
  43.                 x2 = -b - rad(delta) / 2 * a
  44.                 print str(x1)
  45.                 print str(x2)
  46.                 return
  47.         if delta < 0:
  48.                 x1 = complex(-b, -rad(-delta))/ (2*a)
  49.                 print str(x1)
  50.                 x2 = complex(-b, rad(-delta)) / (2*a)
  51.                 print str(x2)
  52.                 return
  53.  
  54. print(sign(-2))
  55.  
  56. ecgrad2();
Advertisement
Add Comment
Please, Sign In to add comment