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
|
class ComplexNumber
{
public:
ComplexNumber(); //Default constructer, intitialize values to 0
ComplexNumber(int realamt, int imaginaryamt);
friend ComplexNumber operator +(const ComplexNumber& firstamt, const ComplexNumber& secamt);
friend ComplexNumber operator -(const ComplexNumber& firstamt, const ComplexNumber& secamt);
friend ComplexNumber operator *(const ComplexNumber& firstamt, const ComplexNumber& secamt);
friend ostream& operator <<(ostream& outs, const ComplexNumber& thevalue);
friend istream& operator >>(istream& ins, ComplexNumber& avalue);
friend bool operator ==(const ComplexNumber& firstamt, const ComplexNumber& secamt);
public:
int real, imaginary;
};
#include <iostream>
#include <cstdlib>
#include <cctype>
using namespace std;
int main ( )
{
int r1, r2, i1, i2;
cout<<"Enter the real number in the first pair: "<<endl;
cin>>r1;
cout<<"Enter the imaginary coefficient in the first pair: "<<endl;
cin>>i1;
cout<<"Enter the real number in the second pair: "<<endl;
cin>>r2;
cout<<"Enter the imaginary coefficient in the second pair: "<<endl;
cin>>i2;
ComplexNumber firstpair(r1,i1), secondpair(r2,i2), total;
total= firstpair + secondpair;
cout<<total<<endl;
total= firstpair - secondpair;
cout<<"\n"<<total<<endl;
total= firstpair * secondpair;
cout<<"\n"<<total<<endl;
if(firstpair==secondpair)
cout<<"The two pairs are the same";
else
cout<<"The two pairs are different";
system ("PAUSE");
return 0;
}
////////////////////////FUNCTIONS/////////////////////////
ComplexNumber::ComplexNumber(int realamt, int imaginaryamt)
{
real=realamt;
imaginary=imaginaryamt;
}
ComplexNumber operator +(const ComplexNumber& firstamt, const ComplexNumber& secondamt)//adds the two pairs
{
int totalreal, totalimaginary;
ComplexNumber result;
totalreal=firstamt.real + secondamt.real;
totalimaginary=firstamt.imaginary + secondamt.imaginary;
result.real= totalreal;
result.imaginary= totalimaginary;
return result;
}
ComplexNumber operator -(const ComplexNumber& firstamt, const ComplexNumber& secondamt)//substracts pair 2 from pair 1
{
int totalreal, totalimaginary;
ComplexNumber result;
totalreal=firstamt.real - secondamt.real;
totalimaginary=firstamt.imaginary - secondamt.imaginary;
result.real= totalreal;
result.imaginary= totalimaginary;
return result;
}
ComplexNumber operator *(const ComplexNumber& firstamt, const ComplexNumber& secondamt)//multiplies pairs
{
int totalreal, totalimaginary;
ComplexNumber result;
totalreal= (firstamt.real*secondamt.real)-(firstamt.imaginary*secondamt.imaginary);
totalimaginary=(firstamt.real*secondamt.imaginary)+(firstamt.imaginary*secondamt.real);
result.real= totalreal;
result.imaginary= totalimaginary;
return result;
}
ostream& operator <<(ostream& outs, const ComplexNumber& thevalue)
{
outs<<"The combination of these two pairs is "<<thevalue.real<<thevalue.imaginary<<"i";
return outs;
}
istream& operator >>(istream& ins, ComplexNumber& avalue)
{
ins>>avalue.real>>avalue.imaginary;
return ins;
}
bool operator ==(const ComplexNumber& firstamt, const ComplexNumber& secamt)//compares the two pairs
{
return (firstamt == secamt);
}
|