C #序列的平均值
我很难计算出平均函数中的循环。用户输入序列,如果序列中的数字数量大于0,则输出数字的平均值,并重复另一个序列。如果序列没有编号,退出程序 我知道我告诉平均值函数在sum=0时返回0,这就是为什么它在第一个序列之后退出(我想) 有没有关于如何避免这种情况的建议?伪代码,如果可能的话C #序列的平均值,c,C,我很难计算出平均函数中的循环。用户输入序列,如果序列中的数字数量大于0,则输出数字的平均值,并重复另一个序列。如果序列没有编号,退出程序 我知道我告诉平均值函数在sum=0时返回0,这就是为什么它在第一个序列之后退出(我想) 有没有关于如何避免这种情况的建议?伪代码,如果可能的话 #include<stdio.h> #include<stdlib.h> double average(); int main () { while( average() )
#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;
}
}
我认为您可以首先获取名为count的变量的输入,要求用户按顺序输入数字的总数,然后按顺序获取这些值。 如果计数为0,则退出程序。。否则继续寻找平均值 这是因为在您的情况下,每次都必须输入一个非数字字符来结束序列
#包括
#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;
}
#包括
整数平均值(无效);
内部主(空)
{
while(average())
;
返回0;
}
整数平均值(无效)
{
双n,和=0;
整数计数=0;
printf(“输入顺序:”);
而(扫描频率(“%lf”,&n)==1)
{
总和+=n;
计数++;
}
INTC;
而((c=getchar())!=EOF&&c!='\n')
;
如果(总和>0)
{
printf(“平均值为%.2f\n”,总和/计数);
返回1;
}
其他的
返回0;
}
在转换失败后,这将读取行上的任何内容直到换行符,从而设置您读取下一个序列。改进了
scanf()
的循环测试;它也将在EOF上退出。师里的演员是不必要的;编译器必须将count
转换为double
,因为sum
是double
,即使您没有明确告诉它这样做。average()
的返回类型不是双精度;它是一个布尔值,经典拼写为int
。在C99或更高版本中(代码假定您有;如果没有,您将被困在Windows上,需要将int c;
移动到函数顶部),然后您可以#包括,并使用bool
的返回类型替换return 1
by返回true
和replace返回0
by返回false代码>我最终得到了这个,它工作得非常好:
#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
#包括
#包括
#包括
双倍平均数();
int main(){
while(average()){}
}//端干管
双倍平均数(){
双n,和=0;
整数计数=0;
printf(“输入顺序:”);
getchar();
while(scanf(“%lf”,&n)){
总和+=n;
计数++;
}//结束时
如果(n>0){
printf(“平均值:%.2f\n”,总和/(双重)计数);
返回1;
}//如果结束
其他的
返回0;
}//端函数
我猜您的输入流仍处于失败或eof状态,因此scanf函数返回时不允许输入。不幸的是,我对C IO了解不多,所以我不知道什么是cin.ignore()
或类似的等价物。也许让sum
默认值为-1,然后退出,而不是0。等价物是getchar();而且它工作得很好!非常感谢:)如果您键入12 3 4 x
,则报告的平均值为3,而不是6.3333,因为您切掉了第一个数字。如果您键入12 3 4 xxx
也可能不会产生预期效果。@JonathanLeffler谢谢。“1234xxx”,回馈6.333有效,但“1234x”无效。我添加了“getchar();”在我的“返回1;”之前语句。如果第一个输入是1234x
,则得到3;如果第二个或后续的输入是1234x
,那么你会得到6.33,因为前导的getchar()
会吃掉前一行的x
(并且scanf()
读取换行符。可能if(count>0)printf(“平均值…”
?——啊,我现在明白了,这就是OP所做的。Hmmmm。
#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