debai26
HoTen: HoangTrungKien
Lop: Thb51
Bai: Bai26.cpp
*/
//Cai dat lop So phuc
#include<iostream>
#include<math.h>
using namespace std;
class complex
{
private:
int thuc;
int ao;
public:
//ham tao khong doi so khoi tao cac gia tri thuc va ao cho so phuc
complex()
{
thuc=0;
ao=0;
}
complex(int t,int a)
{
thuc=t;
ao=a;
}
friend istream& operator>>(istream& is,complex &p);
friend ostream& operator<<(ostream& os,const complex &p);
complex operator+(const complex &p);
};
int main()
{
complex c[20];
int n,i;
cout<<"
Nhap so luong cac so phuc: ";cin>>n;
cout<<"
Nhap thong tin cho tung so phuc: ";
for(i=1;i<=n;i++)
{
cout<<"
So phuc thu "<<i<<": ";
cin>>c[i];
}
complex tong=c[1];
for(i=2;i<=n;i++) tong=tong+c[i];
cout<<"
Tong cua 2 so phuc: "<<tong<<endl;
return 0;
}
//==========dinh nghia cac ham thanh vien cua lop complex==================
istream& operator>>(istream& is,complex &p)
{
is>>p.thuc>>p.ao;
return is;
}
ostream& operator<<(ostream& os,const complex &p)
{
os<<p.thuc<<" "<<p.ao<<"i";
return os;
}
complex complex::operator+(const complex &p)
{
complex sp;
sp.thuc=thuc+p.thuc;
sp.ao=ao+p.ao;
return sp;
}
Bạn đang đọc truyện trên: Truyen247.Pro