获得弹丸运动时间和高度的错误值

时间:2013-06-17 21:28:48

标签: c motion projectile

我正在用C语言编写一个程序,它可以找到飞行时的飞行时间和高度,并且我的程序正在编译,但它的解决方案的错误值却出来了。知道我可能做错了吗?

/*
*Finds final time and height of a projectile when it reaches the target
*/
#include <stdio.h> /* printf, scanf definitions */
#include <math.h> /* cos definition */
#define G 32.17 /* gravitational constant */

int
main(void)
{
    double theta; /* input - angle(radians)of elevation */
    double distance; /* input - distance(ft)to target */
    double velocity; /* input - projectile velocity(ft/sec) */

    double time; /* output - time(sec)of flight */
    double height; /* output - height at impact */

    /* Opening explanation to user */
    printf("This program computes the duration of a projectile's flight and its height above the ground when it reaches the target. To use this program, enter the angle, the distance, and the velocity after each of the prompts.\n\n");

    /* Get angle, distance, velocity. */
    printf("Enter the angle of elevation in radians:");
    scanf("%f", &theta );
    printf("Enter the distance to target in feet:");
    scanf("%f", &distance );
    printf("Enter the projectile velocity in ft/sec:");
    scanf("%f", &velocity );

    /* Get time of flight */
    time = distance / (velocity * cos(theta));

    /*Get height at impact */
    height = velocity * sin(theta) * time - ( (G * time*time) / 2.0 );

    /* Display time of flight */
    printf("The time of flight is %fs seconds.\n", time);

    /* Display height at impact */
    printf("The height at impact is %fm feet.\n", height);

    system("pause");

    return (0);
}

1 个答案:

答案 0 :(得分:1)

double theta;
/* ... */
scanf("%f", &theta );

你必须使用

scanf("%lf", &theta );

阅读doublef转化说明符用于阅读float