Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我想确保我的程序是正确的这是c语言编程_C - Fatal编程技术网

我想确保我的程序是正确的这是c语言编程

我想确保我的程序是正确的这是c语言编程,c,C,我只是想确定我的公式是正确的,想看看是否有办法使高度一直到零,并且时间计数,这是我的代码,到目前为止,我已经用一些数字运行了它,比如高度是10,速度是0,它将在0.0开始时间计数,增量是.25,高度将在10开始,但是不会倒计时到零的高度,会停在0.75的时候,对不起,如果它混淆了我试图解释的内容,我混淆了我自己 // Programmer: Your Name // Date: Date // Program Name: The name of the pr

我只是想确定我的公式是正确的,想看看是否有办法使高度一直到零,并且时间计数,这是我的代码,到目前为止,我已经用一些数字运行了它,比如高度是10,速度是0,它将在0.0开始时间计数,增量是.25,高度将在10开始,但是不会倒计时到零的高度,会停在0.75的时候,对不起,如果它混淆了我试图解释的内容,我混淆了我自己

// 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()

是的,它是C,但是C会因为未能验证用户输入而导致未定义的行为。如果用户在输入球的高度时滑倒并点击“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;
    }
尽管如此。。。。虽然您只需跳到循环的末尾,开始高度<0测试为假,然后跳到速度问题,最好:

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;
}
此外,不需要两个printf调用,因为您没有执行任何转换,而一个put就可以了,例如

// printf("\n");
// printf("Time\tHeight\n");
puts ("\nTime\tHeight");

否则,除了正常的开始验证失败之外,您做得相当不错。

您认为代码是正确的,你想让我们验证一下吗?是的,想看看是否有办法让循环运行到高度为零,使用visual Studio?这是代码的链接,这样就不会错过了我已经将复制粘贴的魔力应用到了你的代码中。另外,这将是我最后一次去那个广告泛滥的下载站点下载一个简单的文本文件。谢谢你还想知道,当我运行这个程序时,我把10作为开始高度,0作为它倒数的速度,但在它到达地面之前停止?如何使其从用户输入的起始高度倒计时到始终为零的地面?如果heightBall>=0,则会阻止打印任何负值,并且仅在heightBall>=0时循环;此外,高度=10,速度=0的球在0.75秒到1.0秒之间到达地面。1.0秒时球在-6英尺处。只需调整你的限制并取消打印限制。
// printf("\n");
// printf("Time\tHeight\n");
puts ("\nTime\tHeight");