要求:利用函数将几何坐标系下的x,y转化为极坐标下的ρ和θ
思路:通常函数只能通过return语句返回一个返回值,需要同时返回两个值,可以通过参数中传引用的方式,将地址传给函数,从而直接在函数中修改该地址的值。
代码:
#include <stdio.h>
#include <math.h>
void convert(double, double, double*, double*);
int main()
{
double x, y, r, t, *ro, *theta;
ro = &r; theta = &t; // 初始化ro和theta指针,否则后面引用时可能出错
printf(“请输入x, y:”);
scanf("%lf %lf", &x, &y);
convert(x, y, ro, theta); // x和y是传值调用
// ro和theta是指针,因此是传引用调用
printf("ro=%lf theta=%lf", *ro, *theta);
return 0;
}
// 将x,y 转换为 ro,theta
void convert(double x, double y, double *ro, double theta)
{
ro = sqrt(xx + yy);
*theta = atan(y/x); //这里应该要补充判断,x不为零,如果x为零,则另判断
return;
}
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
————————————————
版权声明:本文为CSDN博主「杨丰玉」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/frueyang/article/details/103267388