Advertisement
Guest User

Untitled

a guest
Apr 26th, 2015
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.65 KB | None | 0 0
  1. #include "iostream"
  2. #include "stdlib.h"
  3.  
  4. #define N 100
  5.  
  6. using namespace std;
  7.  
  8. class Polynom
  9. {
  10. private:
  11.     int count;
  12.     double *coef;
  13. public:
  14.     Polynom(const Polynom&);
  15.     Polynom(int);
  16.     ~Polynom(){}
  17.     Polynom operator+(Polynom);
  18.     Polynom operator-(Polynom);
  19.     Polynom operator*(Polynom);
  20.     Polynom& operator--();
  21.     Polynom operator--(int);
  22.     Polynom& operator++();
  23.     Polynom operator++(int);
  24.     double operator[](int i)const{return coef[i];}
  25.     const Polynom& operator=(const Polynom&);
  26.     void set_i_coef(int i, double coef_){coef[i] = coef_;}
  27.     void set_i_coef_pl(int i,double coef_){ coef[i] += coef_; }
  28.     void set_count(int);
  29.     void set_pol();
  30.     void set_pol_man();
  31.     void show_pol();
  32.     int get_count()const{ return count; }
  33.     friend ostream &operator<<(ostream &, const Polynom &);
  34. };
  35.  
  36. Polynom & Polynom::operator++()
  37. {
  38.    coef[0]++;
  39.    return *this;
  40. }
  41.  
  42. Polynom  Polynom ::operator++(int)
  43. {
  44.    Polynom  temp = *this;
  45.    ++*this;
  46.    return temp;
  47. }
  48.  
  49. Polynom & Polynom::operator--()
  50. {
  51.    coef[0]--;
  52.    return *this;
  53. }
  54.  
  55. Polynom  Polynom ::operator--(int)
  56. {
  57.    Polynom  temp = *this;
  58.    --*this;
  59.    return temp;
  60. }
  61.  
  62. ostream & operator<<(ostream &output, const Polynom &elem)
  63. {
  64.     int i = elem.get_count()-1;
  65.     for(;i>0;i--)
  66.         if(elem[i]!=0)
  67.             if(i!=1)
  68.                 output<<"x^"<<i<<"*("<<elem[i]<<")+";
  69.             else
  70.                 output<<"x*("<<elem[i]<<")+";
  71.     output<<elem[i];
  72.     return output;
  73. }
  74.  
  75. void Polynom::show_pol()
  76. {
  77.     cout<<"\nPolynom: \n";
  78.     for(int i=get_count()-1;i>=0;i--)
  79.         cout<<"x^"<<i<<"*("<<coef[i]<<")+";
  80.     cout<<endl;
  81. }
  82.  
  83. const Polynom& Polynom::operator=(const Polynom& right)
  84. {
  85.     set_count(right.get_count());
  86.     for(int i=0;i<count;i++)
  87.         coef[i] = right[i];
  88.     return *this;
  89. }
  90.  
  91. Polynom Polynom::operator*(Polynom right)
  92. {
  93.     int new_size = get_count()+right.get_count();
  94.     bool right_ = false;
  95.     Polynom new_pol(new_size);
  96.     for(int i=0;i<get_count();i++)
  97.         for(int j=0;j<right.get_count();j++)
  98.             new_pol.set_i_coef_pl(i+j,right[j]*coef[i]);
  99.     return new_pol;
  100. }
  101.  
  102.  
  103. Polynom Polynom::operator+(Polynom right)
  104. {
  105.     int new_size,new_size_2;
  106.     bool right_ = false;
  107.     if(get_count()>right.get_count())
  108.     {
  109.         new_size_2 = get_count();
  110.         new_size = right.get_count();
  111.     }
  112.     else
  113.     {
  114.         new_size_2 = right.get_count();
  115.         right_ = true;
  116.         new_size = get_count();
  117.     }
  118.     Polynom new_pol(new_size_2);
  119.     for(int i=0;i<new_size;i++)
  120.         new_pol.set_i_coef(i,coef[i]+right[i]);
  121.     if(right_)
  122.         for(int i=new_size;i<right.get_count();i++)
  123.             new_pol.set_i_coef(i,right[i]);
  124.     else
  125.         for(int i=new_size;i<get_count();i++)
  126.             new_pol.set_i_coef(i,coef[i]);
  127.     return new_pol;
  128. }
  129.  
  130. Polynom Polynom::operator-(Polynom right)
  131. {
  132.     int new_size,new_size_2;
  133.     bool right_ = false;
  134.     if(get_count()>right.get_count())
  135.     {
  136.         new_size_2 = get_count();
  137.         new_size = right.get_count();
  138.     }
  139.     else
  140.     {
  141.         new_size_2 = right.get_count();
  142.         right_ = true;
  143.         new_size = get_count();
  144.     }
  145.     Polynom new_pol(new_size_2);
  146.     for(int i=0;i<new_size;i++)
  147.         new_pol.set_i_coef(i,coef[i]-right[i]);
  148.     if(right_)
  149.         for(int i=new_size;i<right.get_count();i++)
  150.             new_pol.set_i_coef(i,right[i]);
  151.     else
  152.         for(int i=new_size;i<get_count();i++)
  153.             new_pol.set_i_coef(i,coef[i]);
  154.     return new_pol;
  155. }
  156.  
  157. void Polynom::set_count(int i=-1)
  158. {
  159.     int count_;
  160.     if(i<0)
  161.     {
  162.         cout<<"\nEnter new count: ";
  163.         cin>>count_;
  164.     }
  165.     else
  166.         count_ = i;
  167.     coef = new double[count_];
  168.     count = count_;
  169. }
  170.  
  171. Polynom::Polynom(const Polynom& right)
  172. {
  173.     set_count(right.get_count());
  174.     for(int i=0;i<right.get_count();i++)
  175.         set_i_coef(i,right[i]);
  176. }
  177.  
  178. Polynom::Polynom(int count_ = 0)
  179. {
  180.     coef = new double[count_];
  181.     count = count_;
  182.     for(int i=0;i<get_count();i++)
  183.         set_i_coef(i,0);
  184. }
  185.  
  186. void Polynom::set_pol()
  187. {
  188.     for(int i=0;i<count;i++)
  189.         coef[i] = rand()/N;
  190. }
  191.  
  192. void Polynom::set_pol_man()
  193. {
  194.     cout<<"/nEnter the coefficient of degrees ";
  195.     for(int i=0;i<count;i++)
  196.     {
  197.         cout<<"\n "<<i<<":";
  198.         cin>>coef[i];
  199.     }
  200.     cout<<"\n";
  201. }
  202.  
  203. Polynom sum(Polynom* arr, int n)
  204. {
  205.     Polynom sum;
  206.     for(int i=0;i<n;i++)
  207.         sum = sum + arr[i];
  208.     return sum;
  209. }
  210.  
  211. int main()
  212. {
  213.     srand(0);
  214.     Polynom *polynoms;
  215.     cout<<"Enter num of polynoms: ";
  216.     int n; cin>>n;
  217.     polynoms = new Polynom[n];
  218.     for(int i=0;i<n;i++)
  219.     {
  220.         cout<<"\nEnter size of pol: ";
  221.         int x; cin>>x;
  222.         polynoms[i] = Polynom(x);
  223.         polynoms[i].set_pol();
  224.     }
  225.     cout<<"Polynoms: \n\n";
  226.     for(int i=0;i<n;i++)
  227.         cout<<polynoms[i]<<endl;
  228.     Polynom summ;
  229.     summ  = sum(polynoms,n);
  230.     cout<<summ;
  231.     cout<<"summ++ = "<<summ++<<endl;
  232.     cout<<"summ = "<<summ<<endl;
  233.     cout<<"--summ = "<<--summ<<endl;
  234.     cout<<"summ - polynoms[0] = "<<summ - polynoms[0]<<endl;
  235.     cout<<"summ  = "<<summ<<endl;
  236.     cout<<"summ * polynoms[1] = "<<summ * polynoms[1]<<endl;
  237.     return 0;
  238. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement