首页 文章

C编程:输出错误?

提问于
浏览
-4

Hy大家,所以我试图解决这个问题

编写一个C程序,计算射弹撞击地面前的距离(即射程),射弹撞击地面所需的时间,以及飞行中射弹的最大高度,考虑射击的角度在空中,以及它发射时的初始速度(速度) . 我们将假设地面是平坦的,并且唯一存在的力是重力(没有空气阻力等) .

Maximum height: h=pow(v*sin(θ),2)/2*g;
Time in air: t = (2*v*sin(θ))/g;
Range: r = (2*pow(v,2)*sin(θ)*cos(θ))/g;



θ = angle that the projectile is launched (in whole degrees)
v = initial velocity of the projectile (in meters/second)
g = acceleration due to gravity = 9.8 meters/second2

现在我不知道为什么我会得到负面结果 . 我想我使用了错误的声明语法,或者我为角度和初始速度提供了错误的输入,或者是因为划分是四舍五入的...有人可以帮我弄清楚解决这个问题的正确方法是什么? (我是C编程的新手,我想学习)

2 回答

  • 0

    正确的公式如下

    Maximum height: h=pow(v*sin(θ*M_PI/180),2)/(2*g);
    Time in air: t = (2*v*sin(θ*M_PI/180))/g;
    Range: r = (2*pow(v,2)*sin(θ*M_PI/180)*cos(θ*M_PI/180))/g;
    

    你甚至可以将Range公式减少到 pow(v,2)*sin(2*θ*M_PI/180)/g

    作为 sin(2θ) = 2*sin(θ)*cos(θ)

    来自@Mat,@ BLUEPIXY和@Sanhadrin的评论

  • 0

    谢谢你的回答,我不知道sin&cos使用弧度 . 我不得不重写整个事情,这就是它现在的样子:

    #include<stdio.h>
    #include<math.h>
    
    int main(void){
        int theta;
        float h, g=9.8, t, r, v;
    
        printf("Type a value for the angle that the projectile is launched (in whole degrees): ");
        scanf("%d",&theta);
        printf("Type a value for the initial velocity of the projectile (in m/s): ");
        scanf("%f",&v);
    
    
        theta = theta*M_PI/180;
        h = pow(v*sin(theta),2)/(2*g);
        t = (2*v*sin(theta))/g;
        r = (2*pow(v,2)*sin(theta)*cos(theta))/g;
    
        printf("The maximum height is %f m, the time it takes for the projectile to hit the ground is %f s and it's range is %f m\n", h, t, r);
    
        return 0;
    }
    

    当我运行它:

    Type a value for the angle that the projectile is launched (in whole degrees): 110
    Type a value for the initial velocity of the projectile (in m/s): 6.2
    The maximum height is 1.388691 m, the time it takes for the projectile to hit the ground is 1.064718 s and it's range is 3.566673 m
    

相关问题