rect_pol.c 916 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /* rect_pol.c -- converts rectangular coordinates to polar */
  2. #include <stdio.h>
  3. #include <math.h>
  4. #define RAD_TO_DEG (180/(4 * atan(1)))
  5. typedef struct polar_v {
  6. double magnitude;
  7. double angle;
  8. } Polar_V;
  9. typedef struct rect_v {
  10. double x;
  11. double y;
  12. } Rect_V;
  13. Polar_V rect_to_polar(Rect_V);
  14. int main(void)
  15. {
  16. Rect_V input;
  17. Polar_V result;
  18. puts("Enter x and y coordinates; enter q to quit:");
  19. while (scanf("%lf %lf", &input.x, &input.y) == 2)
  20. {
  21. result = rect_to_polar(input);
  22. printf("magnitude = %0.2f, angle = %0.2f\n",
  23. result.magnitude, result.angle);
  24. }
  25. puts("Bye.");
  26. return 0;
  27. }
  28. Polar_V rect_to_polar(Rect_V rv)
  29. {
  30. Polar_V pv;
  31. pv.magnitude = sqrt(rv.x * rv.x + rv.y * rv.y);
  32. if (pv.magnitude == 0)
  33. pv.angle = 0.0;
  34. else
  35. pv.angle = RAD_TO_DEG * atan2(rv.y, rv.x);
  36. return pv;
  37. }