bai2-week5

// sp phu'c

#include<iostream.h>

#include<math.h>

#include<conio.h>

class SP

{

int pt,pa;

public:

friend istream& operator>>(istream& is,SP &a); // nhap

friend ostream& operator<<(ostream& os,SP a); // in

SP operator*(SP);

void chia(SP);

SP operator+(SP);

SP operator-(SP);

friend float tinh(SP);

};

istream& operator>>(istream& is,SP &a)

{

cout<<"

nhap phan thuc: ";is>>a.pt;

cout<<"nhap phan ao: ";is>>a.pa;

return(is);

}

ostream& operator<<(ostream& os,SP a)

{

os<<a.pt<<"+"<<a.pa<<"i

";

return(os);

}

SP SP::operator*(SP a)

{ SP kq;

kq.pt=pt*a.pt;

kq.pa=pa*a.pa;

return(kq);

}

SP SP::operator+(SP a)

{ SP kq;

kq.pt=pt+a.pt;

kq.pa=pa+a.pa;

return(kq);

}

SP SP::operator-(SP a)

{ SP kq;

kq.pt=pt-a.pt;

kq.pa=pa-a.pa;

return(kq);

}

void SP::chia(SP a)

{

cout<<(float(pt)/float(a.pt))<<"+"<<(float(pa)/float(a.pa))<<"i

";

}

float tinh(SP a)

{ return(sqrt(a.pt*a.pt+a.pa*a.pa)); }

void main()

{

SP a,b,k,h,i;

cout<<"

nhap 2 so phuc

";cin>>a>>b;

cout<<(k=a+b)<<(h=a-b)<<(i=a*b);

a.chia(b);

cout<<tinh(a)<<'

'<<tinh(b);// hoac co the sd qua ham ban

getch();

}

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

Tags: #business