Advertisement
Guest User

EX3_vector

a guest
Mar 11th, 2012
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.69 KB | None | 0 0
  1. //Trong bai nay ta gia su nhan, cong cac vector co cung so cheu
  2. //Vi thay giao noi nhu vay!!!!!!!hehe
  3. //Con neu de xet cac vec to khac chieu thi phuc tap hon the nay!!!!xet sau!!hehe
  4. #include<iostream>
  5. using namespace std;
  6. class vector
  7. {
  8.     private:
  9.     int n;
  10.     double *v;
  11.     public:
  12.     vector()
  13.     {
  14.         n=0;
  15.     }
  16.     vector(int n1)
  17.     {
  18.         n=n1;
  19.         v=new double[n];
  20.         for(int i=0;i<n;i++)
  21.             v[i]=0;
  22.     }
  23.     ~vector()
  24.     {
  25.         delete [] v;
  26.     }
  27.     vector multiply(double x)
  28.     {
  29.         vector u(n);
  30.         for(int i=0;i<n;i++)
  31.             u.v[i]=(v[i])*x;
  32.         return u;
  33.     }
  34.     double multiply(vector &u)
  35.     {
  36.         double multip=0;
  37.         for(int i=0;i<n;i++)
  38.             multip+=(v[i])*u.v[i];
  39.         return multip;
  40.     }
  41.     void add(vector &u)
  42.     {
  43.         for(int i=0;i<n;i++)
  44.             v[i]+=u.v[i];
  45.     }
  46.     void copyv(vector &u)
  47.     {
  48.         for(int i=0;i<n;i++)
  49.             v[i]=u.v[i];
  50.     }
  51.     void reset(int n1)
  52.     {
  53.         n=n1;
  54.         for(int i=0;i<n;i++)
  55.             v[i]=0;
  56.     }
  57.     void input()
  58.     {
  59.         for(int i=0;i<n;i++)
  60.         {
  61.             double a;
  62.             cin>>a;
  63.             v[i]=a;
  64.         }
  65.     }
  66.     void display()
  67.     {
  68.         for(int i=0;i<n;i++)
  69.             cout<<(v[i])<<endl;
  70.        
  71.     }
  72. };
  73. int main()
  74. {
  75.     cout<<"Input the dimension of vector:"<<endl;
  76.     int nx;
  77.     cin>>nx;
  78.     vector x(nx),y(nx);
  79.     cout<<"Input the component of x:"<<endl;
  80.     x.input();
  81.     cout<<"Input the component of y:"<<endl;
  82.     y.input();
  83.     cout<<"Input one real number:"<<endl;
  84.     double re;
  85.     cin>>re;
  86.     cout<<"x*re"<<endl;
  87.     (x.multiply(re)).display();
  88.     double mult;
  89.     mult=x.multiply(y);
  90.     cout<<"x*y="<<mult<<endl;
  91.     vector z1(nx);
  92.     z1.copyv(x);
  93.     cout<<"z1=x"<<endl;
  94.     z1.display();
  95.     cout<<"x+y="<<endl;
  96.     x.add(y);
  97.     x.display();
  98.     cout<<"Input n1 is new dimension to reset x:"<<endl;
  99.     cin>>nx;
  100.     x.reset(nx);
  101.     cout<<"x after reset:"<<endl;
  102.     x.display();
  103.     return 0;
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement