我只是想确定我的公式是正确的,并且想看看是否有办法使高度一直为零,而这里的时间计数是我的代码,到目前为止,我已经使用了一些方法像高度这样的数字是10且速度是0,它将以0.0开始计时,以.25递增,高度将从10开始计时,但高度不会递减为零,并且在该时间停止于0.75我很抱歉,如果它混淆了我试图解释的内容,im混淆了我的自我
// Programmer: Your Name
// Date: Date
// Program Name: The name of the program
// Chapter: Chapter # - Chapter name
// Description: 2 complete English sentences describing what the program does,
// algorithm used, etc.
#define _CRT_SECURE_NO_WARNINGS // Disable warnings (and errors) when using non-secure versions of printf, scanf, strcpy, etc.
#include <stdio.h> // Needed for working with printf and scanf
#include <math.h>
int main(void)
{
// Constant and Variable Declarations
double startingHeight = 0.0;
double heightBall = 0.0;
double volicetyBall = 0.0;
double ballTime = 0.0;
double downTime = 0.25;
double maxHeight = 0.0;
double timeLast = 0.0;
// *** Your program goes here ***
do {
printf("\n Enter initial height of the ball (in ft.): ");
scanf("%lf", &startingHeight);
if (startingHeight < 0) {
printf("\t The initial height must be greater than or equal to 0");
}
} while (startingHeight < 0);
do {
printf("\n Enter initial velocity of the ball (in ft. per sec.): ");
scanf("%lf", &volicetyBall);
if (volicetyBall < 0) {
printf("\t The initial velocity must be greater than or equal to 0");
}
} while (volicetyBall < 0);
printf("\n");
printf("Time\tHeight\n");
if (startingHeight < 0 && volicetyBall > 0) {
printf("%0.2f\t%0.1f\n", ballTime, startingHeight);
}
else {
do {
heightBall = startingHeight + volicetyBall * ballTime - 16 * pow(ballTime, 2);
if (maxHeight < heightBall) {
maxHeight = heightBall;
}
if (heightBall >= 0) {
printf("%0.2f\t%0.1f\n", ballTime, heightBall);
timeLast = ballTime;
}
ballTime = ballTime + downTime;
} while (heightBall >= 0);
}
printf("\n The maximum height the ball will reach is %0.1f feet\n", maxHeight);
printf("\n The time for the ball to reach the ground is %0.2f seconds\n", timeLast);
printf("\n");
return 0;
} // end main()
答案 0 :(得分:0)
是的,它是C,但是C导致 Undefined Behavior 未能验证用户输入。如果用户在输入球高时滑行并击中'r'
而不是'5'
,在代码中会发生什么?
要防止无效输入并从无效输入中恢复,您需要检查输入功能的返回结果和空stdin
任何有问题的字符 阅读,例如
/* validate every input */
if (scanf ("%lf", &startingHeight) != 1) {
int c;
fputs ("error: invalid input.\n", stderr);
do /* read offending chars until '\n' or EOF */
c = getchar();
while (c != '\n' && c != EOF);
continue;
}
尽管使用do .... while (...)
,您只是跳到循环的结尾,而您的startingHeight < 0
测试为false,然后跳到速度问题,最好是:
for (;;) { /* loop continually until valid input */
printf ("\n Enter initial height of the ball (in ft.): ");
/* validate every input */
if (scanf ("%lf", &startingHeight) != 1) {
int c;
fputs ("error: invalid input.\n", stderr);
do /* read offending chars until '\n' or EOF */
c = getchar();
while (c != '\n' && c != EOF);
continue;
}
if (startingHeight < 0) {
printf ("\t The initial height must be greater than or equal to 0");
/* you should empty stdin here as well -- hint: create a function */
}
else
break;
}
这样,您可以重新询问高度问题,直到您拥有有效的startingHeight >= 0
。
其他形式,请勿使用printf
输出单个字符,例如putchar
或fputc
所要用于的单个字符。
// printf ("\n"); /* replace this with... */
putchar ('\n');
此外,也不需要两个printf
进行的两次puts
调用(您不执行任何转换),例如
// printf("\n");
// printf("Time\tHeight\n");
puts ("\nTime\tHeight");
否则,除了正常的开始无法通过验证之外,您的表现还不错。