该程序模拟以固定垂直和水平速度以角度发射的物体的抛物线轨迹。它以终端控制台中显示的坐标输出数据。模拟抛物线抛物线轨迹的C代码提前终止
但是,程序只输出数据到第二行并终止,因此代码中的某处必定存在错误。我无法识别错误,所以我正在请求帮助!
#include <stdio.h>
#include <stdlib.h>
int main(void) {
float lvelox;
float lveloy;
float xcord;
float ycord;
int stepcount;
int step = 0;
/* Initializing velocity */
{
printf("Enter the initial h velocity of the ball:\n");
scanf("%f", &lvelox);
printf("Enter the initial v velocity of the ball:\n");
scanf("%f", &lveloy);
}
/* Obtain number of steps */
{
printf("Enter the number of steps wanted:\n");
scanf("%d", &stepcount);
}
/* formula for calculating initial position */
if (step == 0)
{
xcord = 0;
ycord = 0;
step = step + 1;
printf("\n");
printf("xcord, ycord, step\n");
printf("\n");
printf("%f, ", xcord);
printf("%f, ", ycord);
printf("%d\n", step);
}
/* Loop method */
if (step < stepcount)
{
lveloy = lveloy - 9.81;
xcord = xcord + lvelox;
ycord = ycord + lveloy;
step = step + 1;
printf("%f, ", xcord);
printf("%f, ", ycord);
printf("%d\n", step);
if (ycord < 0)
{
lveloy = (lveloy * -1);
lveloy = lveloy - 9.81;
xcord = xcord + lvelox;
ycord = ycord + lveloy;
step = step + 1;
printf("%f, ", xcord);
printf("%f, ", ycord);
printf("%d\n", step);
}
}
if (step >= stepcount)
{
return 0;
}
}
附注:你的'printf'过于冗长。例如,你可以这样写:'printf(“%f,%f,%d \ n”,xcord,ycord,step);'。 –
另外,'if(step == 0)'是多余的,因为'step'在那一点上保证为零。 –
另一方面 - 注意:[**缩进C程序**](http://www.cs.arizona.edu/~mccann/indent_c.html) –