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
|
#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;
void getdata(double& x, double& y);
void polar(double x, double y, double& r, double& theta);
void displayresult(double radius, double angle);
int main()
{
double x, y, distance, angle;
char repeat;
do
{
system("cls");
cout << "Rectangular to Polar Coordinate Conversion" << endl;
cout << "------------------------------------------" << endl << endl;
getdata(x,y);
if (x == 0)
{
;
}
else
{
polar(x, y, distance, angle);
cout << endl << endl;
displayresult(distance, angle);
}
cout << endl;
cout << "Run the program again? [y/n]: ";
cin >> repeat;
cout << endl << endl;
} while (repeat == 'y' || repeat == 'Y');
return 0;
}
void getdata(double& x, double& y)
{
cout << "Enter the x coordinate: ";
cin >> x;
cout << endl;
cout << "Enter the y coordinate: ";
cin >> y;
cout << endl << endl;
if (x == 0)
cout << "Error: value of x cannot be 0.";
return;
}
void polar(double x, double y, double& r, double& theta)
{
int a = x;
int b = y;
int c = r;
if (y == 0)
{
r = x;
}
else
{
c = sqrt((a^2) + (b^2));
r = c;
}
theta = ((tan(-1)) * (a/b));
return;
}
void displayresult(double radius, double angle)
{
radius = distance;
angle = angle;
cout << "Polar coordinates: " << endl << endl;
cout << "Distance from origin = " << radius << endl;
cout << "Angle (in degrees) from x-axis = " << angle << endl;
}
|