【问题标题】:Can someone please tell me if my code has anything wrong with it?有人可以告诉我我的代码是否有问题吗?
【发布时间】:2015-07-30 05:20:27
【问题描述】:

我有一个学校项目,我必须资助一个公式并编写一个程序来计算它。我选择了子弹弹道的控制形式,即 X = (v² / g) * Sin(2 theta) ,其中 X = 最大距离,v = 初速,g = 重力(32 为常数)和 Sin(2 Theta) = 两倍射击仰角的正弦值(如果枪以 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;
    }
}

}

【问题讨论】:

  • 如果代码按预期工作,但有改进的潜力,它属于代码审查。
  • 我投票结束这个问题,因为它要求进行代码审查。
  • @MartinJames,为什么不支持将其迁移到 Code Review?

标签: c calculator calculus


【解决方案1】:

我认为不是

   elevationRad = (180 / PI) * elevation;

应该是

   elevationRad = (PI / 180) * elevation;

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-21
    • 2020-10-30
    • 2010-11-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多