我正在用 C 编写一个程序,其中程序的一部分应该将球面坐标转换为笛卡尔坐标,然后将它们显示在屏幕上,但我总是收到这部分的编译错误。我不确定我的语法是否错误,或者我是否遗漏了某些内容。有什么建议么?我认为问题在于我如何使用 printf,例如使用错误的参数或错误的参数数量。错误是:
Compilation error time: 0 memory: 0 signal:0prog.c: In function 'print_test':
prog.c:21:9: error: incompatible type for argument 1 of 'printf'
printf(cartesian.x);
^
这是根据错误消息我认为我做错的代码部分(我想我在代码中添加了一些东西,但大部分效果应该保持不变):
void print_test (struct threedCartesianData cartesian)
{
printf(cartesian.x, cartesian.y)
}
我很确定我使用 printf 是错误的,但不确定到底是怎么回事。
最佳答案
- 向
printf()
添加格式字符串 - 在
printf()
后添加分号 - 声明
cart
并删除ConvertSpclToCart()
中未使用的cartesian
- 将
main()
中的ConvertSphclToCart
更改为ConvertSpclToCart
- 将
int main()
更改为int main(void)
#include <stdio.h>
#include <math.h>
#define PI 3.14159
struct threedSphericalData
{
float azimuth;
float inclination;
float range;
};
struct threedCartesianData
{
float x;
float y;
float z;
};
void print_test (struct threedCartesianData cartesian)
{
printf("%f %f\n", cartesian.x, cartesian.y);
}
struct threedCartesianData ConvertSpclToCart(struct threedSphericalData spherical)
{
struct threedCartesianData cart;
cart.x = spherical.range * cos(spherical.inclination * PI/180)*cos(-(spherical.azimuth)*PI/180);
cart.y = spherical.range * cos(spherical.inclination * PI/180)*sin(-(spherical.azimuth)*PI/180);
return cart;
}
int main (void)
{
struct threedSphericalData spherical;
spherical.azimuth = 35;
spherical.inclination = 10;
spherical.range = 300;
struct threedCartesianData cartesian = ConvertSpclToCart(spherical);
print_test(cartesian);
return 0;
}
关于c - 在C中如何使用printf输出球坐标转换为笛卡尔坐标的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34316336/