[英]Can someone please tell me if my code has anything wrong with it?
I have this project for school where I have to fund a formula and make a program to calculate it. 我在学校有这个项目,我必须资助一个公式并制定一个程序对其进行计算。 I chose the control formal for bullet ballistics, which is X = (v² / g) * Sin(2 theta) , where X = maximum distance, v = muzzle velocity ,g = gravity (32 as constant) and Sin(2 Theta) = the sin value of twice the firing elevation angle ( if the gun is fired at an angle of 30 degrees, use the sine value of 60 degrees). 我为子弹弹道选择了控制形式,即X =(v²/ g)* Sin(2 theta),其中X =最大距离,v =炮口速度,g =重力(32为常数)和Sin(2 Theta) =两倍于射击仰角的正弦值(如果以30度角射击,则使用60度正弦值)。 I am getting weird values in the calculations. 我在计算中得到了奇怪的值。
is it possible if someone can identify anything wrong with the code? 有人可以识别出代码中的任何错误吗?
Code: 码:
//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;
}
}
}
I think instead of 我认为不是
elevationRad = (180 / PI) * elevation;
it should be 它应该是
elevationRad = (PI / 180) * elevation;
since, angle(in radians) = PI*angle(in degree)/180
因为, angle(in radians) = PI*angle(in degree)/180
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.