Hellko

фывфыв

May 22nd, 2013
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.13 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) const;
  16.     friend arr_vec operator+(const arr_vec&,const arr_vec&);
  17.     friend arr_vec operator*(const arr_vec&,double);
  18.     friend arr_vec operator*(double,const arr_vec&);
  19.     void show();
  20. };
  21. vec operator+(vec a, vec b);
  22. vec operator*(vec a, double b);
  23.  
  24. vec toVec(double x,double y, double z) {
  25.     vec tmp;
  26.     tmp.x=x;
  27.     tmp.y=y;
  28.     tmp.z=z;
  29.     return tmp;
  30. }
  31.  
  32. arr_vec::arr_vec(int N) {
  33.     n=N;
  34.     x=new double[n];
  35.     y=new double[n];
  36.     z=new double[n];
  37.     for(int i=0;i<n;i++) {x[i]=i; y[i]=i+1; z[i]=i+2;} //по умолчанию.
  38. }
  39.  
  40. arr_vec::~arr_vec() {
  41.     delete[]x;
  42.     delete[]y;
  43.     delete[]z;
  44. }
  45.  
  46. void arr_vec::set(int N, vec tmp) {
  47.     if(n<N) return;
  48.     x[N]=tmp.x;
  49.     y[N]=tmp.y;
  50.     z[N]=tmp.z;
  51. }
  52.  
  53. vec arr_vec::operator [](int N) const {
  54.     return toVec(x[N],y[N],z[N]);
  55. }
  56.  
  57. arr_vec operator+(const arr_vec& a,const arr_vec& b) {
  58.     arr_vec c(a.n);
  59.     for(int i=0; i<c.n; i++)
  60.         c.set(i,a[i]+b[i]);
  61.     return c;
  62. }
  63.  
  64. vec operator+(vec a, vec b) {
  65.     return toVec(a.x+b.x,a.y+b.y,a.z+b.z);
  66. }
  67.  
  68. vec operator*(vec a, double b) {
  69.     return toVec(a.x*b,a.y*b,a.z*b);
  70. }
  71.  
  72. arr_vec operator*(const arr_vec& a,double k) {
  73.     arr_vec c(a.n);
  74.     for(int i=0; i<c.n; i++) {
  75.         c.set(i,a[i]*k);
  76.     }
  77.     return c;
  78. }
  79.  
  80. arr_vec operator*(double k, const arr_vec& a) {
  81.     return a*k;
  82. }
  83.  
  84. void arr_vec::show() {
  85.     cout<<endl<< "X: "; for(int i=0;i<n;i++) cout<<x[i]<<"  ";
  86.     cout<<endl<< "Y: "; for(int i=0;i<n;i++) cout<<y[i]<<"  ";
  87.     cout<<endl<< "Z: "; for(int i=0;i<n;i++) cout<<z[i]<<"  ";
  88.     cout<<endl;
  89. }
  90.  
  91. int main() {
  92.     arr_vec a(2),b(2);
  93.     vec a1,a2,a3,a4;
  94.     a1=toVec(1,1,1);
  95.     a2=toVec(2,1,0);
  96.     a3=toVec(4,3,5);
  97.     a4=toVec(1,-1,9);
  98.     a.set(0,a1);
  99.     a.set(1,a2);
  100.     b.set(0,a3);
  101.     b.set(1,a4);
  102.     a.show();
  103.     b.show();
  104.     (a+b).show();
  105.     a.show();
  106.     (a*5).show();
  107. }
Advertisement
Add Comment
Please, Sign In to add comment