Advertisement
madalinaradu

Polinom

Apr 15th, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.54 KB | None | 0 0
  1. #include<iostream>
  2. #include<conio.h>
  3. #include<math.h>
  4. using namespace std;
  5.  
  6.  
  7.  
  8. class Polinom{
  9.     private:
  10.         int grad;
  11.         float *coef;
  12.     public:
  13.         Polinom(int grad=0);
  14.         Polinom(const Polinom &p);
  15.         ~Polinom();
  16.         void citire();
  17.         void afisare();
  18.         float valPunct(float x);
  19.         Polinom operator +(const Polinom &p);
  20.         Polinom& operator =(const Polinom &p);
  21. };
  22.  
  23. Polinom::Polinom(int grad){
  24.         this->grad=grad;
  25.         this->coef=new float[grad+1];
  26.         for(int i=0; i<=grad+1; i++){
  27.             this->coef[i]=0;
  28.         }
  29.         cout<<"C cu param"<<endl;
  30. }
  31. Polinom::Polinom(const Polinom &p){
  32.     this->grad=p.grad;
  33.     this->coef=new float[p.grad+1];
  34.     for(int i=0; i<=p.grad+1; i++){
  35.             this->coef[i]=p.coef[i];
  36.         }
  37.     // de copiat coeficientii
  38.     cout<<"C de copiere"<<endl;
  39. }
  40. Polinom::~Polinom(){
  41.     if(coef!=0){
  42.         delete coef;
  43.     }
  44.     cout<<"Destructor"<<endl;
  45. }
  46. void Polinom::citire(){
  47.     cout<<"Dati gradul polinomului"<<endl;
  48.     int temp;
  49.     cin>>temp;
  50.     if(temp>grad){
  51.         delete coef;
  52.         this->coef=new float[temp+1];
  53.     }
  54.     grad=temp;
  55.     //Daca citesc gradul trebuie realocat tabloul.
  56.  
  57.     cout<<"Dati coeficientii:"<<endl;
  58.     for(int i=0; i<=grad;i++){
  59.         cout<<"Dati coef lui X^"<<i<<"= "<<endl;
  60.         cin>>coef[i];
  61.     }
  62. }
  63. void Polinom:: afisare(){
  64.     for(int i=0; i<=grad; i++){
  65.         cout<<coef[i]<<"*X^"<<i<<" + ";
  66.     }
  67.     cout<<endl;
  68. }
  69.  
  70. Polinom Polinom ::operator +(const Polinom &p){
  71.     int max=(this->grad>p.grad) ? this->grad : p.grad;
  72.     Polinom suma(max);
  73.     for (int i=0; i<=this->grad; i++){
  74.         suma.coef[i]+=this->coef[i];
  75.     }
  76.     for (int i=0; i<=p.grad; i++){
  77.         suma.coef[i]+=p.coef[i];
  78.     }
  79. return suma;
  80. }
  81.  
  82. Polinom & Polinom::operator=(const Polinom &p){//p este obiectul din dreapta lui =
  83.     if(this != &p){
  84.         if(this->grad<p.grad){
  85.             delete this->coef;
  86.             this->coef=new float[p.grad+1];
  87.         }
  88.  
  89.         this->grad=p.grad;
  90.         for(int i=0; i<=p.grad; i++){
  91.             this->coef[i]=p.coef[i];
  92.         }
  93.     }
  94.     return *this;
  95. }
  96.  
  97. float Polinom:: valPunct(float x){
  98.     int i;
  99.     float suma=0;
  100.     for(i=0; i<=grad; i++){
  101.         suma+=this->coef[i]*pow(x,i);
  102.     }
  103.     return suma;
  104. }
  105.  
  106. int main(){
  107.     Polinom p,q;
  108.     p.citire();
  109.     p.afisare();
  110.     q.citire();
  111.     q.afisare();
  112.     Polinom rez;
  113.     rez=p+q;
  114.     cout<<"Suma celor 2 polinoame este: ";
  115.     rez.afisare();
  116.     cout<<endl;
  117.     return 0;
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement