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
|
#include <iostream>
#include <cmath>
using namespace std;
class Pol_coord;
class Rec_coord
{
friend void conv_pol(int, double, double, Pol_coord&, Rec_coord&);
public:
double xval;
double yval;
Rec_coord(double = 1.0, double = 1.0);
void display_coords();
void input_rect_coords();
};
Rec_coord::Rec_coord(double XVAL,double YVAL)
{
xval = XVAL;
yval = YVAL;
}
void Rec_coord::display_coords()
{
cout << "The rectangular coordinates are " << endl;
cout << "x = " << xval << endl;
cout << "y = " << yval << endl;
}
void Rec_coord::input_rect_coords()
{
cout << "Enter your rectangular coords" << endl;
cin >> xval;
cin >> yval;
}
class Pol_coord : public Rec_coord
{
friend void conv_pol(int, double, double, Pol_coord&, Rec_coord&);
public:
double dist, theta;
Pol_coord(double = 0, double = 0);
void display_polar_coord();
void input_polar_coords();
};
Pol_coord::Pol_coord(double DIST, double THETA)
{
dist = DIST;
theta = THETA;
}
void Pol_coord::display_polar_coord()
{
cout << "The polar coordinates are" << endl;
cout << "distance = " << dist << endl;
cout << "theta = " << theta << endl;
}
void Pol_coord::input_polar_coords()
{
cout << "Enter your polar coord" << endl;
cout << "Enter your value for the radius " << endl;
cin >> dist;
cout << "Enter your value for theta" << endl;
cin >> theta;
cout << "Your chosen values for r and theta are" << endl;
cout << "r = " << dist << endl;
cout << "theta = " << theta << endl;
}
void conv_pol(int dir, double val1, double val2, Pol_coord &polref, Rec_coord &recref)
{
cout << "Select 1 if you want to convert from rec to polar or 2 if you want to convert from polar to rect" << endl;
cin >> dir;
switch(dir){
case 1:
{
cout << "Converting from rectangular to polar coords" << endl;
val1 = recref.xval;
val2 = recref.yval;
double r = 0.0;
double theta = 0;
r = polref.dist = sqrt((pow(val1,2) + pow(val2,2)));
theta = polref.theta = atan(
val2 / val1);
cout << "r = " << r << endl;
cout << "theta = " << theta << endl;
}
case 2:
{
cout << "Converting from polar to rectangular coords" << endl;
val1 = polref.dist;
val2 = polref.theta;
double x = recref.xval = val1 * cos(val2);
double y = recref.yval = val1 * sin(val2);
cout << "x = " << x << endl;
cout << "y = " << y << endl;
}
}
}
int main()
{
Rec_coord A;
Pol_coord B;
A.display_coords();
B.display_polar_coord();
A.input_rect_coords();
B.input_polar_coords();
conv_pol(2, A.xval, A.yval,B, A);
return 0;
}
|