Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ////////////////////// XVI VARIANT ////////////////////////////
- #include <stdio.h>
- ///////////////////////////////////////////////////////////////////
- //////////////////////// array ////////////////////////////////
- ///////////////////////////////////////////////////////////////////
- class array {
- protected:
- int* arr;
- int n;
- public:
- array();
- array(int N);
- array(int N, int* ARR);
- array(const array& A);
- ~array();
- int& operator[](int i) {return arr[i];}
- const int& operator[](int i) const {return arr[i];}
- void show();
- void operator=(const array& A);
- friend array operator+(const array&,const array&);
- friend array operator*(const array&,const array&);
- };
- array operator+(const array& A, const array& B) {
- array C(A.n);
- for(int i=0;i<A.n;i++) C[i]=A[i]+B[i];
- return C;
- }
- \
- array operator*(const array& A, const array& B) {
- array C(A.n);
- for(int i=0;i<A.n;i++) C[i]=A[i]*B[i];
- return C;
- }
- void array::operator=(const array& A) {
- n=A.n;
- arr=new int [n];
- for(int i=0; i<n; i++) arr[i]=A.arr[i];
- }
- void array::show() {
- for(int i=0; i<n; i++) printf(" [%d]: %3d",i,arr[i]);
- printf("\n");
- }
- array::~array() {
- delete [] arr;
- }
- array::array() {
- n=0;
- arr=NULL;
- }
- array::array(int N) {
- n=N;
- arr=new int [n];
- }
- array::array(int N, int *ARR) {
- n=N;
- arr=new int [n];
- for(int i=0; i<n; i++) arr[i]=ARR[i];
- }
- array::array(const array &A) {
- n=A.n;
- arr=new int [n];
- for(int i=0; i<n; i++) arr[i]=A.arr[i];
- }
- ///////////////////////////////////////////////////////////////////
- //////////////////////// polynom ////////////////////////////////
- ///////////////////////////////////////////////////////////////////
- /// a[0]x^2+a[1]x+a[2] ; a[3]x^2+... ///////
- ///////////////////////////////////////////////////////////////////
- class polynom: public array {
- public:
- polynom(): array() {}
- polynom(int N): array(3*N) {}
- polynom(const polynom& A): array((array)A) {}
- void operator=(const polynom& A);
- void show();
- polynom derive() const;
- array operator()(int) const;
- friend array operator*(const polynom&, const polynom&);
- friend polynom operator+(const polynom&, const polynom&);
- friend polynom operator-(const polynom&, const polynom&);
- };
- polynom operator-(const polynom& A, const polynom& B) {
- polynom C(A);
- for(int i=0; i<C.n; i++) C[i]-=B[i];
- return C;
- }
- polynom operator+(const polynom& A, const polynom& B) {
- polynom C(A);
- for(int i=0; i<C.n; i++) C[i]+=B[i];
- return C;
- }
- array operator*(const polynom& A, const polynom& B) {
- return A.derive()(-1)*B.derive()(-1)+A.derive()(0)*B.derive()(0)+A.derive()(1)*B.derive()(1);
- }
- array polynom::operator ()(int x) const {
- array A(n/3);
- for(int i=0; i<n/3; i++) A[i]=(*this)[3*i]*x*x+(*this)[3*i+1]*x+(*this)[3*i+2];
- return A;
- }
- polynom polynom::derive() const {
- polynom C(n/3);
- for(int i=0; i<n; i++) {
- if(i%3==0) C[i]=0;
- if(i%3==1) C[i]=(*this)[i-1]*2;
- if(i%3==2) C[i]=(*this)[i-1];
- }
- return C;
- }
- void polynom::show() {
- 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]);
- printf("\n");
- }
- void polynom::operator =(const polynom &A) {
- n=A.n;
- arr=new int [n];
- for(int i=0; i<n; i++) arr[i]=A.arr[i];
- }
- int main() {
- int n=3;
- polynom A(n);
- for(int i=0; i<n*3; i++) A[i]=i+1;
- polynom B(n);
- for(int i=0; i<n*3; i++) B[i]=i*i+1;
- printf("\tPolynom A:\n");
- A.show();
- printf("\n\tPolynom B:\n");
- B.show();
- printf("\n\tProizvodnaya A:\n");
- A.derive().show();
- printf("\n\tProizvodnaya B:\n");
- B.derive().show();
- printf("\n\tScalarnoe proizvedenie A*B: A'(0)B'(0)+A'(-1)B'(-1)+A'(1)B'(1):\n");
- (A*B).show();
- printf("\n\tA+B:\n");
- (A+B).show();
- printf("\n\tA+B v vide \"array\":\n");
- (A+B).array::show();
- }
Advertisement
Add Comment
Please, Sign In to add comment