Hellko

asd

May 26th, 2013
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.20 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct Poly {double x,y,z;};
  5. class arr_Poly {
  6. private:
  7.     double *x;
  8.     double *y;
  9.     double *z;
  10.     int n;
  11. public:
  12.     arr_Poly(int);
  13.     ~arr_Poly();
  14.     void set(int,Poly);
  15.     Poly operator[](int) const;
  16.     friend arr_Poly operator+(const arr_Poly&,const arr_Poly&);
  17.     friend arr_Poly operator*(const arr_Poly&,double);
  18.     friend arr_Poly operator*(double,const arr_Poly&);
  19.     void show();
  20. };
  21. Poly operator+(Poly a, Poly b);
  22. Poly operator*(Poly a, double b);
  23.  
  24. Poly toPoly(double x,double y, double z) {
  25.     Poly tmp;
  26.     tmp.x=x;
  27.     tmp.y=y;
  28.     tmp.z=z;
  29.     return tmp;
  30. }
  31.  
  32. arr_Poly::arr_Poly(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_Poly::~arr_Poly() {
  41.     delete[]x;
  42.     delete[]y;
  43.     delete[]z;
  44. }
  45.  
  46. void arr_Poly::set(int N, Poly tmp) {
  47.     if(n<N) return;
  48.     x[N]=tmp.x;
  49.     y[N]=tmp.y;
  50.     z[N]=tmp.z;
  51. }
  52.  
  53. Poly arr_Poly::operator [](int N) const {
  54.     return toPoly(x[N],y[N],z[N]);
  55. }
  56.  
  57. arr_Poly operator+(const arr_Poly& a,const arr_Poly& b) {
  58.     arr_Poly 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. Poly operator+(Poly a, Poly b) {
  65.     return toPoly(a.x+b.x,a.y+b.y,a.z+b.z);
  66. }
  67.  
  68. Poly operator*(Poly a, double b) {
  69.     return toPoly(a.x*b,a.y*b,a.z*b);
  70. }
  71.  
  72. arr_Poly operator*(const arr_Poly& a,double k) {
  73.     arr_Poly 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_Poly operator*(double k, const arr_Poly& a) {
  81.     return a*k;
  82. }
  83.  
  84. void arr_Poly::show() {
  85.     cout<<endl<<"(a)  X^2: "; for(int i=0;i<n;i++) cout<<x[i]<<"  ";
  86.     cout<<endl<<"(b)    X: "; for(int i=0;i<n;i++) cout<<y[i]<<"  ";
  87.     cout<<endl<<"(c)     : "; for(int i=0;i<n;i++) cout<<z[i]<<"  ";
  88.     cout<<endl;
  89. }
  90.  
  91. int main() {
  92.     arr_Poly a(2),b(2);
  93.     Poly a1,a2,a3,a4;
  94.     a1=toPoly(1,1,1);
  95.     a2=toPoly(2,1,0);
  96.     a3=toPoly(4,3,5);
  97.     a4=toPoly(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