Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "iostream"
- #include "stdlib.h"
- #define N 100
- using namespace std;
- class Polynom
- {
- private:
- int count;
- double *coef;
- public:
- Polynom(const Polynom&);
- Polynom(int);
- ~Polynom(){}
- Polynom operator+(Polynom);
- Polynom operator-(Polynom);
- Polynom operator*(Polynom);
- Polynom& operator--();
- Polynom operator--(int);
- Polynom& operator++();
- Polynom operator++(int);
- double operator[](int i)const{return coef[i];}
- const Polynom& operator=(const Polynom&);
- void set_i_coef(int i, double coef_){coef[i] = coef_;}
- void set_i_coef_pl(int i,double coef_){ coef[i] += coef_; }
- void set_count(int);
- void set_pol();
- void set_pol_man();
- void show_pol();
- int get_count()const{ return count; }
- friend ostream &operator<<(ostream &, const Polynom &);
- };
- Polynom & Polynom::operator++()
- {
- coef[0]++;
- return *this;
- }
- Polynom Polynom ::operator++(int)
- {
- Polynom temp = *this;
- ++*this;
- return temp;
- }
- Polynom & Polynom::operator--()
- {
- coef[0]--;
- return *this;
- }
- Polynom Polynom ::operator--(int)
- {
- Polynom temp = *this;
- --*this;
- return temp;
- }
- ostream & operator<<(ostream &output, const Polynom &elem)
- {
- int i = elem.get_count()-1;
- for(;i>0;i--)
- if(elem[i]!=0)
- if(i!=1)
- output<<"x^"<<i<<"*("<<elem[i]<<")+";
- else
- output<<"x*("<<elem[i]<<")+";
- output<<elem[i];
- return output;
- }
- void Polynom::show_pol()
- {
- cout<<"\nPolynom: \n";
- for(int i=get_count()-1;i>=0;i--)
- cout<<"x^"<<i<<"*("<<coef[i]<<")+";
- cout<<endl;
- }
- const Polynom& Polynom::operator=(const Polynom& right)
- {
- set_count(right.get_count());
- for(int i=0;i<count;i++)
- coef[i] = right[i];
- return *this;
- }
- Polynom Polynom::operator*(Polynom right)
- {
- int new_size = get_count()+right.get_count();
- bool right_ = false;
- Polynom new_pol(new_size);
- for(int i=0;i<get_count();i++)
- for(int j=0;j<right.get_count();j++)
- new_pol.set_i_coef_pl(i+j,right[j]*coef[i]);
- return new_pol;
- }
- Polynom Polynom::operator+(Polynom right)
- {
- int new_size,new_size_2;
- bool right_ = false;
- if(get_count()>right.get_count())
- {
- new_size_2 = get_count();
- new_size = right.get_count();
- }
- else
- {
- new_size_2 = right.get_count();
- right_ = true;
- new_size = get_count();
- }
- Polynom new_pol(new_size_2);
- for(int i=0;i<new_size;i++)
- new_pol.set_i_coef(i,coef[i]+right[i]);
- if(right_)
- for(int i=new_size;i<right.get_count();i++)
- new_pol.set_i_coef(i,right[i]);
- else
- for(int i=new_size;i<get_count();i++)
- new_pol.set_i_coef(i,coef[i]);
- return new_pol;
- }
- Polynom Polynom::operator-(Polynom right)
- {
- int new_size,new_size_2;
- bool right_ = false;
- if(get_count()>right.get_count())
- {
- new_size_2 = get_count();
- new_size = right.get_count();
- }
- else
- {
- new_size_2 = right.get_count();
- right_ = true;
- new_size = get_count();
- }
- Polynom new_pol(new_size_2);
- for(int i=0;i<new_size;i++)
- new_pol.set_i_coef(i,coef[i]-right[i]);
- if(right_)
- for(int i=new_size;i<right.get_count();i++)
- new_pol.set_i_coef(i,right[i]);
- else
- for(int i=new_size;i<get_count();i++)
- new_pol.set_i_coef(i,coef[i]);
- return new_pol;
- }
- void Polynom::set_count(int i=-1)
- {
- int count_;
- if(i<0)
- {
- cout<<"\nEnter new count: ";
- cin>>count_;
- }
- else
- count_ = i;
- coef = new double[count_];
- count = count_;
- }
- Polynom::Polynom(const Polynom& right)
- {
- set_count(right.get_count());
- for(int i=0;i<right.get_count();i++)
- set_i_coef(i,right[i]);
- }
- Polynom::Polynom(int count_ = 0)
- {
- coef = new double[count_];
- count = count_;
- for(int i=0;i<get_count();i++)
- set_i_coef(i,0);
- }
- void Polynom::set_pol()
- {
- for(int i=0;i<count;i++)
- coef[i] = rand()/N;
- }
- void Polynom::set_pol_man()
- {
- cout<<"/nEnter the coefficient of degrees ";
- for(int i=0;i<count;i++)
- {
- cout<<"\n "<<i<<":";
- cin>>coef[i];
- }
- cout<<"\n";
- }
- Polynom sum(Polynom* arr, int n)
- {
- Polynom sum;
- for(int i=0;i<n;i++)
- sum = sum + arr[i];
- return sum;
- }
- int main()
- {
- srand(0);
- Polynom *polynoms;
- cout<<"Enter num of polynoms: ";
- int n; cin>>n;
- polynoms = new Polynom[n];
- for(int i=0;i<n;i++)
- {
- cout<<"\nEnter size of pol: ";
- int x; cin>>x;
- polynoms[i] = Polynom(x);
- polynoms[i].set_pol();
- }
- cout<<"Polynoms: \n\n";
- for(int i=0;i<n;i++)
- cout<<polynoms[i]<<endl;
- Polynom summ;
- summ = sum(polynoms,n);
- cout<<summ;
- cout<<"summ++ = "<<summ++<<endl;
- cout<<"summ = "<<summ<<endl;
- cout<<"--summ = "<<--summ<<endl;
- cout<<"summ - polynoms[0] = "<<summ - polynoms[0]<<endl;
- cout<<"summ = "<<summ<<endl;
- cout<<"summ * polynoms[1] = "<<summ * polynoms[1]<<endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement