【问题标题】:Average of # Sequence# 个序列的平均值
【发布时间】:2014-03-10 02:16:41
【问题描述】:

我无法找出平均函数中的循环。用户输入序列,如果序列中的数字数量大于 0,则输出数字的平均值并重复另一个序列。如果序列没有数字,则退出程序。

我知道我告诉平均函数在 sum = 0 时返回 0,这就是它在第一个序列之后退出的原因(我认为)。

关于如何避免这种情况的任何建议?尽可能使用伪代码!

#include<stdio.h>
#include<stdlib.h>

double average();

int main () 
{
    while( average() ) 
    {
    }
}//end main

double average() 
{
    double n, sum = 0;
    int count = 0;

    printf ( "Enter sequence: " );
    while( scanf( "%lf", &n ) )
    {
        sum += n;
        count++;
    }

    if( sum > 0 )
    {
        printf( "average is %.2f\n", sum/(double)count );
        return 1;
    }
    else
        return 0;
    }
}

这是我的输出:

Enter sequence: 3 4 5 x
    average: 4.00
Enter sequence: Press any key to continue . . .

【问题讨论】:

  • 我猜您的输入流仍处于失败或 eof 状态,因此 scanf 函数在不允许输入的情况下返回。不幸的是我对C IO了解不多,所以我不知道cin.ignore()或类似的东西是什么。
  • 也许让sum 默认值为 -1 并退出而不是 0。
  • 等价于getchar();它工作得很好!非常感谢:)

标签: c


【解决方案1】:
#include <stdio.h>
#include <stdlib.h>

int average(void);

int main(void) 
{
    while (average()) 
        ;
    return 0;
}

int average(void) 
{
    double n, sum = 0;
    int count = 0;

    printf("Enter sequence: ");
    while (scanf( "%lf", &n ) == 1)
    {
        sum += n;
        count++;
    }
    int c;
    while ((c = getchar()) != EOF && c != '\n')
        ;

    if (sum > 0)
    {
        printf("average is %.2f\n", sum / count);
        return 1;
    }
    else
        return 0;
}

这会在转换失败后读取行上的任何内容,直到换行,从而为读取下一个序列做好准备。改进了scanf() 的循环测试;它也会在 EOF 上退出。该部门的演员是不必要的。编译器必须将count 转换为double,因为sum 是double,即使您没有明确告诉它这样做。 average() 的返回类型不是 double;它是一个布尔值,经典拼写为int。在 C99 或更高版本中(代码假定您有;如果没有,您会卡在 Windows 上,需要将 int c; 移动到函数顶部),然后您可以 #include &lt;stdbool.h&gt; 并使用返回类型的bool 并将return 1; 替换为return true; 并将return 0; 替换为return false;。

【讨论】:

  • 可能是if (count &gt; 0) printf("average...? ---- 啊,我现在明白了,这就是 OP 所做的。嗯。
【解决方案2】:

我认为您最初可以获取名为 count 的变量的输入,要求用户按顺序输入数字的总数,然后按顺序获取这些值。 如果计数为0,则退出程序..否则继续求平均值。

这是因为在您的情况下,您每次都必须输入一个非数字字符才能结束序列。

【讨论】:

    【解决方案3】:

    好吧,我最终得到了这个并且它完美地工作:

    #include<stdio.h>
    #include<stdlib.h>
    #include<ctype.h>
    
    double average();
    
    int main () {
        while( average() ) {}
    }//end main
    
    double average() {
        double n, sum = 0;
        int count = 0;
    
        printf ( "Enter sequence: " );
        getchar();
        while( scanf( "%lf", &n ) ){
            sum += n;
            count++;
        }//end while
    
        if( n > 0 ){
            printf( "    average: %.2f\n", sum/(double)count );
            return 1;
        }//end if
        else
            return 0;
    }//end function
    

    【讨论】:

    • 如果您输入 12 3 4 x,则将报告平均值 3 而不是 6.3333,因为您将第一个数字去掉。如果你输入12 3 4 xxx 也可能不会产生预期的效果。
    • @JonathanLeffler 谢谢。好吧,'12 3 4 xxx',回馈 6.333 有效,但 '12 3 4 x' 没有。我添加了 'getchar();'在我的'return 1;'之前声明。
    • 如果第一个输入是12 3 4 x,那么你得到3;如果第二个或后续输入是12 3 4 x,那么您会得到 6.33,因为前导的 getchar() 吃掉了前一行中的 x(而scanf() 读取了换行符。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-18
    • 2013-07-22
    • 1970-01-01
    • 2017-02-06
    • 2021-07-17
    • 2018-07-12
    • 2015-09-11
    相关资源
    最近更新 更多