Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Trong bai nay ta gia su nhan, cong cac vector co cung so cheu
- //Vi thay giao noi nhu vay!!!!!!!hehe
- //Con neu de xet cac vec to khac chieu thi phuc tap hon the nay!!!!xet sau!!hehe
- #include<iostream>
- using namespace std;
- class vector
- {
- private:
- int n;
- double *v;
- public:
- vector()
- {
- n=0;
- }
- vector(int n1)
- {
- n=n1;
- v=new double[n];
- for(int i=0;i<n;i++)
- v[i]=0;
- }
- ~vector()
- {
- delete [] v;
- }
- vector multiply(double x)
- {
- vector u(n);
- for(int i=0;i<n;i++)
- u.v[i]=(v[i])*x;
- return u;
- }
- double multiply(vector &u)
- {
- double multip=0;
- for(int i=0;i<n;i++)
- multip+=(v[i])*u.v[i];
- return multip;
- }
- void add(vector &u)
- {
- for(int i=0;i<n;i++)
- v[i]+=u.v[i];
- }
- void copyv(vector &u)
- {
- for(int i=0;i<n;i++)
- v[i]=u.v[i];
- }
- void reset(int n1)
- {
- n=n1;
- for(int i=0;i<n;i++)
- v[i]=0;
- }
- void input()
- {
- for(int i=0;i<n;i++)
- {
- double a;
- cin>>a;
- v[i]=a;
- }
- }
- void display()
- {
- for(int i=0;i<n;i++)
- cout<<(v[i])<<endl;
- }
- };
- int main()
- {
- cout<<"Input the dimension of vector:"<<endl;
- int nx;
- cin>>nx;
- vector x(nx),y(nx);
- cout<<"Input the component of x:"<<endl;
- x.input();
- cout<<"Input the component of y:"<<endl;
- y.input();
- cout<<"Input one real number:"<<endl;
- double re;
- cin>>re;
- cout<<"x*re"<<endl;
- (x.multiply(re)).display();
- double mult;
- mult=x.multiply(y);
- cout<<"x*y="<<mult<<endl;
- vector z1(nx);
- z1.copyv(x);
- cout<<"z1=x"<<endl;
- z1.display();
- cout<<"x+y="<<endl;
- x.add(y);
- x.display();
- cout<<"Input n1 is new dimension to reset x:"<<endl;
- cin>>nx;
- x.reset(nx);
- cout<<"x after reset:"<<endl;
- x.display();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement