Lớp số phức – Complex Number class in C++

#include <iostream.h>
#include
<iomanip.h>
class ComplexN
{
float a;
float b;
public:
ComplexN(){};
ComplexN(float a1, float b1):a(a1),b(b1){};
friend ostream & operator << (ostream &,const ComplexN &);

friend istream & operator >> (istream &, ComplexN &);
ComplexN operator + (const ComplexN &);
ComplexN operator - (const ComplexN &);
ComplexN operator * (const ComplexN &);
};

istream & operator >> (istream & is, ComplexN & r)
{
cout << “Phan thuc:”; is >> r.a;
cout << “Phan ao:”; is >> r.b;
return is;
}

ostream & operator << (ostream & os,const ComplexN & r)
{
os << setprecision(2) << r.a;

if(r.b>0.0)
os << ” + ” << setprecision(2) << r.b << “*i”;
else
os << setprecision(2) << r.b << “*i”;

return os;

}

ComplexN ComplexN::operator+(const ComplexN & r)

{

float a1 = a + r.a; float b1 = b + r.b;

return ComplexN(a1,b1);

}

ComplexN ComplexN::operator-(const ComplexN & r)

{

float a1 = a – r.a; float b1 = b – r.b;

return ComplexN(a1,b1);

}

main()

{

ComplexN z1, z2;

cin >> z1;
cin >> z2;
cout << “z1 = ” << z1 << endl;
cout << “z2 = ” << z2 << endl;
cout << “z1+z2 = ” << z1+z2 << endl;;

return 0;
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: