计算最小,最大和平均测试分数输入验证

时间:2015-03-22 15:54:01

标签: c max min

嘿伙计们我在c编程中有一个任务说:

“编写一个输入整数测试分数列表的C程序 在0到100之间,并计算最高分数,最小分数 得分和平均得分。使用-1作为标记来标记结尾 输入。拒绝0到100范围之外的任何测试分数。您的程序 还应该检测并拒绝错误的输入,例如字母和字符。 您应该有一个读取和验证输入测试分数的函数 并拒绝无效和错误的输入。这是一个示例运行“

我写了一段代码,但我的问题是如何拒绝像字符这样的输入..:

#include <stdio.h>
#define sentinel -1     // to end the program

int main ()
{
    int status, score, sum, i, max, min;
    double avrg;
    i = 0;          /* the counter */
    sum = 0;            // the total scores
    printf (" Enter the score , %d to termenate > ", sentinel);
    status = scanf ("%d", &score);  // checking the validity of the input

    max = score;        // the initial maximum
    min = score;        // the initial minimum

    while (score != sentinel) {

        while (score < 0 || score > 100)    // no negative or more than 100 score

        {
            printf ("The number is out of the range ; try again> ");
            scanf ("%d", &score);
        }

        sum = sum + score;
        i = i + 1;
        printf (" Enter the score , %d to termenate > ", sentinel);
        status = scanf ("%d", &score);
        if (score > max && score < 100)
            max = score;
        if (score < min && score > 0)
            min = score;
    }

    if (i != 0) {
        avrg = (double) sum / i;    // the sum of scores over the number of scores
        printf (" The avarage is : \n %.2f ", avrg);
        printf ("\n");
        printf (" Maximum score is : \n %d", max);
        printf ("\n");
        printf ("Minmum score is : \n %d", min);
    } else          // when the user ends the program without entering any value
    {
        printf ("You didn't enter any score");
    }

    return 0;
}

我希望你能帮助我

2 个答案:

答案 0 :(得分:1)

对多个输入使用scanf时,您需要注意几个条件。那些是matching失败和read失败。检查scanf返回允许您识别两者。如果在while循环中接受重复的整数输入,如果发生匹配失败,您还必须确保清空stdin以防止无限循环。有几种方法,但最简单的整数输入是在再次使用getchar之前,只需使用charsstdin读取所有scanf

以下是实施此方法的代码的快速示例:

#include <stdio.h>
#include <limits.h>     /* for INT_MAX */

#define SENTINEL -1     /* value to end the program         */
#define MAXSCORE 100    /* max input for score allowed      */

int main ()
{
    int status = 0;     /* always initialize you variables  */
    int score = 0;
    size_t sum = 0;     /* size_t for non-negative numbers  */
    size_t idx = 1;
    size_t max = 0;
    size_t min = INT_MAX;
    double avrg = 0.0;
    int c = 0;

    printf ("\n Enter scores [ '%d' to end entry ]\n", SENTINEL);

    while (printf ("\n  score: ") && (status = scanf ("%d", &score)) != -1)
    {
        /* check for matching failure, empty input buffer */
        if (status == 0) do { c = getchar(); } while ( c != '\n' && c != EOF);

        if (score == SENTINEL) break;
        if (score > MAXSCORE || score < 0) continue;

        if (score > max) max = score;
        if (score < min) min = score;

        sum += score;
        avrg = (double)sum/idx++;

        printf ("\n    minimum : %3zu\n    maximum : %3zu\n    average : %.2f\n", min, max, avrg);
    }

    printf ("\n Input complete\n\n");

    if (idx > 1)
        printf ("  number of scores : %3zu    Max score : %3zu    Min score : %3zu    Avg : %.2f\n\n",
                idx - 1, max, min, avrg);
    return 0;
}

<强>输出

$ ./bin/grades

 Enter scores [ '-1' to end entry ]

  score: 77

    minimum :  77
    maximum :  77
    average : 77.00

  score: 88

    minimum :  77
    maximum :  88
    average : 82.50

  score: 83

    minimum :  77
    maximum :  88
    average : 82.67

  score: -88

  score: 108

  score: G

  score: -1

 Input complete

  number of scores :   3    Max score :  88    Min score :  77    Avg : 82.67

答案 1 :(得分:0)

也许您可以先输入字符串作为字符串,然后使用strtol检查无效字符?

例如Input validation of an Integer using atoi()

中的答案