Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <cmath>
- using namespace std;
- class Polinom
- {
- private:
- int grad;
- float* coef;
- public:
- float* alocare(int grad)
- {
- float* vect = new float[grad + 1];
- return vect;
- }
- Polinom() : grad(0), coef(nullptr) {}
- Polinom(int grad) : grad(grad)
- {
- coef = alocare(grad);
- }
- Polinom(const Polinom& P)
- {
- grad = P.grad;
- coef = alocare(grad);
- for (int i = 0; i < grad + 1; ++i)
- coef[i] = P.coef[i];
- }
- ~Polinom()
- {
- delete[] coef;
- }
- friend istream& operator>>(istream& in, Polinom& P)
- {
- in >> P.grad;
- P.coef = P.alocare(P.grad);
- for (int i = 0; i < P.grad + 1; ++i)
- in >> P.coef[i];
- return in;
- }
- friend ostream& operator<<(ostream& out, Polinom P)
- {
- for (int i = 0; i < P.grad + 1; ++i)
- if (P.coef[i] != 0)
- {
- out << "(" << P.coef[i] << ")*x^" << i;
- if (i + 1 < P.grad + 1)
- out << " + ";
- }
- return out;
- }
- Polinom operator=(const Polinom& P)
- {
- if (this != &P)
- {
- this->~Polinom();
- grad = P.grad;
- coef = alocare(grad);
- for (int i = 0; i < grad + 1; ++i)
- coef[i] = P.coef[i];
- }
- return *this;
- }
- Polinom operator+(Polinom P)
- {
- Polinom aux;
- if (grad < P.grad)
- {
- aux = *this;
- *this = P;
- P = aux;
- }
- Polinom suma(grad);
- for (int i = 0; i < P.grad + 1; ++i)
- suma.coef[i] = coef[i] + P.coef[i];
- for (int i = P.grad + 1; i < grad + 1; ++i)
- suma.coef[i] = coef[i];
- swap(*this, P);
- return suma;
- }
- int operator!()
- {
- return grad;
- }
- float val(int x)
- {
- float s = 0;
- for (int i = 0; i < grad + 1; ++i)
- s += coef[i] * pow(x, i);
- return s;
- }
- float getCoef(int i) throw (out_of_range)
- {
- if (i<0 or i>grad + 1)
- throw out_of_range("Indice in afara limitelor");
- return coef[i];
- }
- };
- int main()
- {
- ifstream fin("polinoame.txt");
- Polinom A, B;
- fin >> A >> B;
- int x;
- cin >> x;
- float valA = A.val(x);
- float valB = B.val(x);
- if (valA == valB)
- {
- Polinom Suma(A);
- Suma = Suma + B;
- cout << Suma << endl;
- }
- else
- if (valA > valB)
- {
- cout << A << endl;
- }
- else
- cout << B << endl;
- try
- {
- cout << A.getCoef(0) << endl;
- cout << A.getCoef(100) << endl;
- }
- catch (out_of_range& e)
- {
- cout << "Eroare de tipul : " << e.what();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement