1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
|
ComplexNumber.h
#ifndef COMPLEXNUMBER_H
#define COMPLEXNUMBER_H
using namespace std;
class ComplexNumber
{
public:
ComplexNumber();
ComplexNumber(double realPart, double imPart);
ComplexNumber(double realPart);
const ComplexNumber operator +(const ComplexNumber& amount);
const ComplexNumber operator -(const ComplexNumber& amount);
const ComplexNumber operator *(const ComplexNumber& amount);
const ComplexNumber operator /(const ComplexNumber& amount);
bool operator ==( const ComplexNumber& amount);
private:
double real;
double im;
};
#endif
ComplexNumber.cpp
#include "stdafx.h"
#include <iostream>
#include "ComplexNumber.h"
using namespace std;
ComplexNumber::ComplexNumber()
:real(0.0),
im(0.0)
{}
ComplexNumber::ComplexNumber(double realPart, double imPart)
:real(realPart),
im(imPart)
{}
ComplexNumber::ComplexNumber(double realPart)
: real(realPart),
im(0.0)
{}
const ComplexNumber operator +(const ComplexNumber& amount)
{
ComplexNumber temp;
temp.real=real+amount.real;
temp.im =im + amount.im;
cout << temp.real << "+" << temp.im << "i" << endl;
return temp;
}
const ComplexNumber operator -(const ComplexNumber& amount)
{
ComplexNumber temp;
temp.real=real-amount.real;
temp.im=im-amount.im;
if(temp.im<0)
cout<<temp.real<<temp.im<<"i"<<endl;
else
cout<<temp.real<<"+"<<temp.im<<"i"<<endl;
return temp;
}
const ComplexNumber operator *(const ComplexNumber& amount)
{
ComplexNumber temp;
temp.real=real*amount.real-im*amount.im;
temp.im=im*amount.real+amount.im*real;
if(temp.im<0)
cout<<temp.real<<temp.im<<"i"<<endl;
else
cout<<temp.real<<"+"<<temp.im<<"i"<<endl;
return temp;
}
const ComplexNumber operator /(const ComplexNumber& amount)
{
ComplexNumber temp;
temp.real=(real*amount.real+im*amount.im)/(amount.real*amount.real+amount.im*amount.im);
temp.im=(im*amount.real-real*amount.im)/(amount.real*amount.real+amount.im*amount.im);
if(temp.im<0)
cout<<temp.real<<temp.im<<"i"<<endl;
else
cout<<temp.real<<"+"<<temp.im<<"i"<<endl;
return temp;
}
bool operator ==( const ComplexNumber& amount)
{
if((real==amount.real) && (im==amount.im))
return true;
else
return false;
}
main.cpp //test driver
#include "stdafx.h"
#include <iostream>
#include "ComplexNumber.h"
using namespace std;
int _tmain()
{
double number1, number2, number3, number4;
ComplexNumber C1, C2;
ComplexNumber addition, substraction, product, quotient;
cout<<"Please enter the first complex number(real part followed by imaginary part)"<<endl;
cin>>number1>>number2;
cout<<"Please enter the second complex number(real part followed by imaginary part)"<<endl;
cin>>number3>>number4;
C1=ComplexNumber(number1, number2);
C2=ComplexNumber(number3, number4);
cout<<"The sum of the two complex numbers is:\n";
addition=C1+C2;
cout<<"The substraction of the two complex numbers is:\n\n";
substraction=C1-C2;
cout<<"The product of the two complex numbers is:\n\n";
product=C1*C2;
cout<<"The quotient of the two complex numbers is:\n\n";
quotient=C1/C2;
cout<<"Testing if the two complex numbers are equal:\n\n";
if(C1==C2)
cout<<"The two complex number are equal."<<endl;
else
cout<<"The two complex number are not equal."<<endl;
return 0;
}
|