Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- using namespace std;
- class Polynomial
- {
- unsigned int degree;
- double *coefficients;
- public:
- Polynomial(unsigned int n)
- {
- coefficients=new double[n+1];
- degree=n;
- }
- ~Polynomial()
- {
- delete coefficients;
- }
- void Polynomial::setCoefficients(unsigned int i, double val)
- {
- coefficients[i]=val;
- }
- double Polynomial::getCoefficient(unsigned int i)
- {
- return coefficients[i];
- }
- double Polynomial::value(double y)
- {
- int suma=0;
- for(int k=degree; k>=0; k--)
- suma+=coefficients[k]*pow(y,k);
- return suma;
- }
- friend ostream & operator<<(ostream & out, Polynomial & p)
- {
- out<<"Twoj wielomian to:";
- if(p.coefficients[p.degree]!=1 && p.degree!=1)
- out<<p.coefficients[p.degree]<<"x^"<<p.degree;
- else if(p.degree!=1)
- out<<"x^"<<p.degree;
- if (p.degree==1){
- if(p.coefficients[p.degree]==0 && p.coefficients[p.degree-1]!=0)
- out<<p.coefficients[p.degree-1];
- else
- out<<"";
- if(p.coefficients[p.degree]!=1 && p.coefficients[p.degree]!=0)
- out<<p.coefficients[p.degree]<<"x";
- if(p.coefficients[p.degree-1]>0)
- out<<"+"<<p.coefficients[p.degree-1];
- if(p.coefficients[p.degree-1]<0 && p.coefficients[p.degree]!=0)
- out<<p.coefficients[p.degree-1];
- if(p.coefficients[p.degree-1]==0)
- out<<"";
- }
- else
- for(int k=p.degree-1; k>=0; k--)
- {
- if(k==0 && p.coefficients[k]>0)
- {
- out<<"+"<<p.coefficients[k];
- }
- else if(k==1 && p.coefficients[k]!=0)
- {
- if(p.coefficients[k]!=1)
- out<<"+"<<p.coefficients[k]<<"x";
- else
- out<<"+x";
- }
- if(p.coefficients[k]==0)
- out<<"";
- if (p.coefficients[k]==1 && k!=0 && k!=1)
- out<<"+x^"<<k;
- if(p.coefficients[k]>0 && k!=0 && k!=1)
- out<<"+"<<p.coefficients[k]<<"x^"<<k;
- if(p.coefficients[k]<0&& k!=0)
- out<<""<<p.coefficients[k]<<"x^"<<k;
- if(p.coefficients[k]<0 && k==0)
- out<<p.coefficients[k];
- }
- return out;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement