Write a function polarToCartesian which takes four parameters: r, theta, x, and y. x and y should be passed by pointers. The function should populate the values pointed to by x and y with the r cos(θ) and r sin(θ). Write a test for this function. In the C language, one does not have references, so passing by pointer is the C equivalent of passing by reference.
void polarToCartesian(double r, double theta,
double* x, double* y) {
*x = r*cos(theta);
*y = r*sin(theta);
}
void testPolarToCartesian() {
double r = 2;
double theta = atan(1);
double x;
double y;
polarToCartesian(r, theta, &x, &y);
ASSERT_APPROX_EQUAL(x, sqrt(2), 0.0001);
ASSERT_APPROX_EQUAL(y, sqrt(2), 0.0001);
}
系统当前共有 481 篇文章