Hellko

фывфаыйфц

May 27th, 2013
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class cube{
  5.         friend class a_cube;
  6. private:
  7.         double x,y,z,r;
  8. public:
  9.         cube(double X=1, double Y=1, double Z=1, double R=1){x=X;y=Y;z=Z;r=R;}
  10.         cube (const cube& A){x=A.x;y=A.y;z=A.z;r=A.r;}
  11.         void operator=(const cube& A){x=A.x; y=A.y; z=A.z; r=A.r;}
  12.         friend cube operator-(cube,cube);
  13.         friend cube operator*(cube, double);
  14.         friend cube transform(double,double,double,double);
  15.         };
  16.  
  17. class a_cube {
  18. private:
  19. cube *x; //указатель на куб
  20. int n;
  21. public:
  22.         a_cube(int);
  23.         a_cube(const a_cube&);
  24.         ~a_cube();
  25.         void set(int,cube);//вставляем куб на позицию
  26.         void operator=(const a_cube&);//оператор присваивания
  27.         const cube operator[](int) const;//возвращаем с позиции
  28.         cube& operator[](int N) {return x[N];}  // <<<<<<<<<< новый оператор)
  29.         friend a_cube operator-(const a_cube&, const a_cube&);
  30.         friend a_cube operator*(const a_cube&, double);//оператор умножения
  31.         friend a_cube operator*(double, const a_cube&);
  32.         void show();
  33.         void del(int);
  34.         void add(const cube&);
  35. };
  36. void a_cube::add(const cube &A) {
  37.     cube *tmp;
  38.     tmp=new cube [n+1];
  39.     for(int i=0; i<n; i++) tmp[i]=x[i];
  40.     tmp[n]=A;
  41.     delete[]x;
  42.     x=tmp;
  43.     n++;
  44. }
  45.  
  46. void a_cube::del(int N){
  47.         cube *tmp;
  48.         tmp=new cube[n-1];
  49.         for(int i=0;i<N;i++){
  50.                 tmp[i]=x[i];
  51.                 for(int i=N+1;i<n;i++){
  52.                         tmp[i-1]=x[i];
  53.                 }
  54.                 delete[]x;
  55.                 x=tmp;
  56.                 n--;
  57.         }
  58.         cube operator+(cube a, cube b);
  59.         cube operator*(cube a, double b);
  60. }
  61. a_cube::a_cube(const a_cube& A){//копиконструктор
  62.                 n=A.n;
  63.                 x=new cube [n];
  64.                 for(int i=0;i<n;i++)
  65.                         x[i]=A.x[i];
  66.         }
  67. void a_cube::operator=(const a_cube& A){//оператор присваивания
  68.                 n=A.n;
  69.                 x=new cube[n];
  70.                 for(int i=0;i<n;i++)
  71.                         x[i]=A.x[i];
  72.         }
  73. cube transform(double x, double y, double z, double r){
  74.                 cube tmp;
  75.                 tmp.x=x;
  76.                 tmp.y=y;
  77.                 tmp.z=z;
  78.                 tmp.r=r;
  79.                 return tmp;
  80.         }
  81. a_cube::a_cube(int N){//конструктор
  82.         n=N;
  83.         x=new cube[n];
  84.         for (int i=0;i<n;i++){
  85.                 x[i].x=i;x[i].y=i+1;x[i].z=i+2;x[i].r=i+3;}
  86. }
  87. a_cube::~a_cube(){//деструктор
  88.         delete[]x;
  89. }
  90. void a_cube::set(int N, cube tmp){
  91.         if(n<N) return;
  92.         x[N]=tmp;
  93. }
  94. const cube a_cube::operator[](int N)const{
  95.         return x[N];
  96. }
  97. a_cube operator-(const a_cube& a, const a_cube& b){
  98.         a_cube c(a.n);
  99.         for(int i=0;i<c.n;i++)
  100.                 c.set(i,a[i]-b[i]);
  101.         return c;
  102. }
  103.  
  104. cube operator-(cube a, cube b){
  105.         return transform(a.x-b.x,a.y-b.y,a.z-b.z,a.r-b.r);
  106. }
  107. cube operator*(cube a, double b){
  108.         return transform(a.x*b,a.y*b,a.z*b,a.r*b);
  109. }
  110. a_cube operator*(double k, const a_cube& a){
  111.         a_cube c(a.n);
  112.                 for(int i=0;i<c.n;i++)
  113.                         c.set(i, a[i]*k);
  114.         return c;
  115. }
  116.  
  117. a_cube operator*(const a_cube& a,double k){
  118.         return a*k;}
  119.  
  120. void a_cube::show(){
  121.         cout<<endl<<"X:"; for(int i=0;i<n;i++) cout<<x[i].x<<"  ";
  122.         cout<<endl<<"Y:"; for(int i=0;i<n;i++) cout<<x[i].y<<"  ";
  123.         cout<<endl<<"Z:"; for(int i=0;i<n;i++) cout<<x[i].z<<"  ";
  124.         cout<<endl<<"R:"; for(int i=0;i<n;i++) cout<<x[i].r<<"  ";
  125.         cout<<endl;
  126. }
  127. int main() {
  128. a_cube a(5),b(5);
  129. cube a1,b1,c1,d1;
  130. a1=transform(1,1,1,1);
  131. b1=transform(1,2,3,4);
  132. a.set(0,a1);
  133. b.set(1,b1);
  134. a.show();
  135. b.show();
  136. (a-b).show();
  137. a.add(a1);
  138. a.show();
  139. }
Advertisement
Add Comment
Please, Sign In to add comment