有人可以告诉我,如果我的代码有任何问题吗?

时间:2015-05-18 15:51:26

标签: c calculator calculus

我有这个项目在学校,我必须资助一个公式,并制定一个程序来计算它。我为子弹弹道选择了控制形式,即X =(v²/ g)* Sin(2θ),其中X =最大距离,v =初速度,g =重力(32为常数)和Sin(2 Theta) =射击仰角两倍的sin值(如果枪以30度的角度射击,则使用60度的正弦值)。我在计算中得到了奇怪的值。

是否有人可以识别代码有什么问题?

代码:

//Calculator for bullet ballistics
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <math.h>
#define g 32
#define PI 3.1416

void main(void)
{
float X, v, vg, v2, elevation, elevationRad, e, Sin2Theta, Theta2, Theta, ThetaDeg, choice, restart, restart2;
restart = 0;
while (restart <= 1)
{
    printf("Please state what you are trying to find.\n");
    printf("1. Maximum distance\n");
    printf("2. Muzzle velocity\n");
    printf("3. Elevation angle\n");
    scanf("%f", &choice);


    if (choice == 1)   //calculating maximum distance
    {
        printf("You are now calculating Maximum distance.\n");
        printf("Please enter the muzzle velocity (m/s): ");
        scanf("%f", &v);
        printf("Please enter the elevation angle (degree)");
        scanf("%f", &elevation);

        elevationRad = (180 / PI) * elevation;
        Sin2Theta = sin(2 * elevationRad);

        X = (pow(v, 2) / g) * Sin2Theta;

        printf("The maximum distance is %f feet\n", X);
        printf("Would you like to restart? 1. Yes 2. No ");
        scanf("%f", &restart2);
        restart = restart + restart2;
    }
    else if (choice == 2)    //calculating muzzle velocity
    {
        printf("You are now calculating Muzzle velocity.\n");
        printf("Please enter the maximum distance (feet): ");
        scanf("%f", &X);
        printf("Please enter the elevation angle (degree):");
        scanf("%f", &elevation);

        elevationRad = (180 / PI) * elevation;
        Sin2Theta = sin(2 * elevationRad);

        vg = X / Sin2Theta;
        v2 = vg / g;
        v = sqrt(v2);

        printf("The muzzle velocity is %f m/s \n", v);
        printf("Would you like to restart? 1. Yes 2. No ");
        scanf("%f", &restart2);
        restart = restart + restart2;
    }
    else //calculating elevation angle
    {
        printf("You are now calculating Elevation angle.\n");
        printf("Please enter the maximum distance (feet): ");
        scanf("%f", &X);
        printf("Please enter the muzzle velocity (m/s):");
        scanf("%f", &v);

        Sin2Theta = X / (pow(v, 2) / g);
        elevationRad = asin(Sin2Theta);
        elevation = (elevationRad / 2);
        e = (180 / PI)* elevation;

        printf("The elevation angle is %f degrees \n", e);
        printf("Would you like to restart? 1. Yes 2. No ");
        scanf("%f", &restart2);
        restart = restart + restart2;
    }
}

}

1 个答案:

答案 0 :(得分:4)

我认为不是

   elevationRad = (180 / PI) * elevation;

应该是

   elevationRad = (PI / 180) * elevation;

因为,angle(in radians) = PI*angle(in degree)/180