Hellko

#6

May 12th, 2013
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.01 KB | None | 0 0
  1. //////////////////////   XVI VARIANT   ////////////////////////////
  2. #include <stdio.h>
  3. ///////////////////////////////////////////////////////////////////
  4. ////////////////////////   array   ////////////////////////////////
  5. ///////////////////////////////////////////////////////////////////
  6. class array {
  7. protected:
  8.     int* arr;
  9.     int n;
  10. public:
  11.     array();
  12.     array(int N);
  13.     array(int N, int* ARR);
  14.     array(const array& A);
  15.     ~array();
  16.     int& operator[](int i) {return arr[i];}
  17.     const int& operator[](int i) const {return arr[i];}
  18.     void show();
  19.     void operator=(const array& A);
  20.     friend array operator+(const array&,const array&);
  21.     friend array operator*(const array&,const array&);
  22. };
  23. array operator+(const array& A, const array& B) {
  24.     array C(A.n);
  25.     for(int i=0;i<A.n;i++) C[i]=A[i]+B[i];
  26.     return C;
  27. }
  28. \
  29. array operator*(const array& A, const array& B) {
  30.     array C(A.n);
  31.     for(int i=0;i<A.n;i++) C[i]=A[i]*B[i];
  32.     return C;
  33. }
  34.  
  35. void array::operator=(const array& A) {
  36.     n=A.n;
  37.     arr=new int [n];
  38.     for(int i=0; i<n; i++) arr[i]=A.arr[i];
  39. }
  40.  
  41. void array::show() {
  42.     for(int i=0; i<n; i++) printf(" [%d]: %3d",i,arr[i]);
  43.     printf("\n");
  44. }
  45.  
  46. array::~array() {
  47.     delete [] arr;
  48. }
  49.  
  50. array::array() {
  51.     n=0;
  52.     arr=NULL;
  53. }
  54.  
  55. array::array(int N) {
  56.     n=N;
  57.     arr=new int [n];
  58. }
  59.  
  60. array::array(int N, int *ARR) {
  61.     n=N;
  62.     arr=new int [n];
  63.     for(int i=0; i<n; i++) arr[i]=ARR[i];
  64. }
  65.  
  66. array::array(const array &A) {
  67.     n=A.n;
  68.     arr=new int [n];
  69.     for(int i=0; i<n; i++) arr[i]=A.arr[i];
  70. }
  71.  
  72. ///////////////////////////////////////////////////////////////////
  73. ////////////////////////  polynom  ////////////////////////////////
  74. ///////////////////////////////////////////////////////////////////
  75. ///     a[0]x^2+a[1]x+a[2] ; a[3]x^2+...                    ///////
  76. ///////////////////////////////////////////////////////////////////
  77. class polynom: public array {
  78. public:
  79.     polynom(): array() {}
  80.     polynom(int N): array(3*N) {}
  81.     polynom(const polynom& A): array((array)A) {}
  82.     void operator=(const polynom& A);
  83.     void show();
  84.     polynom derive() const;
  85.     array operator()(int) const;
  86.     friend array operator*(const polynom&, const polynom&);
  87.     friend polynom operator+(const polynom&, const polynom&);
  88.     friend polynom operator-(const polynom&, const polynom&);
  89. };
  90. polynom operator-(const polynom& A, const polynom& B) {
  91.     polynom C(A);
  92.     for(int i=0; i<C.n; i++) C[i]-=B[i];
  93.     return C;
  94. }
  95.  
  96. polynom operator+(const polynom& A, const polynom& B) {
  97.     polynom C(A);
  98.     for(int i=0; i<C.n; i++) C[i]+=B[i];
  99.     return C;
  100. }
  101.  
  102. array operator*(const polynom& A, const polynom& B) {
  103.     return A.derive()(-1)*B.derive()(-1)+A.derive()(0)*B.derive()(0)+A.derive()(1)*B.derive()(1);
  104. }
  105.  
  106. array polynom::operator ()(int x) const {
  107.     array A(n/3);
  108.     for(int i=0; i<n/3; i++) A[i]=(*this)[3*i]*x*x+(*this)[3*i+1]*x+(*this)[3*i+2];
  109.     return A;
  110. }
  111.  
  112. polynom polynom::derive() const {
  113.     polynom C(n/3);
  114.     for(int i=0; i<n; i++) {
  115.         if(i%3==0) C[i]=0;
  116.         if(i%3==1) C[i]=(*this)[i-1]*2;
  117.         if(i%3==2) C[i]=(*this)[i-1];
  118.     }
  119.     return C;
  120. }
  121.  
  122. void polynom::show() {
  123.     for(int i=0; i<n; i=i+3) printf(" [%d]: %3dx^2%+3dx%+3d",i/3,arr[i],arr[i+1],arr[i+2]);
  124.     printf("\n");
  125. }
  126.  
  127. void polynom::operator =(const polynom &A) {
  128.     n=A.n;
  129.     arr=new int [n];
  130.     for(int i=0; i<n; i++) arr[i]=A.arr[i];
  131. }
  132.  
  133. int main() {
  134.     int n=3;
  135.     polynom A(n);
  136.     for(int i=0; i<n*3; i++) A[i]=i+1;
  137.     polynom B(n);
  138.     for(int i=0; i<n*3; i++) B[i]=i*i+1;
  139.     printf("\tPolynom A:\n");
  140.     A.show();
  141.     printf("\n\tPolynom B:\n");
  142.     B.show();
  143.     printf("\n\tProizvodnaya A:\n");
  144.     A.derive().show();
  145.     printf("\n\tProizvodnaya B:\n");
  146.     B.derive().show();
  147.     printf("\n\tScalarnoe proizvedenie A*B: A'(0)B'(0)+A'(-1)B'(-1)+A'(1)B'(1):\n");
  148.     (A*B).show();
  149.     printf("\n\tA+B:\n");
  150.     (A+B).show();
  151.     printf("\n\tA+B v vide \"array\":\n");
  152.     (A+B).array::show();
  153. }
Advertisement
Add Comment
Please, Sign In to add comment