Wednesday 16 August 2017

For math buffs only: Create a class Polar that represents the points on the plain as polar coordinates (radius and angle). Create an overloaded +operator for addition of two Polar quantities. “Adding” two points on the plain can be accomplished by adding their X coordinates and then adding their Y coordinates. This gives the X and Y coordinates of the “answer.” Thus you’ll need to convert two sets of polar coordinates to rectangular coordinates, add them, then convert the resulting rectangular representation back to polar.




                                                                               SOLUTION:      
#include "stdafx.h"
#include "iostream"
#include "math.h"
using namespace std;
/////////////////////////////////////////////////////////////////////////////////////////
class polar
{
            private:
                        float radius, angle;
            public:
                        polar( ) : radius(0.0), angle(0.0)          {/*Empty Body*/}
                        polar(float r, float a) : radius(r), angle(a)         {/*Empty Body*/}
                        void get_coordinates( )
                        {           cout << "Enter Radius & Angle of the Point: ";           cin >> radius >> angle;           }
                        void put_coordinates( ) const
                        {           cout << "(" << radius << "," << angle << "\xF8)";       }
                        polar operator + (polar) const;
};
//---------------------------------------------------------------------------------------polar polar::operator + (polar p2) const
{
            float x, y;
            polar temp;
            x = radius*cos(angle) + p2.radius*cos(p2.angle);
            y = radius*sin(angle) + p2.radius*sin(p2.angle);
            temp.radius = sqrt((x*x) + (y*y));       temp.angle = atan(y/x);
            return temp;
}
/////////////////////////////////////////////////////////////////////////////////////////
void main( )
{
            polar pol1(20,45), pol2, pol3;
            pol2.get_coordinates( );
            pol3 = pol1 + pol2;                  cout << endl;
            pol1.put_coordinates( );          cout << " + ";
            pol2.put_coordinates( );          cout << " = ";
            pol3.put_coordinates( );          cout << endl << endl;
            system("pause");
}
OUTPUT:

0 comments: