Trước khi viết các hàm thao tác trên các cấu trúc Số phức, ta cần khai báo cấu trúc như sau:
struct Complex
{
float Re; //phần thực - real
float Im; //phần ảo - imaginary
};
Các hàm thao tác với Complex:
Hiển thị số phức
void OutputComplex(Complex a)
{
if (a.Re != 0) cout << a.Re;
if (a.Im != 0)
{
if (a.Im == -1) cout << "-i";
else if (a.Im == 1)
{
if (a.Re == 0) cout<<"i";
else cout<<"+i";
}
else
{
if (a.Re != 0 && a.Im > 0)
cout<<"+"<<a.Im<<"i"<<endl;
else
cout<<a.Im<<"i"<<endl;
}
}
if (a.Re==0 & a.Im==0)
cout<<"0"<<endl;
}

