gpt4 book ai didi

c 代码不适用于带有 float 的 Turbo C++

转载 作者:行者123 更新时间:2023-11-30 19:51:13 25 4
gpt4 key购买 nike

#include <stdio.h>
#include <math.h>

int main(void)
{
double r,d,x,y,pi;

clrscr();
printf("Input the value of r and degree: ");
//scanf("%f",&r);
//scanf("%f",&d);

r = 12;
d = 195;
pi = 3.14;
x = r * cos(d * pi/180);
y = r * sin(d * pi/180);

printf("The polar form is: (%f,%f)", x, y);
getch();
}

在第一种情况下,定义了 r 和 d 的值,输出是正确的,但在第二种情况下,当我给出输入时,输出与原始答案不匹配。该代码适用于代码块,但不适用于 Turbo C++。

我在 Turbo C++ 中做错了什么?

最佳答案

格式说明符不匹配。将 "%lf"double *

结合使用
double r,d,x,y,pi;
...
//scanf("%f",&r);
//scanf("%f",&d);
scanf("%lf",&r);
scanf("%lf",&d);
<小时/>

更好的代码在使用 r 之前检查是否成功。

if (scanf("%lf",&r) != 1) Handle_Error();
<小时/>

输出为笛卡尔坐标@Jens 。我还推荐'\n'

// printf("The polar form is: (%f,%f)", x, y);
printf("The Cartesian form is: (%f,%f)\n", x, y);
<小时/>

出现 Turbo-C requires "l" 当打印 double 时,所以使用

printf("The Cartesian form is: (%lf,%lf)\n", x, y);
<小时/>

没有什么理由使用低精度pi。建议

pi = acos(-1.0);

关于c 代码不适用于带有 float 的 Turbo C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48243253/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com