Eighth Week item-1.3

Source: Internet
Author: User

#include <iostream>
using namespace Std;
Class Complex
{
Public
Complex () {real = 0; imag = 0;}
Complex (Double R, double i) {real = r; imag = i;}
Complex operator+ (const Complex &AMP;C2);
Complex operator-(const Complex &AMP;C2);
Complex operator* (const Complex &AMP;C2);
Complex operator/(const Complex &AMP;C2);
FriendComplex operator+ (const double &c1,const Complex &AMP;C2);
Friend Complex operator-(const double &c1,const Complex &AMP;C2);
Friend Complex operator* (const double &c1,const Complex &AMP;C2);
Friend Complex operator/(const double &c1,const Complex &AMP;C2);
FriendComplex operator+ (const Complex &c1,const double &AMP;C2);
Friend Complex operator-(const Complex &c1,const double &AMP;C2);
Friend Complex operator* (const Complex &c1,const double &AMP;C2);
Friend Complex operator/(const Complex &c1,const double &AMP;C2);
void display ();
Private
Double Real;
Double imag;
};
The following defines the member functions
Complex complex::operator+ (const Complex &AMP;C2)
{
Return Complex (real + c2.real, imag + c2.imag);
}
Complex complex::operator-(const Complex &AMP;C2)
{
Return Complex (Real-c2.real, Imag-c2.imag);
}
Complex complex::operator* (const Complex &AMP;C2)
{
Return Complex (Real*c2.real-imag*c2.imag, Real*c2.imag + imag*c2.real);
}
Complex complex::operator/(const Complex &AMP;C2)//(AC+BD)/(c^2+d^2) +((bc-ad)/(c^2+d^2))i
{
Return Complex ((real*c2.real + imag*c2.imag)/(C2.real*c2.real + C2.imag*c2.imag), (IMAG*C2.REAL-REAL*C2.IMAG)/(c2.re Al*c2.real + C2.imag*c2.imag));
}
Complex operator+ (const double &AMP;C1, const Complex &AMP;C2)
{
Return Complex (c1 + c2.real, c2.imag);
}
Complex operator-(const double &AMP;C1, const Complex &AMP;C2)
{
Return Complex (C1-c2.real, C2.imag);
}
Complex operator* (const double &AMP;C1, const Complex &AMP;C2)
{
Return Complex (C1 * c2.real, C2.IMAG);
}
Complex operator/(const double &AMP;C1, const Complex &AMP;C2)
{
Return Complex (C1/c2.real, C2.imag);
}
Complex operator+ (const Complex &AMP;C1, const double &AMP;C2)
{
Return Complex (c1.real + c2, c1.imag);
}
Complex operator-(const Complex &AMP;C1, const double &AMP;C2)
{
Return Complex (C1.REAL-C2, C1.imag);
}
Complex operator* (const Complex &AMP;C1, const double &AMP;C2)
{
Return Complex (c1.real * c2, C1.imag);
}
Complex operator/(const Complex &AMP;C1, const double &AMP;C2)
{
Return Complex (C1.REAL/C2, C1.imag);
}
void Complex::d isplay ()
{
if (Imag = = 0)
cout << Real << Endl;
else if (Imag < 0)
cout << Real << "-" <<-imag << "i" << Endl;
Else
cout << Real << "+" << imag << "i" << Endl;
}
The following defines the main () function for testing
int main ()
{
Complex C1 (3, 4), C2 (5, -10), C3;
cout << "c1=";
C1.display ();
cout << "c2=";
C2.display ();
C3 = C1 + C2;
cout << "c1+c2=";
C3.display ();
C3 = C1-c2;
cout << "c1-c2=";
C3.display ();
C3 = C1*c2;
cout << "c1*c2=";
C3.display ();
C3 = C1/c2;
cout << "c1/c2=";
C3.display ();
C3 = C1 + 3;
cout << "C1 + 3 =";
C3.display ();
C3 = 5 + c2;
cout << "-5 + c2=";
C3.display ();//-----------------------+
C3 = C1-3;
cout << "c1-3=";
C3.display ();
C3 = -5-c2;
cout << " -5-c2=";
C3.display ();//-----------------------
C3 = C1 * 3;
cout << "C1 * 3 =";
C3.display ();
C3 =-5 * c2;
cout << "-5 * c2=";
C3.display ();//----------------------*
C3 = C1/3;
cout << "c1/3=";
C3.display ();
C3 = -5/c2;
cout << " -5/c2=";
C3.display ();//---------------------/
while (1) {}
return 0;
}

Eighth Week item-1.3

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.