Hellko

asd

May 22nd, 2013
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.51 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct vec {double x,y,z;};
  5. class arr_vec {
  6. private:
  7.     double *x;
  8.     double *y;
  9.     double *z;
  10.     int n;
  11. public:
  12.     arr_vec(int);
  13.     ~arr_vec();
  14.     void set(int,vec);
  15.     vec operator[](int);
  16.     friend arr_vec operator+(arr_vec,arr_vec);
  17.     friend arr_vec operator*(arr_vec,double);
  18.     friend arr_vec operator*(double,arr_vec);
  19. };
  20. vec operator+(vec a, vec b);
  21. vec operator*(vec a, double b);
  22.  
  23. vec toVec(double x,double y, double z) {
  24.     vec tmp;
  25.     tmp.x=x;
  26.     tmp.y=y;
  27.     tmp.z=z;
  28.     return tmp;
  29. }
  30.  
  31. arr_vec::arr_vec(int N) {
  32.     n=N;
  33.     x=new double[n];
  34.     y=new double[n];
  35.     z=new double[n];
  36.     for(int i=0;i<n;i++) {x[i]=i; y[i]=i+1; z[i]=i+2;} //по умолчанию.
  37. }
  38.  
  39. arr_vec::~arr_vec() {
  40.     delete[]x;
  41.     delete[]y;
  42.     delete[]z;
  43. }
  44.  
  45. void arr_vec::set(int N, vec tmp) {
  46.     if(n<N) return;
  47.     x[N]=tmp.x;
  48.     y[N]=tmp.y;
  49.     z[N]=tmp.z;
  50. }
  51.  
  52. vec arr_vec::operator [](int N) {
  53.     return toVec(x[N],y[N],z[N]);
  54. }
  55.  
  56. arr_vec operator+(arr_vec a,arr_vec b) {
  57.     arr_vec c(a.n);
  58.     for(int i=0; i<c.n; i++)
  59.         c.set(i,a[i]+b[i]);
  60.     return c;
  61. }
  62.  
  63. vec operator+(vec a, vec b) {
  64.     return toVec(a.x+b.x,a.y+b.y,a.z+b.z);
  65. }
  66.  
  67. vec operator*(vec a, double b) {
  68.     return toVec(a.x*b,a.y*b,a.z*b);
  69. }
  70.  
  71. arr_vec operator*(arr_vec a,double k) {
  72.     arr_vec c(a.n);
  73.     for(int i=0; i<c.n; i++) {
  74.         c.set(i,a[i]*k);
  75.     }
  76.     return c;
  77. }
  78.  
  79. arr_vec operator*(double k, arr_vec a) {
  80.     return a*k;
  81. }
Advertisement
Add Comment
Please, Sign In to add comment