bai3-week5
// vecto
#include<iostream.h>
#include<conio.h>
class VT
{
int n; float a[50];
public:
friend istream& operator>>(istream& is,VT &b);
friend ostream& operator<<(ostream& os,VT b);
VT operator+(VT);
int operator==(VT);
float operator*(VT);
};
istream& operator>>(istream& is,VT &b)
{
cout<<"
nhap so phan tu: ";
do
{ is>>b.n; if(b.n<=1) cout<<"nhap n lon hon 1: "; }
while(b.n<=1);
for(int i=0;i<b.n;i++)
{cout<<"nhap phan tu thu "<<(i+1)<<": ";is>>b.a[i];}
return(is);
}
ostream& operator<<(ostream& os,VT b)
{ os<<"(";
for(int i=0;i<b.n-1;i++)
{ os<<b.a[i]<<","; }
os<<b.a[b.n-1]<<")";
return(os);
}
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;
cout<<" nhap 2 vecto
";cin>>c>>b;
if(c==b) cout<<"
2 vecto ko thuoc cung he toa do,ko thuc hien duoc yeu cau bai toan";
else
{
cout<<"
tong 2 vecto la vecto: "<<(d=c+b);
cout<<"
tich vo huong 2 vecto la: "<<(c*b);
}
getch();
}
Bạn đang đọc truyện trên: TruyenTop.Vip