Hellko

Untitled

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