gpt4 book ai didi

c - 如何停止读取输入并分析C中的数据

转载 作者:行者123 更新时间:2023-11-30 16:47:56 25 4
gpt4 key购买 nike

我需要在 C 中创建一个代码,读取数字对列表,第一个数字是产品编号(介于 1 和 以及由输入确定的产品总数之间),第二个数字是产品编号。从 1-10 进行评分并将其存储在 2D 数组中。数组中从 1 开始的行是产品编号,第 1-10 列是评级。第 11 列是星星总数,第 12 列是评分总数。我必须使用 scanf 读取值,当 scan()!=2 时,循环应该停止并生成报告。

报告应列出每个产品每个值的评分数量(即 2 个 5 星评分、17 个 10 星评分等)以及平均评分。

我认为我的逻辑是正确的,并且我的代码可以编译,但当我尝试生成报告时,它不断返回错误。有什么线索可能是什么问题吗?

这是我到目前为止的代码:

int productStar[][13] = {0};
int i = 0;
int product = 0;
int stars = 0;
int howMany = 0;
int ratings = 0;
float avg = 0;
int sum = 0;

printf("How many unique products?\n");
scanf("%d", &howMany);

printf("Please enter product # and stars\n");

while(1)
{
scanf("%d %d", &product, &stars); /* Reads in two values; first is product number, second is rating */

if (scanf("%d %d", &product, &stars) != 2) /* kills the loop */
{
printf("Hi from the inside\n");
break;
}


productStar[product][stars] += 1; /* adds tally to specific product rating */

productStar[product][11] += stars; /* increments total number of stars; used later to find avg */

productStar[product][12] += 1; /* increments how many ratings */

}

for (int product = 0; product <= howMany; ++product)
{
for (int stars = 0; stars <= 10; ++stars)
{
avg = productStar[product][11]/productStar[product][12];

printf("Product %d Information:\n", product);
printf("1 star: %d\n", productStar[product][1]);
printf("2 star: %d\n", productStar[product][2]);
printf("3 star: %d\n", productStar[product][3]);
printf("4 star: %d\n", productStar[product][4]);
printf("5 star: %d\n", productStar[product][5]);
printf("6 star: %d\n", productStar[product][6]);
printf("7 star: %d\n", productStar[product][7]);
printf("8 star: %d\n", productStar[product][8]);
printf("9 star: %d\n", productStar[product][9]);
printf("10 star: %d\n", productStar[product][10]);
printf("Average rating: %2.2f out of 10 (%d ratings total).\n", avg, ratings);
printf("--------------------\n");
}
}

for (product = 0; product <= howMany; ++product)
{
sum += productStar[product][12];
/* Sum of all ratings */
}

printf("%d product ratings evaluated in all.\n\n", sum);


return 0;

}

最佳答案

您连续两次从标准输入读取内容,我认为您不想这样做。也许尝试将输入读取循环中的第二个 scanf 更改为检查产品和星星是否不是空字符串的语句:

  while(1) {
scanf("%d %d", &product, &stars); /* Reads in two values; first is product number, second is rating */

if (product == '\0' || stars == '\0') /* kills the loop */
{
// Break out of input loop and process data.
break;
}
}

// Processing code.

此外,检查是否被零除也是一个好主意:

if (productStar[product][12] != 0) {
avg = productStar[product][11]/productStar[product][12];
}

在我进行这些更改后,您的代码完美运行:)

#include <stdio.h>

int productStar[][13] = {0};
int i = 0;
int product = 0;
int stars = 0;
int howMany = 0;
int ratings = 0;
float avg = 0;
int sum = 0;

int main(void) {
printf("How many unique products?\n");
scanf("%d", &howMany);

printf("Please enter product # and stars\n");

while(1)
{


scanf("%d %d", &product, &stars); /* Reads in two values; first is product number, second is rating */

if (product == '\0' || stars == '\0') /* kills the loop */
{
break;
}


productStar[product][stars] += 1; /* adds tally to specific product rating */

productStar[product][11] += stars; /* increments total number of stars; used later to find avg */

productStar[product][12] += 1; /* increments how many ratings */

}

for (int product = 0; product <= howMany; ++product)
{
for (int stars = 0; stars <= 10; ++stars)
{
if (productStar[product][12] != 0) {
avg = productStar[product][11]/productStar[product][12];
}

printf("Product %d Information:\n", product);
printf("1 star: %d\n", productStar[product][1]);
printf("2 star: %d\n", productStar[product][2]);
printf("3 star: %d\n", productStar[product][3]);
printf("4 star: %d\n", productStar[product][4]);
printf("5 star: %d\n", productStar[product][5]);
printf("6 star: %d\n", productStar[product][6]);
printf("7 star: %d\n", productStar[product][7]);
printf("8 star: %d\n", productStar[product][8]);
printf("9 star: %d\n", productStar[product][9]);
printf("10 star: %d\n", productStar[product][10]);
printf("Average rating: %2.2f out of 10 (%d ratings total).\n", avg, ratings);
printf("--------------------\n");
}
}



for (product = 0; product <= howMany; ++product)
{
sum += productStar[product][12];
/* Sum of all ratings */
}

printf("%d product ratings evaluated in all.\n\n", sum);


return 0;
}

关于c - 如何停止读取输入并分析C中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43164421/

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