Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import math
- class Polinom:
- def __init__(self, gr, coef):
- if (len(coef)-gr)==1:
- self.grad=gr
- coef.reverse()
- self.coeficienti = coef
- else:
- raise Exception ("Parametrii incorecti")
- def __repr__(self):
- g=self.grad
- self.coeficienti.reverse()
- v=self.coeficienti
- self.coeficienti
- s="{}x^{}".format(v[0],g)
- for i in range (1, len(v)):
- g=g-1
- if v[i] >0 and i!=(len(v)-1):
- s=s+"+{}x^{}".format(v[i],g)
- if v[i] <0 and i!=(len(v)-1):
- s=s+"+({})x^{}".format(v[i],g)
- if i==(len(v)-1):
- s=s+"+{}".format(v[i])
- return s
- def calcul(self,x):
- suma=0
- for i in range(0, len(self.coeficienti)):
- monom=self.coeficienti[i]*x^i
- suma=suma + monom
- return suma
- def __add__(self, q):
- if self.grad == q.grad:
- v=[]
- for i in range(0, len(self.coeficienti)):
- el=self.coeficienti[i]+q.coeficienti[i]
- v.append(el)
- p = Polinom(self.grad,v)
- return p
- p=Polinom(4,[5,2,-1,0,7])
- p2=Polinom(4,[2,5,-1,7,8])
- print (p)
- print (p.calcul(2))
- print(p+p2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement