bai3-week4

// vecto

#include<iostream.h>

#include<conio.h>

class VT

{

int n; float a[50];

public:

void nhap();

void in();

VT operator+(VT);

int operator==(VT);

float operator*(VT);

};

void VT::nhap()

{

cout<<"nhap so phan tu: ";

do

{ cin>>n; if(n<=1) cout<<"nhap n lon hon 1: "; }

while(n<=1);

for(int i=0;i<n;i++)

{cout<<"nhap phan tu thu "<<(i+1)<<": ";cin>>a[i];}

}

void VT::in()

{ cout<<"(";

for(int i=0;i<n-1;i++)

{ cout<<a[i]<<","; }

cout<<a[n-1]<<")";

}

int VT::operator==(VT b)

{

if(n==b.n) return(0);

else return(1);

}

VT VT::operator+(VT b)

{ VT kq;

kq.n=n;

for(int i=0;i<n;i++)

{ kq.a[i]=a[i]+b.a[i]; }

return(kq);

}

float VT::operator*(VT b)

{ float kq=0;

for(int i=0;i<n;i++)

{ kq=kq+a[i]*b.a[i]; }

return(kq);

}

main()

{VT c,b,d;

c.nhap();cout<<'

';b.nhap();

if(c==b) cout<<"

2 vecto ko thuoc cung he toa do,ko thuc hien duoc yeu cau bai toan";

else

{d=c+b;

cout<<"tong 2 vecto la vecto:";

d.in();

cout<<"

tich vo huong 2 vecto la: "<<(c*b);

}

getch();

}

Bạn đang đọc truyện trên: AzTruyen.Top

Tags: #business