Nov 11, 2010 at 2:24pm UTC
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
#include <cmath>
#include <iostream>
#include <iomanip>
using namespace std;
class complex
{
private :
int real; // Real Part
int imag; // Imaginary Part
public :
complex(int ,int );
complex(const complex&);
complex operator /(complex);
complex getconjugate();
complex getreciprocal();
void setdata(int ,int );
void getdata();
int getreal();
int getimaginary();
void operator =(complex);
friend ostream& operator <<(ostream &s,complex &c);
};
//CONSTRUCTOR
complex::complex(int r=0,int im=0)
{
real=r;
imag=im;
}
//COPY CONSTRUCTOR
complex::complex(const complex &c)
{
this ->real=c.real;
this ->imag=c.imag;
}
void complex::operator =(complex c)
{
real=c.real;
imag=c.imag;
}
complex complex::operator /(complex c)
{
int div=(c.real*c.real) + (c.imag*c.imag);
complex tmp;
tmp.real=(real*c.real)+(imag*c.imag);
tmp.real/=div;
tmp.imag=(imag*c.real)-(real*c.imag);
tmp.imag/=div;
return tmp;
}
complex complex::getconjugate()
{
complex tmp;
tmp.real=this ->real;
tmp.imag=this ->imag * -1;
return tmp;
}
complex complex::getreciprocal()
{
complex t;
t.real=real;
t.imag=imag * -1;
int div;
div=(real*real)+(imag*imag);
t.real/=div;
return t;
}
void complex::setdata(int r,int i)
{
real=r;
imag=i;
}
void complex::getdata()
{
cout<<"Enter Real:" ;
cin>>this ->real;
cout<<"Enter Imaginary:" ;
cin>>this ->imag;
}
int complex::getreal()
{
return real;
}
int complex::getimaginary()
{
return imag;
}
ostream& operator <<(ostream &s,complex &c)
{
s << "Real Part = " << c.real <<endl << "Imaginary Part = " << c.imag << endl;
s << "Result = " << c.real << setiosflags(ios::showpos) << c.imag << "i" << endl << resetiosflags(ios::showpos);
return s;
}
int main()
{
complex b;
complex c;
b.getdata(); // Calls Getdata()
c.getdata();
complex d;
d=b/c; // calls overloaded /
cout<<d<<endl;
return 0;
}
G:\workspace>Text1.exe
Enter Real:4
Enter Imaginary:5
Enter Real:3
Enter Imaginary:2
Real Part = 1
Imaginary Part = 0
Result = 10i
Based on my notes available, a complex numbers division with the above input would result in (22 + 7i) / 13. Or am I figuring some stuff wrong.
Last edited on Nov 11, 2010 at 2:25pm UTC
Nov 11, 2010 at 3:23pm UTC
I don't know if there's anything wrong with your code, but this is most likely because you're using integers for everything. (1/2=0). use floats or doubles.
Nov 12, 2010 at 2:03pm UTC
nothing wrong with that. (22+7i)/13 = 22/13 + 7/13*i = 1.69 + 0.54i.
If you want to get the output "(22+7i)/13" you'll have a lot of trouble.
It would be easier to get "22/13 + 7/13*i" if you want though (you'd have to write a 'fraction' class, overload +,-,*,/ operators and replace 'float' with 'fraction'.)