Advertisement
Tucancitto

Pb11. Polinom

Jul 1st, 2021
881
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.34 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cmath>
  4. using namespace std;
  5.  
  6. class Polinom
  7. {
  8. private:
  9.     int grad;
  10.     float* coef;
  11.  
  12. public:
  13.     float* alocare(int grad)
  14.     {
  15.         float* vect = new float[grad + 1];
  16.         return vect;
  17.     }
  18.     Polinom() : grad(0), coef(nullptr) {}
  19.     Polinom(int grad) : grad(grad)
  20.     {
  21.         coef = alocare(grad);
  22.     }
  23.     Polinom(const Polinom& P)
  24.     {
  25.         grad = P.grad;
  26.         coef = alocare(grad);
  27.  
  28.         for (int i = 0; i < grad + 1; ++i)
  29.             coef[i] = P.coef[i];
  30.     }
  31.     ~Polinom()
  32.     {
  33.         delete[] coef;
  34.     }
  35.     friend istream& operator>>(istream& in, Polinom& P)
  36.     {
  37.         in >> P.grad;
  38.         P.coef = P.alocare(P.grad);
  39.  
  40.         for (int i = 0; i < P.grad + 1; ++i)
  41.             in >> P.coef[i];
  42.         return in;
  43.     }
  44.     friend ostream& operator<<(ostream& out, Polinom P)
  45.     {
  46.         for (int i = 0; i < P.grad + 1; ++i)
  47.             if (P.coef[i] != 0)
  48.             {
  49.                 out << "(" << P.coef[i] << ")*x^" << i;
  50.  
  51.                 if (i + 1 < P.grad + 1)
  52.                     out << " + ";
  53.             }
  54.  
  55.         return out;
  56.     }
  57.  
  58.     Polinom operator=(const Polinom& P)
  59.     {
  60.         if (this != &P)
  61.         {
  62.             this->~Polinom();
  63.             grad = P.grad;
  64.             coef = alocare(grad);
  65.  
  66.             for (int i = 0; i < grad + 1; ++i)
  67.                 coef[i] = P.coef[i];
  68.         }
  69.  
  70.         return *this;
  71.     }
  72.  
  73.     Polinom operator+(Polinom P)
  74.     {
  75.         Polinom aux;
  76.         if (grad < P.grad)
  77.         {
  78.             aux = *this;
  79.             *this = P;
  80.             P = aux;
  81.         }
  82.  
  83.         Polinom suma(grad);
  84.         for (int i = 0; i < P.grad + 1; ++i)
  85.             suma.coef[i] = coef[i] + P.coef[i];
  86.         for (int i = P.grad + 1; i < grad + 1; ++i)
  87.             suma.coef[i] = coef[i];
  88.  
  89.         swap(*this, P);
  90.  
  91.         return suma;
  92.     }
  93.  
  94.     int operator!()
  95.     {
  96.         return grad;
  97.     }
  98.  
  99.     float val(int x)
  100.     {
  101.         float s = 0;
  102.         for (int i = 0; i < grad + 1; ++i)
  103.             s += coef[i] * pow(x, i);
  104.  
  105.         return s;
  106.     }
  107.  
  108.     float getCoef(int i) throw (out_of_range)
  109.     {
  110.         if (i<0 or i>grad + 1)
  111.             throw out_of_range("Indice in afara limitelor");
  112.  
  113.         return coef[i];
  114.     }
  115. };
  116.  
  117. int main()
  118. {
  119.     ifstream fin("polinoame.txt");
  120.     Polinom A, B;
  121.     fin >> A >> B;
  122.     int x;
  123.     cin >> x;
  124.     float valA = A.val(x);
  125.     float valB = B.val(x);
  126.  
  127.     if (valA == valB)
  128.     {
  129.         Polinom Suma(A);
  130.         Suma = Suma + B;
  131.         cout << Suma << endl;
  132.     }
  133.     else
  134.         if (valA > valB)
  135.         {
  136.             cout << A << endl;
  137.         }
  138.         else
  139.             cout << B << endl;
  140.  
  141.     try
  142.     {
  143.         cout << A.getCoef(0) << endl;
  144.         cout << A.getCoef(100) << endl;
  145.     }
  146.     catch (out_of_range& e)
  147.     {
  148.         cout << "Eroare de tipul : " << e.what();
  149.     }
  150.  
  151.     return 0;
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement