POLAR (r,a) <-------> CARTESIAN (x,y)
r=sqrt(x*x+y*y)
a=atan(x/y)
x=r*cos(a);
y=r*sin(a);
#include<iostream.h>
#include<conio.h>
#include<math.h>
class cartesian;
class polar
{
float radius,angle;
public :
polar()
{
radius=0.0;
angle=0.0;
}
polar(float r,float a)
{
radius=r;
angle=a;
}
polar(cartesian p);
float get_radius();
float get_angle();
friend ostream & operator << (ostream & dout, polar x)
{
dout<<"\nRADIUS : "<<x.radius;
dout<<"\nANGLE : "<<x.angle;
return dout;
}
};
class cartesian
{
float x,y;
public :
cartesian()
{
x=0.0;
y=0.0;
}
cartesian(float X,float Y)
{
x=X;
y=Y;
}
cartesian(polar p);
float get_x();
float get_y();
friend ostream & operator << (ostream & dout, cartesian c)
{
dout<<"\nX : "<<c.x;
dout<<"\nY : "<<c.y;
return dout;
}
};
float polar :: get_radius() { return radius; }
float polar :: get_angle() { return angle; }
float cartesian :: get_x() { return x; }
float cartesian :: get_y() { return y; }
polar :: polar(cartesian p)
{
float X=p.get_x();
float Y=p.get_y();
radius=sqrt(X*X+Y*Y);
angle=atan(X/Y);
}
cartesian :: cartesian(polar p)
{
float r=p.get_radius();
float a=p.get_angle();
x=r*cos(a);
y=r*sin(a);
}
void main()
{
clrscr();
polar ob1,ob2(2,1);
cartesian obj1,obj2(2,1);
cout<<"\nPOLAR\n"<<ob2;
obj1=ob1;
cout<<endl<<"\nPOLAR -> CARTESIAN\n"<<obj1;
cout<<"\n\n\nCARTESIAN\n"<<obj2;
ob1=obj1;
cout<<endl<<"\nCARTESIAN -> POLAR\n"<<ob1;
getch();
}
The above code is compiled.
But doesn't give right output . All wrong answers ! :mellow
POLAR
radius=2
angle=1
POLAR -> CARTESIAN
X : 1.080605
Y : 1.682942
CARTESIAN
X=2
Y=1
CARTESIAN -> POLAR
RADIUS : 2.236068
ANGLE : 1.107149

New Topic/Question
Reply



MultiQuote



|