简体   繁体   English

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

[英]I want to make sure my program is correct this is c programming

i just wanted to make sure my formula was correct and wanted to see if there is a way to make the height go all the way to zero and the time count with it here is my code so far i have ran it with some numbers like height is 10 and velocity is 0 and it will start the time count at 0.0 and increment in .25 and the height will start at 10 but it will not count down to zero for the height and will stop at .75 for the time im sorry if its confusing what im trying to explain bc im confused my self我只是想确保我的公式是正确的,并想看看是否有办法让高度一直为零,这里的时间计数是我的代码,到目前为止我已经用一些数字来运行它,比如高度是 10,速度为 0,它将从 0.0 开始计时,并以 0.25 为增量,高度将从 10 开始,但它不会倒数到高度为零,并且会在 0.75 的时间停止,对不起,如果它混淆了我试图解释的东西 bc 我混淆了我自己

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

Yes, it's C, but C that invites Undefined Behavior for failing to validate the user input.是的,它是 C,但是 C 会因未能验证用户输入而导致未定义行为 What happens in your code if the user slips and hits 'r' instead of '5' when entering the height of the ball?如果用户在输入球的高度时滑倒并点击'r'而不是'5' ,您的代码会发生什么情况?

To prevent and recover from an invalid input you need to check the return of your input function and empty stdin of any offending characters before your next attempted read, eg为了防止和恢复无效输入,您需要在下一次尝试读取之前检查输入函数的返回任何有问题字符的空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;
    }

All though with a do .... while (...) you simply jump to the end of the loop and your startingHeight < 0 test false and you skip to the velocity question, better to:尽管使用do .... while (...)您只需跳到循环的末尾,并且您的startingHeight < 0高度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;
}

That way you re-ask the height question until you have a valid startingHeight >= 0 .这样你就可以重新问身高问题,直到你有一个有效的startingHeight >= 0高度startingHeight >= 0

Other nit, don't use printf to output a single-character, that is what putchar or fputc is for, eg其他尼特,不要使用printf输出单个字符,这就是putcharfputc的用途,例如

// printf ("\n");      /* replace this with... */
putchar ('\n');

Also, no need for two- printf calls (you are not performing any conversions) where a single puts will do, eg此外,不需要两个printf调用(您不执行任何转换),其中单个puts将执行,例如

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

Otherwise, aside from the normal beginning failure to validates, you are doing fairly well.否则,除了正常的开始验证失败之外,您做得还不错。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM