Hellko

фывфыайцйу

May 27th, 2013
80
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 vec {
  5.         friend class arr_vec;
  6.         private: double x, y, z;
  7.         public: friend vec operator+ (vec, vec);
  8.                         friend vec operator* (vec, double);
  9.                         friend vec operator* (double, vec);
  10.                         vec(double a=1, double b=1, double c=1) {x=a; y=b; z=c;}
  11.                         friend vec toVec(double x, double y, double z);
  12.                         void show() {
  13.                                 cout<<endl<<"X: "; cout<<x;
  14.                                 cout<<endl<<"Y: "; cout<<y;
  15.                                 cout<<endl<<"Z: "; cout<<z;
  16.                                 cout<<endl;
  17.                                 }
  18. };
  19.  
  20. vec toVec(double x, double y, double z) {
  21.         vec v;
  22.         v.x=x;
  23.         v.y=y;
  24.         v.z=z;
  25.         return v;
  26. }
  27. vec operator+ (vec c, vec d) {
  28.         return toVec(c.x+d.x, c.y+d.y, c.z+d.z);}
  29.  
  30. vec operator* (vec c, double d) {
  31.         return toVec(c.x*d, c.y*d, c.z*d);}
  32.  
  33. vec operator* (double c, vec d) {
  34.         return toVec(c*d.x, c*d.y, c*d.z);}
  35.  
  36. class arr_vec {
  37.         private: double *x, *y, *z;
  38.                                 int n;
  39.         public: arr_vec(int);
  40.                         ~arr_vec();
  41.                         void put(int, vec);
  42.                         vec operator[] (int) const;
  43.                         void operator=(const arr_vec&);
  44.                         friend arr_vec operator+ (const arr_vec&, const arr_vec&);
  45.                         friend arr_vec operator* (double, const arr_vec&);
  46.                         friend arr_vec operator* (const arr_vec&, double);
  47.                         void show();
  48.                         void add(const vec&);
  49. };
  50.  
  51. void arr_vec::add(const vec &A) {
  52.     double *tmpx, *tmpy, *tmpz;
  53.     tmpx = new double [n+1];
  54.     tmpy = new double [n+1];
  55.     tmpz = new double [n+1];
  56.     for(int i=0; i<n; i++) {
  57.         tmpx[i]=x[i];
  58.         tmpy[i]=y[i];
  59.         tmpz[i]=z[i];
  60.     }
  61.     tmpx[n]=A.x;
  62.     tmpy[n]=A.y;
  63.     tmpz[n]=A.z;
  64.     delete[]x; delete[]y; delete[]z;
  65.     x=tmpx;
  66.     y=tmpy;
  67.     z=tmpz;
  68.     n++;
  69. }
  70.  
  71. arr_vec::arr_vec(int N) {
  72.         n=N;
  73.         x=new double[n];
  74.         y=new double[n];
  75.         z=new double[n];
  76.         for(int i=0; i<n; i++) {x[i]=y[i]=z[i]=0;}
  77. }
  78. arr_vec::~arr_vec() {
  79.         delete[]x;
  80.         delete[]y;
  81.         delete[]z;
  82. }
  83. void arr_vec::put(int N, vec v) {
  84.         if (n<N) return;
  85.         x[N]=v.x;
  86.         y[N]=v.y;
  87.         z[N]=v.z;
  88.         //v.x=x[N+1];
  89.         //v.y=y[N+1];
  90.         //v.z=z[N+1];
  91. }
  92. vec arr_vec::operator [](int N) const {
  93.         return toVec(x[N], y[N], z[N]);
  94. }
  95. arr_vec operator+ (const arr_vec& c, const arr_vec& d) {
  96.         arr_vec *e;
  97.         e=new arr_vec(c.n);
  98.         for(int i=0; i<e->n; i++) e->put(i, c[i]+d[i]);
  99.         return *e;
  100. }
  101. arr_vec operator* (double c, const arr_vec& d) {
  102.         arr_vec *e;
  103.         e=new arr_vec(d.n);
  104.         for (int i=0; i<e->n; i++) e->put(i, c*d[i]);
  105.         return *e;
  106. }
  107. arr_vec operator* (const arr_vec& c, double d) {
  108.         arr_vec *e;
  109.         e=new arr_vec(c.n);
  110.         for (int i=0; i<e->n; i++) e->put(i, c[i]*d);
  111.         return *e;
  112. }
  113. void arr_vec::show() {
  114.         cout<<endl<<"X: ";
  115.                 for(int i=0; i<n; i++) cout<<x[i]<<"\t";
  116.         cout<<endl<<"Y: ";
  117.                 for(int i=0; i<n; i++) cout<<y[i]<<"\t";
  118.         cout<<endl<<"Z: ";
  119.                 for(int i=0; i<n; i++) cout<<z[i]<<"\t";
  120.         cout<<endl;
  121. }
  122. void arr_vec::operator =(const arr_vec& A) {
  123.         n=A.n;
  124.         x=new double[n];
  125.         y=new double[n];
  126.         x=new double[n];
  127.         for(int i=0; i<n; i++) {x[i]=A.x[i]; y[i]=A.y[i]; z[i]=A.z[i];}
  128. }
  129.  
  130. int main() {
  131.         double g=3.;
  132.         arr_vec c(2), d(2);
  133.         vec e(1,2,3);
  134.         vec f(4,5,6);
  135.         c.put(0, e);
  136.         c.show();
  137.         d.put(1,f);
  138.         d.show();
  139.         (c+d).show();
  140.         (g*c).show();
  141.         c=c+d; c.show();
  142.         c.put(0,vec(0,0,0)); c.show();
  143.         (d[1]).show();
  144.         c.add(vec(666,666,666));
  145.         c.show();
  146.  
  147. }
Advertisement
Add Comment
Please, Sign In to add comment