在C中直接输入整数 [英] Inputing integers straight across in C
问题描述
我正在上C语言入门课程,我想知道是否有一种方法可以直接输入整数并将它们平均在一起?我试图使我的程序尽可能整洁.
I'm in a beginner C course and I was wondering if there's a way to input integers straight across and averages them together? I'm trying to make my program nice and tidy as possible.
我想像这样直接输入整数:
I want to input integers straight across like:
输入温度,完成后输入00:
Enter the temperatures and Enter 00 when finished:
60 80 97 42
Average is: 69.75
我不想输入如下所示的整数:
I don't want to input integers like shown below:
输入温度,完成后输入00:75
Enter the temperatures and Enter 00 when finished: 75
输入温度,完成后输入00:80
Enter the temperatures and Enter 00 when finished: 80
输入温度,完成后输入00:46
Enter the temperatures and Enter 00 when finished: 46
输入温度,完成后输入00:91
Enter the temperatures and Enter 00 when finished: 91
平均为:73
推荐答案
#include <stdio.h>
#include <string.h>
int main(void){
char input[64];
double ave = 0.0, value;
int count = 0;
printf("Enter the temperatures and Enter 00 when finished:\n");
while(1){
if(1==scanf("%63s", input)){
if(strcmp(input, "00") == 0)
break;
if(1==sscanf(input, "%lf", &value))
ave += (value - ave) / ++count;
}
}
if(count)
printf("Average is: %g\n", ave);
else
printf("Input one or more values\n");
return 0;
}
这篇关于在C中直接输入整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!