gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 15:22:31 25 4
gpt4 key购买 nike

嘿伙计们,我有一份用 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;
}

希望你能帮助我

最佳答案

当使用 scanf 进行多个输入时,您必须注意几个条件。这些是匹配失败和读取失败。检查 scanf 返回可以让您识别两者。在 while 循环中接受重复的整数输入的情况下,如果发生匹配失败,还必须确保清空 stdin 以防止无限循环。有多种方法,但最直接的整数输入是在使用 之前简单地使用 getcharstdin 读取所有 chars再次扫描

下面是实现此方法的代码的快速示例:

#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

关于计算最小、最大和平均测试分数输入验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29196469/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com