1 /*------------------------------------- 2 rect_pol.c -- 把直角坐标转换为极坐标 3 -------------------------------------*/ 4 5 #include <stdio.h> 6 #include <math.h> 7 8 #define RAD_TO_DEG (180 / (4 * atan(1.0))) 9 10 typedef struct polar_v 11 { 12 double magnitude; 13 double angle; 14 } Polar_V; 15 16 typedef struct rect_v 17 { 18 double x; 19 double y; 20 } Rect_V; 21 22 Polar_V rect_to_polar(Rect_V); 23 24 int main() 25 { 26 Rect_V input; 27 Polar_V result; 28 29 puts("Enter x and y coordinates; enter q to quit:"); 30 while (scanf("%lf %lf", &input.x, &input.y) == 2) 31 { 32 result = rect_to_polar(input); 33 printf("magnitude = %0.2f, angle = %0.2f ", result.magnitude, result.angle); 34 } 35 36 return 0; 37 } 38 39 Polar_V rect_to_polar(Rect_V rv) 40 { 41 Polar_V pv; 42 43 pv.magnitude = sqrt(pow(rv.x, 2) + pow(rv.y, 2)); 44 if (pv.magnitude == 0) 45 pv.angle = 0; 46 else 47 pv.angle = RAD_TO_DEG * atan2(rv.y, rv.x); 48 49 return pv; 50 }